Skip to content
Open
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
13 changes: 6 additions & 7 deletions pkg/providers/vsphere/vmlifecycle/update_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,16 @@ func reconcileStatusZone(

var errs []error

zoneName := vmCtx.VM.Labels[corev1.LabelTopologyZone]
if zoneName == "" {
zoneLabel := vmCtx.VM.Labels[corev1.LabelTopologyZone]
zoneStatus := vmCtx.VM.Status.Zone

if zoneLabel == "" || zoneLabel != zoneStatus {
clusterMoRef, err := vcenter.GetResourcePoolOwnerMoRef(
vmCtx, vcVM.Client(), vmCtx.MoVM.ResourcePool.Value)
if err != nil {
errs = append(errs, err)
} else {
zoneName, err = topology.LookupZoneForClusterMoID(
zoneName, err := topology.LookupZoneForClusterMoID(
vmCtx, k8sClient, clusterMoRef.Value)
if err != nil {
errs = append(errs, err)
Expand All @@ -377,14 +379,11 @@ func reconcileStatusZone(
vmCtx.VM.Labels = map[string]string{}
}
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
vmCtx.VM.Status.Zone = zoneName
}
}
}

if zoneName != "" {
vmCtx.VM.Status.Zone = zoneName
}

return errs
}

Expand Down
84 changes: 84 additions & 0 deletions pkg/providers/vsphere/vmlifecycle/update_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ var _ = Describe("UpdateStatus", func() {
BeforeEach(func() {
ctx = suite.NewTestContextForVCSim(builder.VCSimTestConfig{})

nsInfo := ctx.CreateWorkloadNamespace()

vm := builder.DummyVirtualMachine()
vm.Name = "update-status-test"
vm.Namespace = nsInfo.Namespace

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

nsRP := ctx.GetResourcePoolForNamespace(nsInfo.Namespace, "", "")
task, err := vcVM.Relocate(ctx, vimtypes.VirtualMachineRelocateSpec{
Folder: ptr.To(nsInfo.Folder.Reference()),
Pool: ptr.To(nsRP.Reference()),
}, vimtypes.VirtualMachineMovePriorityDefaultPriority)
Expect(err).ToNot(HaveOccurred())
Expect(task.Wait(ctx)).To(Succeed())

// Initialize with the expected properties. Tests can overwrite this if needed.
Expect(vcVM.Properties(
ctx,
Expand Down Expand Up @@ -2470,6 +2481,79 @@ var _ = Describe("UpdateStatus", func() {
})
})

Context("Zone", func() {
var zoneName string

BeforeEach(func() {
delete(vmCtx.VM.Labels, corev1.LabelTopologyZone)
vmCtx.VM.Status.Zone = ""

zoneName = ctx.GetFirstZoneName()
})

assertZones := func() {
GinkgoHelper()
Expect(vmCtx.VM.Labels).To(HaveKeyWithValue(corev1.LabelTopologyZone, zoneName))
Expect(vmCtx.VM.Status.Zone).To(Equal(zoneName))
}

Context("Neither label and status are set", func() {
It("Sets Zone", assertZones)
})

Context("Label and status are both set", func() {
BeforeEach(func() {
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
vmCtx.VM.Status.Zone = zoneName
})
It("Zone still set", assertZones)
})

Context("Label is set but status is not", func() {
BeforeEach(func() {
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
})
It("Sets Zone", assertZones)
})

Context("Label is not set but status is", func() {
BeforeEach(func() {
vmCtx.VM.Status.Zone = zoneName
})
It("Sets Zone", assertZones)
})

Context("Label is not set but status is", func() {
BeforeEach(func() {
vmCtx.VM.Status.Zone = zoneName
})
It("Sets Zone", assertZones)
})

Context("Label is not set but status is", func() {
BeforeEach(func() {
vmCtx.VM.Status.Zone = zoneName
})
It("Sets Zone", assertZones)
})

Context("Label is set to incorrect value", func() {
BeforeEach(func() {
vmCtx.VM.Labels[corev1.LabelTopologyZone] = "bogus"
vmCtx.VM.Status.Zone = zoneName
})
It("Sets Zone", assertZones)
})

Context("Status is set to incorrect value", func() {
BeforeEach(func() {
vmCtx.VM.Labels[corev1.LabelTopologyZone] = zoneName
vmCtx.VM.Status.Zone = "bogus"
})
It("Sets Zone", assertZones)
})
})

Context("Controllers", func() {
When("moVM.Config is nil", func() {
BeforeEach(func() {
Expand Down