From d2936df9a3f581086cfb465530d9afda2d6d8f95 Mon Sep 17 00:00:00 2001 From: Deepak Muley Date: Wed, 1 May 2024 15:04:18 -0700 Subject: [PATCH] removed optional annotation from non-optional fields same code as https://github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pull/400/files which was reverted in https://github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pull/414 --- Makefile | 10 +-- api/v1beta1/nutanixcluster_types.go | 4 +- api/v1beta1/nutanixmachine_types.go | 2 + ...ture.cluster.x-k8s.io_nutanixclusters.yaml | 3 + ...ster.x-k8s.io_nutanixclustertemplates.yaml | 3 + controllers/helpers_test.go | 24 ++++--- controllers/nutanixcluster_controller_test.go | 72 +++++++++---------- templates/cluster-template-clusterclass.yaml | 10 +++ templates/clusterclass/nct.yaml | 10 +++ test/e2e/config/nutanix.yaml | 2 +- test/e2e/nutanix_client_test.go | 12 ++++ 11 files changed, 93 insertions(+), 59 deletions(-) diff --git a/Makefile b/Makefile index 97bd591aa7..7f39312f4a 100644 --- a/Makefile +++ b/Makefile @@ -286,7 +286,7 @@ cluster-templates: ## Generate cluster templates for all flavors .PHONY: docker-build-e2e docker-build-e2e: ## Build docker image with the manager with e2e tag. echo "Git commit hash: ${GIT_COMMIT_HASH}" - KO_DOCKER_REPO=ko.local GOFLAGS="-ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH}" ko build -B --platform=${PLATFORMS_E2E} -t ${IMG_TAG} -L . + KO_DOCKER_REPO=ko.local GOFLAGS="-ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH}" ko build -B --platform=${PLATFORMS_E2E} -t ${IMG_TAG} . docker tag ko.local/cluster-api-provider-nutanix:${IMG_TAG} ${IMG_REPO}:e2e .PHONY: prepare-local-clusterctl @@ -335,7 +335,7 @@ test-e2e: docker-build-e2e cluster-e2e-templates cluster-templates ## Run the en mkdir -p $(ARTIFACTS) NUTANIX_LOG_LEVEL=debug ginkgo -v \ --trace \ - --progress \ + --show-node-events \ --tags=e2e \ --label-filter=$(LABEL_FILTER_ARGS) \ $(_SKIP_ARGS) \ @@ -345,7 +345,7 @@ test-e2e: docker-build-e2e cluster-e2e-templates cluster-templates ## Run the en --output-dir="$(ARTIFACTS)" \ --junit-report=${JUNIT_REPORT_FILE} \ --timeout="24h" \ - --always-emit-ginkgo-writer \ + -v \ $(GINKGO_ARGS) ./test/e2e -- \ -e2e.artifacts-folder="$(ARTIFACTS)" \ -e2e.config="$(E2E_CONF_FILE)" \ @@ -357,7 +357,7 @@ test-e2e-no-kubeproxy: docker-build-e2e cluster-e2e-templates-no-kubeproxy clust mkdir -p $(ARTIFACTS) NUTANIX_LOG_LEVEL=debug ginkgo -v \ --trace \ - --progress \ + --show-node-events \ --tags=e2e \ --label-filter=$(LABEL_FILTER_ARGS) \ $(_SKIP_ARGS) \ @@ -366,7 +366,7 @@ test-e2e-no-kubeproxy: docker-build-e2e cluster-e2e-templates-no-kubeproxy clust --output-dir="$(ARTIFACTS)" \ --junit-report=${JUNIT_REPORT_FILE} \ --timeout="24h" \ - --always-emit-ginkgo-writer \ + -v \ $(GINKGO_ARGS) ./test/e2e -- \ -e2e.artifacts-folder="$(ARTIFACTS)" \ -e2e.config="$(E2E_CONF_FILE)" \ diff --git a/api/v1beta1/nutanixcluster_types.go b/api/v1beta1/nutanixcluster_types.go index d3646998b2..3eb3121f7d 100644 --- a/api/v1beta1/nutanixcluster_types.go +++ b/api/v1beta1/nutanixcluster_types.go @@ -48,14 +48,12 @@ type NutanixClusterSpec struct { // ControlPlaneEndpoint represents the endpoint used to communicate with the control plane. // host can be either DNS name or ip address - // +optional ControlPlaneEndpoint capiv1.APIEndpoint `json:"controlPlaneEndpoint"` // prismCentral holds the endpoint address and port to access the Nutanix Prism Central. // When a cluster-wide proxy is installed, by default, this endpoint will be accessed via the proxy. // Should you wish for communication with this endpoint not to be proxied, please add the endpoint to the // proxy spec.noProxy list. - // +optional PrismCentral *credentialTypes.NutanixPrismEndpoint `json:"prismCentral"` // failureDomains configures failure domains information for the Nutanix platform. @@ -64,7 +62,7 @@ type NutanixClusterSpec struct { // +listType=map // +listMapKey=name // +optional - FailureDomains []NutanixFailureDomain `json:"failureDomains"` + FailureDomains []NutanixFailureDomain `json:"failureDomains,omitempty"` } // NutanixClusterStatus defines the observed state of NutanixCluster diff --git a/api/v1beta1/nutanixmachine_types.go b/api/v1beta1/nutanixmachine_types.go index 97a5c7cd5a..2b8675132d 100644 --- a/api/v1beta1/nutanixmachine_types.go +++ b/api/v1beta1/nutanixmachine_types.go @@ -83,6 +83,7 @@ type NutanixMachineSpec struct { Subnets []NutanixResourceIdentifier `json:"subnet"` // List of categories that need to be added to the machines. Categories must already exist in Prism Central // +kubebuilder:validation:Optional + // +optional AdditionalCategories []NutanixCategoryIdentifier `json:"additionalCategories,omitempty"` // Add the machine resources to a Prism Central project // +optional @@ -104,6 +105,7 @@ type NutanixMachineSpec struct { // List of GPU devices that need to be added to the machines. // +kubebuilder:validation:Optional + // +optional GPUs []NutanixGPU `json:"gpus,omitempty"` } diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclusters.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclusters.yaml index 67f0bb6b0f..c161dbd26c 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclusters.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclusters.yaml @@ -512,6 +512,9 @@ spec: - address - port type: object + required: + - controlPlaneEndpoint + - prismCentral type: object status: description: NutanixClusterStatus defines the observed state of NutanixCluster diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclustertemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclustertemplates.yaml index 8a55c5a1d0..dc3937706e 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclustertemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixclustertemplates.yaml @@ -228,6 +228,9 @@ spec: - address - port type: object + required: + - controlPlaneEndpoint + - prismCentral type: object required: - spec diff --git a/controllers/helpers_test.go b/controllers/helpers_test.go index 7b5f0027c5..2ab74c9391 100644 --- a/controllers/helpers_test.go +++ b/controllers/helpers_test.go @@ -23,7 +23,7 @@ import ( "testing" "github.com/golang/mock/gomock" - credentialtypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials" + credentialTypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials" prismclientv3 "github.com/nutanix-cloud-native/prism-go-client/v3" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -31,6 +31,7 @@ import ( "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + capiv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/util" infrav1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1" @@ -60,7 +61,8 @@ func TestControllerHelpers(t *testing.T) { Namespace: "default", }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ // Adding port info to override default value (0) Port: 9440, }, @@ -134,11 +136,11 @@ func TestGetPrismCentralClientForCluster(t *testing.T) { ctx := context.Background() cluster := &infrav1.NutanixCluster{ Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ Address: "prismcentral.nutanix.com", Port: 9440, - CredentialRef: &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + CredentialRef: &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: "test-credential", Namespace: "test-ns", }, @@ -164,9 +166,9 @@ func TestGetPrismCentralClientForCluster(t *testing.T) { t.Run("GetOrCreate Fails", func(t *testing.T) { ctrl := gomock.NewController(t) - creds := []credentialtypes.Credential{ + creds := []credentialTypes.Credential{ { - Type: credentialtypes.BasicAuthCredentialType, + Type: credentialTypes.BasicAuthCredentialType, Data: []byte(`{"prismCentral":{"username":"user","password":"password"}}`), }, } @@ -175,7 +177,7 @@ func TestGetPrismCentralClientForCluster(t *testing.T) { secret := &corev1.Secret{ Data: map[string][]byte{ - credentialtypes.KeyName: credsMarshal, + credentialTypes.KeyName: credsMarshal, }, } @@ -202,9 +204,9 @@ func TestGetPrismCentralClientForCluster(t *testing.T) { // Create a new client cache with session auth disabled to avoid network calls in tests nutanixclient.NutanixClientCache = prismclientv3.NewClientCache() - creds := []credentialtypes.Credential{ + creds := []credentialTypes.Credential{ { - Type: credentialtypes.BasicAuthCredentialType, + Type: credentialTypes.BasicAuthCredentialType, Data: []byte(`{"prismCentral":{"username":"user","password":"password"}}`), }, } @@ -213,7 +215,7 @@ func TestGetPrismCentralClientForCluster(t *testing.T) { require.NoError(t, err) secret := &corev1.Secret{ Data: map[string][]byte{ - credentialtypes.KeyName: credsMarshal, + credentialTypes.KeyName: credsMarshal, }, } diff --git a/controllers/nutanixcluster_controller_test.go b/controllers/nutanixcluster_controller_test.go index 9df61fa28f..855a4baf70 100644 --- a/controllers/nutanixcluster_controller_test.go +++ b/controllers/nutanixcluster_controller_test.go @@ -22,7 +22,7 @@ import ( "testing" "github.com/golang/mock/gomock" - credentialtypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials" + credentialTypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/onsi/gomega/gstruct" @@ -86,7 +86,8 @@ func TestNutanixClusterReconciler(t *testing.T) { UID: utilruntime.NewUUID(), }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ // Adding port info to override default value (0) Port: 9440, }, @@ -248,7 +249,8 @@ func TestNutanixClusterReconciler(t *testing.T) { Namespace: corev1.NamespaceDefault, }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ // Adding port info to override default value (0) Port: 9440, }, @@ -257,8 +259,8 @@ func TestNutanixClusterReconciler(t *testing.T) { g.Expect(k8sClient.Create(ctx, additionalNtnxCluster)).To(Succeed()) // Add credential ref to the ntnxCluster resource - ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: ntnxSecret.Name, Namespace: ntnxSecret.Namespace, } @@ -280,8 +282,8 @@ func TestNutanixClusterReconciler(t *testing.T) { }) It("should add credentialRef and finalizer if not owned by other cluster", func() { // Add credential ref to the ntnxCluster resource - ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: ntnxSecret.Name, Namespace: ntnxSecret.Namespace, } @@ -307,8 +309,8 @@ func TestNutanixClusterReconciler(t *testing.T) { }) It("does not add another credentialRef if it is already set", func() { // Add credential ref to the ntnxCluster resource - ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: ntnxSecret.Name, Namespace: ntnxSecret.Namespace, } @@ -344,8 +346,8 @@ func TestNutanixClusterReconciler(t *testing.T) { It("allows multiple ownerReferences with different kinds", func() { // Add credential ref to the ntnxCluster resource - ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: ntnxSecret.Name, Namespace: ntnxSecret.Namespace, } @@ -379,8 +381,8 @@ func TestNutanixClusterReconciler(t *testing.T) { }) It("should error if secret does not exist", func() { // Add credential ref to the ntnxCluster resource - ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + ntnxCluster.Spec.PrismCentral.CredentialRef = &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: ntnxSecret.Name, Namespace: ntnxSecret.Namespace, } @@ -412,10 +414,11 @@ func TestNutanixClusterReconciler(t *testing.T) { Namespace: "default", }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ // Adding port info to override default value (0) Port: 9440, - CredentialRef: &credentialtypes.NutanixCredentialReference{ + CredentialRef: &credentialTypes.NutanixCredentialReference{ Name: "test", Namespace: "default", Kind: "Secret", @@ -474,7 +477,8 @@ func TestNutanixClusterReconciler(t *testing.T) { Namespace: "default", }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ // Adding port info to override default value (0) Port: 9440, }, @@ -508,10 +512,11 @@ func TestNutanixClusterReconciler(t *testing.T) { Namespace: "default", }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ // Adding port info to override default value (0) Port: 9440, - CredentialRef: &credentialtypes.NutanixCredentialReference{ + CredentialRef: &credentialTypes.NutanixCredentialReference{ Name: "test", Namespace: "default", Kind: "Secret", @@ -533,13 +538,9 @@ func TestNutanixClusterReconciler(t *testing.T) { }) }) - Context("Delete credentials ref reconcile failed: PrismCentral Info is null", func() { + Context("NutanixCluster creation failed: PrismCentral Info is null", func() { It("Should not return error", func() { ctx := context.Background() - reconciler := &NutanixClusterReconciler{ - Client: k8sClient, - Scheme: runtime.NewScheme(), - } ntnxCluster := &infrav1.NutanixCluster{ ObjectMeta: metav1.ObjectMeta{ @@ -547,20 +548,13 @@ func TestNutanixClusterReconciler(t *testing.T) { Namespace: "default", }, Spec: infrav1.NutanixClusterSpec{ - PrismCentral: nil, + ControlPlaneEndpoint: capiv1.APIEndpoint{}, + PrismCentral: nil, }, } // Create the NutanixCluster object - g.Expect(k8sClient.Create(ctx, ntnxCluster)).To(Succeed()) - defer func() { - err := k8sClient.Delete(ctx, ntnxCluster) - Expect(err).NotTo(HaveOccurred()) - }() - - // Reconile Delete credential ref - err := reconciler.reconcileCredentialRefDelete(ctx, ntnxCluster) - g.Expect(err).NotTo(HaveOccurred()) + g.Expect(k8sClient.Create(ctx, ntnxCluster)).NotTo(Succeed()) }) }) }) @@ -587,9 +581,9 @@ func TestReconcileCredentialRefWithValidCredentialRef(t *testing.T) { nutanixCluster := &infrav1.NutanixCluster{ Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ - CredentialRef: &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ + CredentialRef: &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: "test-credential", Namespace: "test-ns", }, @@ -625,9 +619,9 @@ func TestReconcileCredentialRefWithValidCredentialRefFailedUpdate(t *testing.T) fakeClient := mockctlclient.NewMockClient(mockCtrl) nutanixCluster := &infrav1.NutanixCluster{ Spec: infrav1.NutanixClusterSpec{ - PrismCentral: &credentialtypes.NutanixPrismEndpoint{ - CredentialRef: &credentialtypes.NutanixCredentialReference{ - Kind: credentialtypes.SecretKind, + PrismCentral: &credentialTypes.NutanixPrismEndpoint{ + CredentialRef: &credentialTypes.NutanixCredentialReference{ + Kind: credentialTypes.SecretKind, Name: "test-credential", Namespace: "test-ns", }, diff --git a/templates/cluster-template-clusterclass.yaml b/templates/cluster-template-clusterclass.yaml index d5d4909022..1fd99d1ea6 100644 --- a/templates/cluster-template-clusterclass.yaml +++ b/templates/cluster-template-clusterclass.yaml @@ -699,7 +699,17 @@ metadata: spec: template: spec: + controlPlaneEndpoint: + host: PLACEHOLDER + port: 6443 failureDomains: [] + prismCentral: + address: PLACEHOLDER + credentialRef: + kind: Secret + name: PLACEHOLDER + namespace: default + port: 9440 --- apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 kind: NutanixMachineTemplate diff --git a/templates/clusterclass/nct.yaml b/templates/clusterclass/nct.yaml index 350355cde9..794183bf8f 100644 --- a/templates/clusterclass/nct.yaml +++ b/templates/clusterclass/nct.yaml @@ -5,4 +5,14 @@ metadata: spec: template: spec: + controlPlaneEndpoint: + host: PLACEHOLDER + port: 6443 + prismCentral: + address: PLACEHOLDER + port: 9440 + credentialRef: + name: PLACEHOLDER + kind: Secret + namespace: default failureDomains: [] diff --git a/test/e2e/config/nutanix.yaml b/test/e2e/config/nutanix.yaml index 7e220e87c4..3476b1277c 100644 --- a/test/e2e/config/nutanix.yaml +++ b/test/e2e/config/nutanix.yaml @@ -244,7 +244,7 @@ variables: CONTROL_PLANE_MACHINE_COUNT: 3 WORKER_MACHINE_COUNT: 3 NUTANIX_PRISM_ELEMENT_CLUSTER_NAME: "" - NUTANIX_MACHINE_TEMPLATE_IMAGE_NAME: "ubuntu-2004-kube-v1.29.2.qcow2" + NUTANIX_MACHINE_TEMPLATE_IMAGE_NAME: "ubuntu-2204-kube-v1.29.2.qcow2" NUTANIX_MACHINE_TEMPLATE_IMAGE_NAME_v1_26: "ubuntu-2204-kube-v1.26.13.qcow2" NUTANIX_MACHINE_TEMPLATE_IMAGE_NAME_v1_27: "ubuntu-2204-kube-v1.27.9.qcow2" NUTANIX_MACHINE_TEMPLATE_IMAGE_NAME_v1_28: "ubuntu-2204-kube-v1.28.6.qcow2" diff --git a/test/e2e/nutanix_client_test.go b/test/e2e/nutanix_client_test.go index 4779c8fee1..b8c4c253d0 100644 --- a/test/e2e/nutanix_client_test.go +++ b/test/e2e/nutanix_client_test.go @@ -143,6 +143,18 @@ var _ = Describe("Nutanix client", Label("capx-feature-test", "nutanix-client"), controlplaneEndpointPort, ) + ntnxCreds, err := getNutanixCredentials(*e2eConfig) + Expect(err).ToNot(HaveOccurred()) + + ntnxPort, err := strconv.Atoi(ntnxCreds.Port) + Expect(err).ToNot(HaveOccurred()) + + ntnxCluster.Spec.PrismCentral = &credentialTypes.NutanixPrismEndpoint{ + Address: "example.com", // setting this to generate infrav1.PrismCentralClientCondition to true + Port: int32(ntnxPort), + Insecure: ntnxCreds.Insecure, + } + testHelper.createCapiObject(ctx, createCapiObjectParams{ creator: bootstrapClusterProxy.GetClient(), capiObject: ntnxCluster,