Skip to content

Commit a056607

Browse files
authored
Merge pull request vmware-tanzu#1392 from dilyar85/bugfix/group-placement-condition
🐛 Set AlreadyPlaced group member condition correctly
2 parents 2ff5ede + 03b5d71 commit a056607

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

controllers/virtualmachinegroup/virtualmachinegroup_controller.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,9 @@ func (r *Reconciler) getVMForPlacement(
626626
return
627627
}
628628

629-
// If both error and vm are nil, then the VM is already placed, or will
630-
// be placed outside the group. Update PlacementReady condition to True.
629+
// Set PlacementReady condition to True if the VM is already placed.
630+
// For VMs with zone label override, VMG will receive reconciliation
631+
// requests once they're actually placed and update the condition then.
631632
if alreadyPlaced {
632633
conditions.Set(memberStatus, &metav1.Condition{
633634
Type: vmopv1.VirtualMachineGroupMemberConditionPlacementReady,
@@ -640,8 +641,6 @@ func (r *Reconciler) getVMForPlacement(
640641
//
641642
Message: alreadyPlacedZone,
642643
})
643-
} else {
644-
conditions.MarkTrue(memberStatus, vmopv1.VirtualMachineGroupMemberConditionPlacementReady)
645644
}
646645
}()
647646

@@ -658,21 +657,6 @@ func (r *Reconciler) getVMForPlacement(
658657
return nil, fmt.Errorf("VM %q is assigned to group %q instead of expected %q", vmName, gn, vmGroup.Name)
659658
}
660659

661-
// Check if VM has uniqueID set first to update the AlreadyPlaced reason.
662-
if vm.Status.UniqueID != "" {
663-
664-
alreadyPlaced = true
665-
alreadyPlacedZone = vm.Status.Zone
666-
667-
pkglog.FromContextOrDefault(ctx).V(4).Info(
668-
"VM has uniqueID, skipping group placement",
669-
"vmName", vmName,
670-
"uniqueID", vm.Status.UniqueID,
671-
"zone", vm.Status.Zone,
672-
)
673-
return nil, nil
674-
}
675-
676660
// Skip if the group already has placement condition ready true for this VM.
677661
// Need to check the UID in case the VM is recreated with the same name and
678662
// without being removed from the group (could have stale placement status).
@@ -688,8 +672,24 @@ func (r *Reconciler) getVMForPlacement(
688672
return nil, nil
689673
}
690674

675+
// Group status doesn't have placement condition ready for this VM (UID).
676+
// Check if the VM is already placed or will be placed outside the group.
677+
691678
memberStatus.UID = vm.GetUID()
692679

680+
if vm.Status.UniqueID != "" {
681+
alreadyPlaced = true
682+
alreadyPlacedZone = vm.Status.Zone
683+
684+
pkglog.FromContextOrDefault(ctx).V(4).Info(
685+
"VM has uniqueID, skipping group placement",
686+
"vmName", vmName,
687+
"uniqueID", vm.Status.UniqueID,
688+
"zone", vm.Status.Zone,
689+
)
690+
return nil, nil
691+
}
692+
693693
// If VM has a zone label, skip group placement to respect zone override.
694694
if zoneName := vm.Labels[corev1.LabelTopologyZone]; zoneName != "" {
695695
pkglog.FromContextOrDefault(ctx).V(4).Info(
@@ -700,6 +700,9 @@ func (r *Reconciler) getVMForPlacement(
700700
return nil, nil
701701
}
702702

703+
// VMG doesn't have a PlacementReady condition true for this VM (UID).
704+
// VM is not already placed, nor has a zone label override.
705+
// Return this VM to get it placed by the group.
703706
return vm, nil
704707
}
705708

controllers/virtualmachinegroup/virtualmachinegroup_controller_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,43 @@ var _ = Describe(
649649
setVMZoneLabel(vm3Key, "zone-c")
650650
})
651651

652-
It("should skip placing VM by group and mark PlacementReady true", func() {
653-
verifyGroupPlacementReadyCondition(vmGroup1Key, 3, "")
654-
verifyGroupPlacementReadyCondition(vmGroup2Key, 1, "")
655-
Expect(groupPlacementCallCount.Load()).To(BeZero(), "VM with zone label should not be placed by group")
652+
It("should skip group placement and set PlacementReady after VM is placed", func() {
653+
By("verifying provider was not called for group placement")
654+
Expect(groupPlacementCallCount.Load()).To(BeZero(),
655+
"VMs with zone label override should not be placed by group")
656+
657+
By("verifying PlacementReady is not set on member status")
658+
Consistently(func(g Gomega) {
659+
group := &vmopv1.VirtualMachineGroup{}
660+
g.Expect(ctx.Client.Get(ctx, vmGroup1Key, group)).To(Succeed())
661+
g.Expect(group.Status.Members).To(HaveLen(3))
662+
for _, ms := range group.Status.Members {
663+
if ms.Kind == virtualMachineKind {
664+
g.Expect(conditions.Get(&ms, vmopv1.VirtualMachineGroupMemberConditionPlacementReady)).To(BeNil())
665+
}
666+
}
667+
}, "3s").Should(Succeed())
668+
669+
By("simulating VMs being placed by setting status.uniqueID")
670+
setVMUniqueID(vm1Key, "vm-unique-id-1", "zone-a")
671+
setVMUniqueID(vm2Key, "vm-unique-id-2", "zone-b")
672+
setVMUniqueID(vm3Key, "vm-unique-id-3", "zone-c")
673+
674+
By("triggering reconciliation of groups")
675+
reconcileVMG(vmGroup1Key)
676+
reconcileVMG(vmGroup2Key)
677+
678+
By("verifying group placement call count remains 0")
679+
Expect(groupPlacementCallCount.Load()).To(BeZero())
680+
681+
By("verifying PlacementReady is now true with AlreadyPlaced reason")
682+
verifyGroupPlacementReadyCondition(vmGroup1Key, 3, "AlreadyPlaced")
683+
verifyGroupPlacementReadyCondition(vmGroup2Key, 1, "AlreadyPlaced")
684+
685+
By("verifying zone is preserved in PlacementReady condition message")
686+
verifyGroupPlacementReadyConditionWithZone(vmGroup1Key, vm1Key, "zone-a")
687+
verifyGroupPlacementReadyConditionWithZone(vmGroup2Key, vm2Key, "zone-b")
688+
verifyGroupPlacementReadyConditionWithZone(vmGroup1Key, vm3Key, "zone-c")
656689
})
657690
})
658691

0 commit comments

Comments
 (0)