Skip to content

Commit

Permalink
removed optional annotation from non-optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakm-ntnx committed May 6, 2024
1 parent fac5b21 commit d2936df
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 59 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) \
Expand All @@ -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)" \
Expand All @@ -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) \
Expand All @@ -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)" \
Expand Down
4 changes: 1 addition & 3 deletions api/v1beta1/nutanixcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions api/v1beta1/nutanixmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ spec:
- address
- port
type: object
required:
- controlPlaneEndpoint
- prismCentral
type: object
status:
description: NutanixClusterStatus defines the observed state of NutanixCluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ spec:
- address
- port
type: object
required:
- controlPlaneEndpoint
- prismCentral
type: object
required:
- spec
Expand Down
24 changes: 13 additions & 11 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ 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"
"github.com/stretchr/testify/assert"
"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"
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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",
},
Expand All @@ -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"}}`),
},
}
Expand All @@ -175,7 +177,7 @@ func TestGetPrismCentralClientForCluster(t *testing.T) {

secret := &corev1.Secret{
Data: map[string][]byte{
credentialtypes.KeyName: credsMarshal,
credentialTypes.KeyName: credsMarshal,
},
}

Expand All @@ -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"}}`),
},
}
Expand All @@ -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,
},
}

Expand Down
72 changes: 33 additions & 39 deletions controllers/nutanixcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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",
Expand All @@ -533,34 +538,23 @@ 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{
Name: "test",
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())
})
})
})
Expand All @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down
10 changes: 10 additions & 0 deletions templates/cluster-template-clusterclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d2936df

Please sign in to comment.