From 70b8ec8217d3571cd2612613f86dc3d245942363 Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Tue, 4 Jun 2024 18:56:25 +0000 Subject: [PATCH] Fix PR comments - add validation - fix template - add unittest for validation --- .../v1alpha1/nutanixdatacenterconfig_test.go | 16 +++++++++ .../v1alpha1/nutanixdatacenterconfig_types.go | 33 +++++++++++++++++++ pkg/providers/nutanix/config/cp-template.yaml | 6 ++-- pkg/providers/nutanix/template.go | 4 +-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/pkg/api/v1alpha1/nutanixdatacenterconfig_test.go b/pkg/api/v1alpha1/nutanixdatacenterconfig_test.go index 1eaf40e16525..1055bb8e05c6 100644 --- a/pkg/api/v1alpha1/nutanixdatacenterconfig_test.go +++ b/pkg/api/v1alpha1/nutanixdatacenterconfig_test.go @@ -175,6 +175,22 @@ func TestGetNutanixDatacenterConfigValidConfig(t *testing.T) { assert.Contains(t, err.Error(), "NutanixDatacenterConfig credentialRef name is not set or is empty") }, }, + { + name: "datacenterconfig-valid-failure-domains", + fileName: "testdata/nutanix/datacenterconfig-valid-failuredomains.yaml", + assertions: func(t *testing.T, dcConf *v1alpha1.NutanixDatacenterConfig) { + assert.NoError(t, dcConf.Validate()) + }, + }, + { + name: "datecenterconfig-invalid-failure-domains", + fileName: "testdata/nutanix/datacenterconfig-invalid-failuredomains.yaml", + assertions: func(t *testing.T, dcConf *v1alpha1.NutanixDatacenterConfig) { + err := dcConf.Validate() + assert.Error(t, err) + assert.Contains(t, err.Error(), "NutanixDatacenterConfig.Spec.FailureDomains.Subnets: missing subnet UUID: default/eksa-unit-test") + }, + }, } for _, test := range tests { diff --git a/pkg/api/v1alpha1/nutanixdatacenterconfig_types.go b/pkg/api/v1alpha1/nutanixdatacenterconfig_types.go index 54e13815ebde..465ad17624cb 100644 --- a/pkg/api/v1alpha1/nutanixdatacenterconfig_types.go +++ b/pkg/api/v1alpha1/nutanixdatacenterconfig_types.go @@ -165,9 +165,42 @@ func (in *NutanixDatacenterConfig) Validate() error { } } + if in.Spec.FailureDomains != nil && len(in.Spec.FailureDomains) != 0 { + dccName := in.Namespace + "/" + in.Name + validateClusterResourceIdentifier := createValidateNutanixResourceFunc("NutanixDatacenterConfig.Spec.FailureDomains.Cluster", "cluster", dccName) + validateSubnetResourceIdentifier := createValidateNutanixResourceFunc("NutanixDatacenterConfig.Spec.FailureDomains.Subnets", "subnet", dccName) + for _, fd := range in.Spec.FailureDomains { + if err := validateClusterResourceIdentifier(&fd.Cluster); err != nil { + return err + } + + for _, subnet := range fd.Subnets { + if err := validateSubnetResourceIdentifier(&subnet); err != nil { + return err + } + } + } + } + return nil } +func createValidateNutanixResourceFunc(msgPrefix, entityName, mfstName string) (func (*NutanixResourceIdentifier) error) { + return func (ntnxRId *NutanixResourceIdentifier) error { + if ntnxRId.Type != NutanixIdentifierName && ntnxRId.Type != NutanixIdentifierUUID { + return fmt.Errorf("%s: invalid identifier type for %s: %s", msgPrefix, entityName, ntnxRId.Type) + } + + if ntnxRId.Type == NutanixIdentifierName && (ntnxRId.Name == nil || *ntnxRId.Name == "") { + return fmt.Errorf("%s: missing %s name: %s", msgPrefix, entityName, mfstName) + } else if ntnxRId.Type == NutanixIdentifierUUID && (ntnxRId.UUID == nil || *ntnxRId.UUID == "") { + return fmt.Errorf("%s: missing %s UUID: %s", msgPrefix, entityName, mfstName) + } + + return nil + } +} + // SetDefaults sets default values for the NutanixDatacenterConfig object. func (in *NutanixDatacenterConfig) SetDefaults() { if in.Spec.CredentialRef == nil { diff --git a/pkg/providers/nutanix/config/cp-template.yaml b/pkg/providers/nutanix/config/cp-template.yaml index 8791826c867d..82acdc53dc66 100644 --- a/pkg/providers/nutanix/config/cp-template.yaml +++ b/pkg/providers/nutanix/config/cp-template.yaml @@ -13,8 +13,7 @@ spec: {{- if (eq $value.Cluster.Type "uuid") }} type: "uuid" uuid: "{{ $value.Cluster.UUID }}" - {{- end}} - {{- if (eq $value.Cluster.Type "name") }} + {{- else if (eq $value.Cluster.Type "name") }} type: "name" name: "{{ $value.Cluster.Name }}" {{- end}} @@ -23,8 +22,7 @@ spec: {{- if (eq .Type "uuid") }} - type: "uuid" uuid: "{{ .UUID }}" - {{- end}} - {{- if (eq .Type "name") }} + {{- else if (eq .Type "name") }} - type: "name" name: "{{ .Name }}" {{- end}} diff --git a/pkg/providers/nutanix/template.go b/pkg/providers/nutanix/template.go index e0dcab12c979..24e93dcc03b2 100644 --- a/pkg/providers/nutanix/template.go +++ b/pkg/providers/nutanix/template.go @@ -465,9 +465,9 @@ func generateNoProxyList(clusterSpec *cluster.Spec) []string { return noProxyList } -func generateNutanixFailureDomains(FailureDomains []v1alpha1.NutanixDatacenterFailureDomain) []capxv1beta1.NutanixFailureDomain { +func generateNutanixFailureDomains(eksNutanixFailureDomains []v1alpha1.NutanixDatacenterFailureDomain) []capxv1beta1.NutanixFailureDomain { var failureDomains []capxv1beta1.NutanixFailureDomain - for _, fd := range FailureDomains { + for _, fd := range eksNutanixFailureDomains { subnets := []capxv1beta1.NutanixResourceIdentifier{} for _, subnet := range fd.Subnets {