diff --git a/go.mod b/go.mod index bdc3732727a2..914f19af3414 100644 --- a/go.mod +++ b/go.mod @@ -181,7 +181,7 @@ require ( go.opentelemetry.io/otel/trace v1.20.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/mod v0.14.0 // indirect - golang.org/x/sync v0.6.0 // indirect + golang.org/x/sync v0.6.0 golang.org/x/sys v0.18.0 // indirect golang.org/x/term v0.18.0 // indirect golang.org/x/time v0.5.0 // indirect diff --git a/pkg/providers/nutanix/validator.go b/pkg/providers/nutanix/validator.go index 432e2c72773d..63228db8bd34 100644 --- a/pkg/providers/nutanix/validator.go +++ b/pkg/providers/nutanix/validator.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "regexp" - "slices" "strconv" "strings" @@ -178,7 +177,7 @@ func (v *Validator) validateFailureDomains(ctx context.Context, client Client, s func (v *Validator) validateWorkerMachineGroup(spec *cluster.Spec, workerMachineGroupName string) error { workerMachineGroupNames := getWorkerMachineGroupNames(spec) - if !slices.Contains(workerMachineGroupNames, workerMachineGroupName) { + if !sliceContains(workerMachineGroupNames, workerMachineGroupName) { return fmt.Errorf("worker machine group %s not found in the cluster worker node group definitions", workerMachineGroupName) } @@ -856,6 +855,15 @@ func findProjectUUIDByName(ctx context.Context, v3Client Client, projectName str return res.Entities[0].Metadata.UUID, nil } +func sliceContains(slice []string, element string) bool { + for _, sliceElement := range slice { + if sliceElement == element { + return true + } + } + return false +} + func isRequestedGPUAssignable(gpu v3.GPU, requestedGpu anywherev1.NutanixGPUIdentifier) bool { if requestedGpu.Type == anywherev1.NutanixGPUIdentifierDeviceID { return (*gpu.DeviceID == *requestedGpu.DeviceID) && gpu.Assignable diff --git a/pkg/providers/nutanix/validator_test.go b/pkg/providers/nutanix/validator_test.go index 063822d5b6dc..e3d6b89e4197 100644 --- a/pkg/providers/nutanix/validator_test.go +++ b/pkg/providers/nutanix/validator_test.go @@ -61,6 +61,9 @@ var nutanixDatacenterConfigSpecWithFailureDomainInvalidCluster string //go:embed testdata/datacenterConfig_with_failure_domains_invalid_subnet.yaml var nutanixDatacenterConfigSpecWithFailureDomainInvalidSubnet string +//go:embed testdata/datacenterConfig_with_failure_domains_invalid_wg.yaml +var nutanixDatacenterConfigSpecWithFailureDomainInvalidWorkerMachineGroups string + func fakeClusterList() *v3.ClusterListIntentResponse { return &v3.ClusterListIntentResponse{ Entities: []*v3.ClusterIntentResponse{ @@ -219,6 +222,40 @@ func fakeProjectList() *v3.ProjectListResponse { } } +func TestSliceContainsFunc(t *testing.T) { + tests := []struct { + name string + slice []string + value string + expected bool + }{ + { + name: "empty slice", + slice: []string{}, + value: "test", + expected: false, + }, + { + name: "slice contains value", + slice: []string{"test", "test1", "test2"}, + value: "test", + expected: true, + }, + { + name: "slice does not contain value", + slice: []string{"test", "test2", "test3"}, + value: "test1", + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.expected, sliceContains(tt.slice, tt.value)) + }) + } +} + func TestNutanixValidatorValidateMachineConfig(t *testing.T) { ctrl := gomock.NewController(t) @@ -1460,6 +1497,11 @@ func TestNutanixValidatorValidateDatacenterConfig(t *testing.T) { dcConfFile: nutanixDatacenterConfigSpecWithFailureDomainInvalidSubnet, expectErr: true, }, + { + name: "failure domains with invalid workerMachineGroups", + dcConfFile: nutanixDatacenterConfigSpecWithFailureDomainInvalidWorkerMachineGroups, + expectErr: true, + }, } ctrl := gomock.NewController(t)