From a525379144169d4dedd8770e165ded42dae40dd3 Mon Sep 17 00:00:00 2001 From: Yannick Struyf Date: Tue, 5 Dec 2023 14:33:02 +0100 Subject: [PATCH] Add additional check for cluster in validateMachineConfig --- controllers/nutanixmachine_controller.go | 10 +++- controllers/nutanixmachine_controller_test.go | 57 +++++++++++++++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/controllers/nutanixmachine_controller.go b/controllers/nutanixmachine_controller.go index 7a17878dda..3dd2bcadf7 100644 --- a/controllers/nutanixmachine_controller.go +++ b/controllers/nutanixmachine_controller.go @@ -513,8 +513,14 @@ func (r *NutanixMachineReconciler) reconcileNode(rctx *nctx.MachineContext) (rec } func (r *NutanixMachineReconciler) validateMachineConfig(rctx *nctx.MachineContext) error { - if rctx.Machine.Spec.FailureDomain == nil && len(rctx.NutanixMachine.Spec.Subnets) == 0 { - return fmt.Errorf("atleast one subnet is needed to create the VM %s if no failure domain is set", rctx.NutanixMachine.Name) + if rctx.Machine.Spec.FailureDomain == nil { + if len(rctx.NutanixMachine.Spec.Subnets) == 0 { + return fmt.Errorf("atleast one subnet is needed to create the VM %s if no failure domain is set", rctx.NutanixMachine.Name) + } + if (rctx.NutanixMachine.Spec.Cluster.Name == nil || *rctx.NutanixMachine.Spec.Cluster.Name == "") && + (rctx.NutanixMachine.Spec.Cluster.UUID == nil || *rctx.NutanixMachine.Spec.Cluster.UUID == "") { + return fmt.Errorf("cluster name or uuid are required to create the VM %s if no failure domain is set", rctx.NutanixMachine.Name) + } } diskSize := rctx.NutanixMachine.Spec.SystemDiskSize diff --git a/controllers/nutanixmachine_controller_test.go b/controllers/nutanixmachine_controller_test.go index 640a40364f..6fc270e9a0 100644 --- a/controllers/nutanixmachine_controller_test.go +++ b/controllers/nutanixmachine_controller_test.go @@ -55,10 +55,18 @@ func TestNutanixMachineReconciler(t *testing.T) { Scheme: runtime.NewScheme(), } - ntnxMachine = &infrav1.NutanixMachine{ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: "default", - }} + ntnxMachine = &infrav1.NutanixMachine{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "default", + }, + Spec: infrav1.NutanixMachineSpec{ + VCPUsPerSocket: int32(minVCPUsPerSocket), + MemorySize: minMachineMemorySize, + SystemDiskSize: minMachineSystemDiskSize, + VCPUSockets: int32(minVCPUSockets), + }, + } machine = &capiv1.Machine{ObjectMeta: metav1.ObjectMeta{ Name: "test", Namespace: "default", @@ -101,6 +109,47 @@ func TestNutanixMachineReconciler(t *testing.T) { }) g.Expect(err).To(HaveOccurred()) }) + It("should error if no failure domain is present on machine and no cluster name is passed", func() { + ntnxMachine.Spec.Subnets = []infrav1.NutanixResourceIdentifier{ + { + Type: infrav1.NutanixIdentifierName, + Name: &r, + }, + } + err := reconciler.validateMachineConfig(&nctx.MachineContext{ + Context: ctx, + NutanixMachine: ntnxMachine, + Machine: machine, + }) + g.Expect(err).To(HaveOccurred()) + }) + It("returns no error if valid machine config is passed without failure domain", func() { + ntnxMachine.Spec.Subnets = []infrav1.NutanixResourceIdentifier{ + { + Type: infrav1.NutanixIdentifierName, + Name: &r, + }, + } + ntnxMachine.Spec.Cluster = infrav1.NutanixResourceIdentifier{ + Type: infrav1.NutanixIdentifierName, + Name: &r, + } + err := reconciler.validateMachineConfig(&nctx.MachineContext{ + Context: ctx, + NutanixMachine: ntnxMachine, + Machine: machine, + }) + g.Expect(err).ToNot(HaveOccurred()) + }) + It("returns no error if valid machine config is passed with failure domain", func() { + machine.Spec.FailureDomain = &r + err := reconciler.validateMachineConfig(&nctx.MachineContext{ + Context: ctx, + NutanixMachine: ntnxMachine, + Machine: machine, + }) + g.Expect(err).ToNot(HaveOccurred()) + }) }) Context("Gets the subnet and PE UUIDs", func() {