Skip to content

Commit

Permalink
Propogate project to the NutanixMachineTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderboltsid committed Mar 15, 2023
1 parent fe6b1f9 commit 2c4ab46
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/providers/nutanix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ type Client interface {
ListImage(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.ImageListIntentResponse, error)
GetCluster(ctx context.Context, uuid string) (*v3.ClusterIntentResponse, error)
ListCluster(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.ClusterListIntentResponse, error)
GetProject(ctx context.Context, uuid string) (*v3.Project, error)
ListProject(ctx context.Context, getEntitiesRequest *v3.DSMetadata) (*v3.ProjectListResponse, error)
GetCurrentLoggedInUser(ctx context.Context) (*v3.UserIntentResponse, error)
}
10 changes: 10 additions & 0 deletions pkg/providers/nutanix/config/cp-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ spec:
- type: uuid
uuid: "{{.subnetUUID}}"
{{ end }}
{{- if .projectIDType}}
project:
{{- if (eq .projectIDType "name") }}
type: name
name: "{{.projectName}}"
{{- else if (eq .projectIDType "uuid") }}
type: uuid
uuid: "{{.projectUUID}}"
{{ end }}
{{ end }}
---
{{- if .registryAuth }}
apiVersion: v1
Expand Down
10 changes: 10 additions & 0 deletions pkg/providers/nutanix/config/md-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ spec:
- type: uuid
uuid: "{{.subnetUUID}}"
{{ end }}
{{- if .projectIDType}}
project:
{{- if (eq .projectIDType "name") }}
type: name
name: "{{.projectName}}"
{{- else if (eq .projectIDType "uuid") }}
type: uuid
uuid: "{{.projectUUID}}"
{{ end }}
{{ end }}
---
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
Expand Down
30 changes: 30 additions & 0 deletions pkg/providers/nutanix/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/providers/nutanix/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ func buildTemplateMapCP(
values["etcdSshUsername"] = etcdMachineSpec.Users[0].Name
}

if controlPlaneMachineSpec.Project != nil {
values["projectIDType"] = controlPlaneMachineSpec.Project.Type
values["projectName"] = controlPlaneMachineSpec.Project.Name
values["projectUUID"] = controlPlaneMachineSpec.Project.UUID
}

return values, nil
}

Expand Down Expand Up @@ -277,6 +283,12 @@ func buildTemplateMapMD(clusterSpec *cluster.Spec, workerNodeGroupMachineSpec v1
}
}

if workerNodeGroupMachineSpec.Project != nil {
values["projectIDType"] = workerNodeGroupMachineSpec.Project.Type
values["projectName"] = workerNodeGroupMachineSpec.Project.Name
values["projectUUID"] = workerNodeGroupMachineSpec.Project.UUID
}

return values, nil
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/providers/nutanix/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var nutanixDatacenterConfigSpec string
//go:embed testdata/machineConfig.yaml
var nutanixMachineConfigSpec string

//go:embed testdata/machineConfig_project.yaml
var nutanixMachineConfigSpecWithProject string

func fakemarshal(v interface{}) ([]byte, error) {
return []byte{}, errors.New("marshalling failed")
}
Expand Down Expand Up @@ -200,6 +203,46 @@ func TestNewNutanixTemplateBuilderRegistryMirrorConfigNoRegistryCredsSet(t *test
assert.Error(t, err)
}

func TestNewNutanixTemplateBuilderProject(t *testing.T) {
t.Setenv(constants.EksaNutanixUsernameKey, "admin")
t.Setenv(constants.EksaNutanixPasswordKey, "password")
creds := GetCredsFromEnv()

dcConf, _, _ := minimalNutanixConfigSpec(t)
machineConf := &anywherev1.NutanixMachineConfig{}
err := yaml.Unmarshal([]byte(nutanixMachineConfigSpecWithProject), machineConf)
require.NoError(t, err)

workerConfs := map[string]anywherev1.NutanixMachineConfigSpec{
"eksa-unit-test": machineConf.Spec,
}
builder := NewNutanixTemplateBuilder(&dcConf.Spec, &machineConf.Spec, &machineConf.Spec, workerConfs, creds, time.Now)
assert.NotNil(t, builder)

buildSpec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-project.yaml")
cpSpec, err := builder.GenerateCAPISpecControlPlane(buildSpec)
assert.NoError(t, err)
assert.NotNil(t, cpSpec)

expectedControlPlaneSpec, err := os.ReadFile("testdata/expected_results_project.yaml")
require.NoError(t, err)
assert.Equal(t, expectedControlPlaneSpec, cpSpec)

workloadTemplateNames := map[string]string{
"eksa-unit-test": "eksa-unit-test",
}
kubeadmconfigTemplateNames := map[string]string{
"eksa-unit-test": "eksa-unit-test",
}
workerSpec, err := builder.GenerateCAPISpecWorkers(buildSpec, workloadTemplateNames, kubeadmconfigTemplateNames)
assert.NoError(t, err)
assert.NotNil(t, workerSpec)

expectedWorkersSpec, err := os.ReadFile("testdata/expected_results_project_md.yaml")
require.NoError(t, err)
assert.Equal(t, expectedWorkersSpec, workerSpec)
}

func minimalNutanixConfigSpec(t *testing.T) (*anywherev1.NutanixDatacenterConfig, *anywherev1.NutanixMachineConfig, map[string]anywherev1.NutanixMachineConfigSpec) {
dcConf := &anywherev1.NutanixDatacenterConfig{}
err := yaml.Unmarshal([]byte(nutanixDatacenterConfigSpec), dcConf)
Expand Down
79 changes: 79 additions & 0 deletions pkg/providers/nutanix/testdata/eksa-cluster-project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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
credentialRef:
kind: Secret
name: "nutanix-credentials"
---
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"
project:
type: "name"
name: "prism-project"
systemDiskSize: 40Gi
osFamily: "ubuntu"
users:
- name: "mySshUsername"
sshAuthorizedKeys:
- "mySshAuthorizedKey"
---
Loading

0 comments on commit 2c4ab46

Please sign in to comment.