Skip to content

Commit c137dfe

Browse files
committed
add validation to check whether the failure domains are sorted by name
Signed-off-by: sivchari <[email protected]>
1 parent 69541dc commit c137dfe

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

test/infrastructure/docker/internal/webhooks/devcluster_webhook.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package webhooks
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
23+
"sort"
2224

2325
apierrors "k8s.io/apimachinery/pkg/api/errors"
2426
"k8s.io/apimachinery/pkg/runtime"
@@ -72,7 +74,14 @@ func (webhook *DevCluster) ValidateCreate(_ context.Context, obj runtime.Object)
7274
}
7375

7476
// 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+
}
7685
return nil, nil
7786
}
7887

@@ -94,6 +103,16 @@ func defaultDevClusterSpec(s *infrav1.DevClusterSpec) {
94103
}
95104
}
96105

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+
98117
return nil
99118
}

test/infrastructure/docker/internal/webhooks/dockercluster_webhook.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package webhooks
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
23+
"sort"
2224

2325
apierrors "k8s.io/apimachinery/pkg/api/errors"
2426
"k8s.io/apimachinery/pkg/runtime"
@@ -87,6 +89,15 @@ func defaultDockerClusterSpec(s *infrav1.DockerClusterSpec) {
8789
}
8890
}
8991

90-
func validateDockerClusterSpec(_ infrav1.DockerClusterSpec) field.ErrorList {
92+
func validateDockerClusterSpec(spec infrav1.DockerClusterSpec) field.ErrorList {
93+
domainNames := make([]string, 0, len(spec.FailureDomains))
94+
for _, fd := range spec.FailureDomains {
95+
domainNames = append(domainNames, fd.Name)
96+
}
97+
originalDomainNames := domainNames
98+
sort.Strings(domainNames)
99+
if !slices.Equal(originalDomainNames, domainNames) {
100+
return field.ErrorList{field.Invalid(field.NewPath("spec", "failureDomains"), spec.FailureDomains, "failure domains must be sorted by name")}
101+
}
91102
return nil
92103
}

0 commit comments

Comments
 (0)