Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable for setting the project on cluster resource #395

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions templates/cluster-template-clusterclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,31 @@ spec:
controlPlane: true
enabledIf: '{{if .failureDomains}}true{{end}}'
name: add-failure-domains
- definitions:
- jsonPatches:
- op: add
path: /spec/template/spec/project
valueFrom:
variable: project
selector:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: NutanixMachineTemplate
matchResources:
controlPlane: true
- jsonPatches:
- op: add
path: /spec/template/spec/project
valueFrom:
variable: project
selector:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: NutanixMachineTemplate
matchResources:
machineDeploymentClass:
names:
- nutanix-quick-start-worker
enabledIf: '{{if .project}}true{{end}}'
name: add-project
variables:
- name: sshKey
required: true
Expand Down Expand Up @@ -362,6 +387,22 @@ spec:
type: array
type: object
type: array
- name: project
required: false
schema:
openAPIV3Schema:
description: Prism Central project to deploy the cluster resources into.
properties:
name:
type: string
type:
enum:
- name
- uuid
type: string
uuid:
type: string
type: object
workers:
machineDeployments:
- class: nutanix-quick-start-worker
Expand Down
41 changes: 41 additions & 0 deletions templates/clusterclass/clusterclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ spec:
path: /spec/template/spec/cluster
- op: remove
path: /spec/template/spec/subnet
- name: add-project
enabledIf: "{{if .project}}true{{end}}"
definitions:
- selector:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: NutanixMachineTemplate
matchResources:
controlPlane: true
jsonPatches:
- op: add
path: /spec/template/spec/project
valueFrom:
variable: project
- selector:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: NutanixMachineTemplate
matchResources:
machineDeploymentClass:
names:
- nutanix-quick-start-worker
jsonPatches:
- op: add
path: /spec/template/spec/project
valueFrom:
variable: project
variables:
- name: sshKey
required: true
Expand Down Expand Up @@ -378,3 +403,19 @@ spec:
enum:
- name
- uuid
- name: project
required: false
schema:
openAPIV3Schema:
description: Prism Central project to deploy the cluster resources into.
type: object
properties:
name:
type: string
uuid:
type: string
type:
type: string
enum:
- name
- uuid
48 changes: 48 additions & 0 deletions templates/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
Expand All @@ -38,6 +39,7 @@ const (
var clnt client.Client

func init() {
format.MaxLength = 100000
// Add NutanixCluster and NutanixMachine to the scheme
_ = v1beta1.AddToScheme(scheme.Scheme)
_ = capiv1.AddToScheme(scheme.Scheme)
Expand Down Expand Up @@ -326,4 +328,50 @@ var _ = Describe("Cluster Class Template Patches Test Suite", Ordered, func() {
))
})
})

Describe("patches for project (with name)", func() {
It("should have correct project", func() {
clusterManifest := "testdata/cluster-with-project-name.yaml"
obj, err := getClusterManifest(clusterManifest)
Expect(err).NotTo(HaveOccurred())

err = clnt.Create(context.Background(), obj) // Create the cluster
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
_, err = fetchNutanixCluster(clnt, obj.GetName())
return err
}).Within(time.Minute).Should(Succeed())

Eventually(func() ([]*v1beta1.NutanixMachineTemplate, error) {
return fetchMachineTemplates(clnt, obj.GetName())
}).Within(time.Minute).Should(And(HaveLen(2),
HaveEach(HaveExistingField("Spec.Template.Spec.Project")),
HaveEach(HaveField("Spec.Template.Spec.Project.Type", Equal(v1beta1.NutanixIdentifierName))),
HaveEach(HaveField("Spec.Template.Spec.Project.Name", HaveValue(Equal("fake-project"))))))
})
})

Describe("patches for project (with uuid)", func() {
It("should have correct project", func() {
clusterManifest := "testdata/cluster-with-project-uuid.yaml"
obj, err := getClusterManifest(clusterManifest)
Expect(err).NotTo(HaveOccurred())

err = clnt.Create(context.Background(), obj) // Create the cluster
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
_, err = fetchNutanixCluster(clnt, obj.GetName())
return err
}).Within(time.Minute).Should(Succeed())

Eventually(func() ([]*v1beta1.NutanixMachineTemplate, error) {
return fetchMachineTemplates(clnt, obj.GetName())
}).Within(time.Minute).Should(And(HaveLen(2),
HaveEach(HaveExistingField("Spec.Template.Spec.Project")),
HaveEach(HaveField("Spec.Template.Spec.Project.Type", Equal(v1beta1.NutanixIdentifierUUID))),
HaveEach(HaveField("Spec.Template.Spec.Project.Name", HaveValue(Equal("00000000-0000-0000-0000-000000000001"))))))
})
})
})
56 changes: 56 additions & 0 deletions templates/testdata/cluster-with-project-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
labels:
ccm: nutanix
cluster.x-k8s.io/cluster-name: cluster-with-project-name
name: cluster-with-project-name
spec:
topology:
class: nutanix-quick-start
controlPlane:
metadata: {}
replicas: 1
variables:
- name: sshKey
value: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMe61GqA9gqeX3zDCiwuU8zEDt3ckLnfVm8ZxN7UuFyL user@host
- name: controlPlaneEndpoint
value:
IP: 1.2.3.4
port: 6443
- name: prismCentralEndpoint
value:
address: prismcentral.fake
credentialSecret: nutanix-quick-start-pc-creds
insecure: false
port: 9440
- name: controlPlaneMachineDetails
value:
bootType: legacy
clusterName: fake-cluster
imageName: ubuntu-2204-kube-v1.29.2.qcow2
memorySize: 4Gi
subnetName: fake-subnet
systemDiskSize: 40Gi
vcpuSockets: 2
vcpusPerSocket: 1
- name: workerMachineDetails
value:
bootType: legacy
clusterName: fake-cluster
imageName: ubuntu-2204-kube-v1.29.2.qcow2
memorySize: 4Gi
subnetName: fake-subnet
systemDiskSize: 40Gi
vcpuSockets: 2
vcpusPerSocket: 1
- name: project
value:
type: name
name: fake-project
version: v1.29.2
workers:
machineDeployments:
- class: nutanix-quick-start-worker
name: md-0
replicas: 2
56 changes: 56 additions & 0 deletions templates/testdata/cluster-with-project-uuid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
labels:
ccm: nutanix
cluster.x-k8s.io/cluster-name: cluster-with-project-uuid
name: cluster-with-project-uuid
spec:
topology:
class: nutanix-quick-start
controlPlane:
metadata: {}
replicas: 1
variables:
- name: sshKey
value: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMe61GqA9gqeX3zDCiwuU8zEDt3ckLnfVm8ZxN7UuFyL user@host
- name: controlPlaneEndpoint
value:
IP: 1.2.3.4
port: 6443
- name: prismCentralEndpoint
value:
address: prismcentral.fake
credentialSecret: nutanix-quick-start-pc-creds
insecure: false
port: 9440
- name: controlPlaneMachineDetails
value:
bootType: legacy
clusterName: fake-cluster
imageName: ubuntu-2204-kube-v1.29.2.qcow2
memorySize: 4Gi
subnetName: fake-subnet
systemDiskSize: 40Gi
vcpuSockets: 2
vcpusPerSocket: 1
- name: workerMachineDetails
value:
bootType: legacy
clusterName: fake-cluster
imageName: ubuntu-2204-kube-v1.29.2.qcow2
memorySize: 4Gi
subnetName: fake-subnet
systemDiskSize: 40Gi
vcpuSockets: 2
vcpusPerSocket: 1
- name: project
value:
type: uuid
name: 00000000-0000-0000-0000-000000000001
version: v1.29.2
workers:
machineDeployments:
- class: nutanix-quick-start-worker
name: md-0
replicas: 2
Loading