@@ -19,6 +19,8 @@ package webhooks
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "slices"
23
+ "sort"
22
24
23
25
apierrors "k8s.io/apimachinery/pkg/api/errors"
24
26
"k8s.io/apimachinery/pkg/runtime"
@@ -72,7 +74,14 @@ func (webhook *DevCluster) ValidateCreate(_ context.Context, obj runtime.Object)
72
74
}
73
75
74
76
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
75
- func (webhook * DevCluster ) ValidateUpdate (_ context.Context , _ , _ runtime.Object ) (admission.Warnings , error ) {
77
+ func (webhook * DevCluster ) ValidateUpdate (_ context.Context , _ , new runtime.Object ) (admission.Warnings , error ) {
78
+ cluster , ok := new .(* infrav1.DevCluster )
79
+ if ! ok {
80
+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a DevCluster but got a %T" , new ))
81
+ }
82
+ if allErrs := validateDevClusterSpec (cluster .Spec ); len (allErrs ) > 0 {
83
+ return nil , apierrors .NewInvalid (infrav1 .GroupVersion .WithKind ("DevCluster" ).GroupKind (), cluster .Name , allErrs )
84
+ }
76
85
return nil , nil
77
86
}
78
87
@@ -94,6 +103,16 @@ func defaultDevClusterSpec(s *infrav1.DevClusterSpec) {
94
103
}
95
104
}
96
105
97
- func validateDevClusterSpec (_ infrav1.DevClusterSpec ) field.ErrorList {
106
+ func validateDevClusterSpec (spec infrav1.DevClusterSpec ) field.ErrorList {
107
+ domainNames := make ([]string , 0 , len (spec .Backend .Docker .FailureDomains ))
108
+ for _ , fd := range spec .Backend .Docker .FailureDomains {
109
+ domainNames = append (domainNames , fd .Name )
110
+ }
111
+ originalDomainNames := domainNames
112
+ sort .Strings (domainNames )
113
+ if ! slices .Equal (originalDomainNames , domainNames ) {
114
+ return field.ErrorList {field .Invalid (field .NewPath ("spec" , "backend" , "docker" , "failureDomains" ), spec .Backend .Docker .FailureDomains , "failure domains must be sorted by name" )}
115
+ }
116
+
98
117
return nil
99
118
}
0 commit comments