Skip to content

Commit 9c624d9

Browse files
committed
Don't always backfill VM Status.Zone from label
When either is unset, or they have different values, lookup the VM's zone and assign the zone to the label and status. To avoid a lookup if both the label and status are set to the same value, we'll just trust what is there.
1 parent 3fa235f commit 9c624d9

File tree

2 files changed

+92
-7
lines changed

2 files changed

+92
-7
lines changed

pkg/providers/vsphere/vmlifecycle/update_status.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,18 @@ func reconcileStatusZone(
375375

376376
var errs []error
377377

378-
zoneName := vmCtx.VM.Labels[corev1.LabelTopologyZone]
379-
if zoneName == "" {
378+
zoneLabel := vmCtx.VM.Labels[corev1.LabelTopologyZone]
379+
zoneStatus := vmCtx.VM.Status.Zone
380+
381+
// The label value is protected by the VM validation webhook for non-privileged users,
382+
// but in case the value was accidentally changed by like kube-admin relook the zone.
383+
if zoneLabel == "" || zoneLabel != zoneStatus {
380384
clusterMoRef, err := vcenter.GetResourcePoolOwnerMoRef(
381385
vmCtx, vcVM.Client(), vmCtx.MoVM.ResourcePool.Value)
382386
if err != nil {
383387
errs = append(errs, err)
384388
} else {
385-
zoneName, err = topology.LookupZoneForClusterMoID(
389+
zoneName, err := topology.LookupZoneForClusterMoID(
386390
vmCtx, k8sClient, clusterMoRef.Value)
387391
if err != nil {
388392
errs = append(errs, err)
@@ -391,14 +395,11 @@ func reconcileStatusZone(
391395
vmCtx.VM.Labels = map[string]string{}
392396
}
393397
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
398+
vmCtx.VM.Status.Zone = zoneName
394399
}
395400
}
396401
}
397402

398-
if zoneName != "" {
399-
vmCtx.VM.Status.Zone = zoneName
400-
}
401-
402403
return errs
403404
}
404405

pkg/providers/vsphere/vmlifecycle/update_status_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ var _ = Describe("UpdateStatus", func() {
5454
BeforeEach(func() {
5555
ctx = suite.NewTestContextForVCSim(builder.VCSimTestConfig{})
5656

57+
nsInfo := ctx.CreateWorkloadNamespace()
58+
5759
vm := builder.DummyVirtualMachine()
5860
vm.Name = "update-status-test"
61+
vm.Namespace = nsInfo.Namespace
5962

6063
vmCtx = pkgctx.VirtualMachineContext{
6164
Context: ctx,
@@ -67,6 +70,14 @@ var _ = Describe("UpdateStatus", func() {
6770
vcVM, err = ctx.Finder.VirtualMachine(ctx, "DC0_C0_RP0_VM0")
6871
Expect(err).ToNot(HaveOccurred())
6972

73+
nsRP := ctx.GetResourcePoolForNamespace(nsInfo.Namespace, "", "")
74+
task, err := vcVM.Relocate(ctx, vimtypes.VirtualMachineRelocateSpec{
75+
Folder: ptr.To(nsInfo.Folder.Reference()),
76+
Pool: ptr.To(nsRP.Reference()),
77+
}, vimtypes.VirtualMachineMovePriorityDefaultPriority)
78+
Expect(err).ToNot(HaveOccurred())
79+
Expect(task.Wait(ctx)).To(Succeed())
80+
7081
// Initialize with the expected properties. Tests can overwrite this if needed.
7182
Expect(vcVM.Properties(
7283
ctx,
@@ -2470,6 +2481,79 @@ var _ = Describe("UpdateStatus", func() {
24702481
})
24712482
})
24722483

2484+
Context("Zone", func() {
2485+
var zoneName string
2486+
2487+
BeforeEach(func() {
2488+
delete(vmCtx.VM.Labels, corev1.LabelTopologyZone)
2489+
vmCtx.VM.Status.Zone = ""
2490+
2491+
zoneName = ctx.GetFirstZoneName()
2492+
})
2493+
2494+
assertZones := func() {
2495+
GinkgoHelper()
2496+
Expect(vmCtx.VM.Labels).To(HaveKeyWithValue(corev1.LabelTopologyZone, zoneName))
2497+
Expect(vmCtx.VM.Status.Zone).To(Equal(zoneName))
2498+
}
2499+
2500+
Context("Neither label and status are set", func() {
2501+
It("Sets Zone", assertZones)
2502+
})
2503+
2504+
Context("Label and status are both set", func() {
2505+
BeforeEach(func() {
2506+
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
2507+
vmCtx.VM.Status.Zone = zoneName
2508+
})
2509+
It("Zone still set", assertZones)
2510+
})
2511+
2512+
Context("Label is set but status is not", func() {
2513+
BeforeEach(func() {
2514+
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
2515+
})
2516+
It("Sets Zone", assertZones)
2517+
})
2518+
2519+
Context("Label is not set but status is", func() {
2520+
BeforeEach(func() {
2521+
vmCtx.VM.Status.Zone = zoneName
2522+
})
2523+
It("Sets Zone", assertZones)
2524+
})
2525+
2526+
Context("Label is not set but status is", func() {
2527+
BeforeEach(func() {
2528+
vmCtx.VM.Status.Zone = zoneName
2529+
})
2530+
It("Sets Zone", assertZones)
2531+
})
2532+
2533+
Context("Label is not set but status is", func() {
2534+
BeforeEach(func() {
2535+
vmCtx.VM.Status.Zone = zoneName
2536+
})
2537+
It("Sets Zone", assertZones)
2538+
})
2539+
2540+
Context("Label is set to incorrect value", func() {
2541+
BeforeEach(func() {
2542+
vmCtx.VM.Labels[corev1.LabelTopologyZone] = "bogus"
2543+
vmCtx.VM.Status.Zone = zoneName
2544+
})
2545+
It("Sets Zone", assertZones)
2546+
})
2547+
2548+
Context("Status is set to incorrect value", func() {
2549+
BeforeEach(func() {
2550+
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
2551+
vmCtx.VM.Status.Zone = "bogus"
2552+
})
2553+
It("Sets Zone", assertZones)
2554+
})
2555+
})
2556+
24732557
Context("Controllers", func() {
24742558
When("moVM.Config is nil", func() {
24752559
BeforeEach(func() {

0 commit comments

Comments
 (0)