diff --git a/pkg/providers/nutanix/template.go b/pkg/providers/nutanix/template.go index cd934576d4ce..48b53cde67ae 100644 --- a/pkg/providers/nutanix/template.go +++ b/pkg/providers/nutanix/template.go @@ -116,7 +116,10 @@ func (ntb *TemplateBuilder) GenerateEKSASpecSecret(clusterSpec *cluster.Spec, bu // EKSASecretName returns the name of the secret containing the credentials for the nutanix prism central and is used by the // EKS-Anywhere controller. func EKSASecretName(spec *cluster.Spec) string { - return spec.NutanixDatacenter.Spec.CredentialRef.Name + if spec.NutanixDatacenter.Spec.CredentialRef != nil { + return spec.NutanixDatacenter.Spec.CredentialRef.Name + } + return constants.NutanixCredentialsName } func (ntb *TemplateBuilder) generateSpecSecret(secretName string, creds credentials.BasicAuthCredential, buildOptions ...providers.BuildMapOption) ([]byte, error) { diff --git a/pkg/providers/nutanix/template_test.go b/pkg/providers/nutanix/template_test.go index 528226008ea0..5786b113969b 100644 --- a/pkg/providers/nutanix/template_test.go +++ b/pkg/providers/nutanix/template_test.go @@ -98,6 +98,25 @@ func TestNewNutanixTemplateBuilderGenerateSpecSecretFailure(t *testing.T) { assert.Error(t, err) } +func TestNewNutanixTemplateBuilderGenerateSpecSecretDefaultCreds(t *testing.T) { + dcConf, machineConf, workerConfs := minimalNutanixConfigSpec(t) + t.Setenv(constants.NutanixUsernameKey, "admin") + t.Setenv(constants.NutanixPasswordKey, "password") + creds := GetCredsFromEnv() + builder := NewNutanixTemplateBuilder(&dcConf.Spec, &machineConf.Spec, &machineConf.Spec, workerConfs, creds, time.Now) + assert.NotNil(t, builder) + + buildSpec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-no-credentialref.yaml") + + secretSpec, err := builder.GenerateCAPISpecSecret(buildSpec) + assert.NoError(t, err) + assert.NotNil(t, secretSpec) + + secretSpec, err = builder.GenerateEKSASpecSecret(buildSpec) + assert.NoError(t, err) + assert.NotNil(t, secretSpec) +} + func TestNutanixTemplateBuilderGenerateCAPISpecForCreateWithAutoscalingConfiguration(t *testing.T) { dcConf, machineConf, workerConfs := minimalNutanixConfigSpec(t) diff --git a/pkg/providers/nutanix/testdata/eksa-cluster-no-credentialref.yaml b/pkg/providers/nutanix/testdata/eksa-cluster-no-credentialref.yaml new file mode 100644 index 000000000000..964237eab26a --- /dev/null +++ b/pkg/providers/nutanix/testdata/eksa-cluster-no-credentialref.yaml @@ -0,0 +1,72 @@ +apiVersion: anywhere.eks.amazonaws.com/v1alpha1 +kind: Cluster +metadata: + name: eksa-unit-test + namespace: default +spec: + kubernetesVersion: "1.19" + controlPlaneConfiguration: + name: eksa-unit-test + count: 3 + endpoint: + host: test-ip + machineGroupRef: + name: eksa-unit-test + kind: NutanixMachineConfig + workerNodeGroupConfigurations: + - count: 4 + name: eksa-unit-test + machineGroupRef: + name: eksa-unit-test + kind: NutanixMachineConfig + externalEtcdConfiguration: + name: eksa-unit-test + count: 3 + machineGroupRef: + name: eksa-unit-test + kind: NutanixMachineConfig + datacenterRef: + kind: NutanixDatacenterConfig + name: eksa-unit-test + clusterNetwork: + cni: "cilium" + pods: + cidrBlocks: + - 192.168.0.0/16 + services: + cidrBlocks: + - 10.96.0.0/12 +--- +apiVersion: anywhere.eks.amazonaws.com/v1alpha1 +kind: NutanixDatacenterConfig +metadata: + name: eksa-unit-test + namespace: default +spec: + endpoint: "prism.nutanix.com" + port: 9440 +--- +apiVersion: anywhere.eks.amazonaws.com/v1alpha1 +kind: NutanixMachineConfig +metadata: + name: eksa-unit-test + namespace: default +spec: + vcpusPerSocket: 1 + vcpuSockets: 4 + memorySize: 8Gi + image: + type: "name" + name: "prism-image" + cluster: + type: "name" + name: "prism-cluster" + subnet: + type: "name" + name: "prism-subnet" + systemDiskSize: 40Gi + osFamily: "ubuntu" + users: + - name: "mySshUsername" + sshAuthorizedKeys: + - "mySshAuthorizedKey"