Skip to content

Commit

Permalink
Add additional check for cluster in validateMachineConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickstruyf3 committed Dec 5, 2023
1 parent 1eff6cb commit a525379
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
10 changes: 8 additions & 2 deletions controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 53 additions & 4 deletions controllers/nutanixmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a525379

Please sign in to comment.