Skip to content

Commit 01911ec

Browse files
authored
Fix the VM name lazy intialize (#61)
* Fix the VM name lazy intialize * Fix tests * Address reviews
1 parent a052fb1 commit 01911ec

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

internal/service/vmservice/find.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var (
3535

3636
// ErrVMNotFound VM is not Found in Proxmox.
3737
ErrVMNotFound = errors.New("vm not found")
38+
39+
// ErrVMNotInitialized VM is not Initialized in Proxmox.
40+
ErrVMNotInitialized = errors.New("vm not initialized")
3841
)
3942

4043
// FindVM returns the Proxmox VM if the vmID is set, otherwise
@@ -50,6 +53,10 @@ func FindVM(ctx context.Context, scope *scope.MachineScope) (*proxmox.VirtualMac
5053
scope.Error(err, "unable to find vm")
5154
return nil, ErrVMNotFound
5255
}
56+
if vm.Name != scope.ProxmoxMachine.GetName() {
57+
scope.Error(err, "vm is not initialized yet")
58+
return nil, ErrVMNotInitialized
59+
}
5360
return vm, nil
5461
}
5562

@@ -72,11 +79,17 @@ func updateVMLocation(ctx context.Context, s *scope.MachineScope) error {
7279

7380
// We are looking for a machine with the ID and check if the name matches.
7481
// Then we have to update the node in the machine and cluster status.
75-
vm, err := s.InfraCluster.ProxmoxClient.FindVMResource(ctx, uint64(vmID))
82+
rsc, err := s.InfraCluster.ProxmoxClient.FindVMResource(ctx, uint64(vmID))
7683
if err != nil {
7784
return err
7885
}
7986

87+
// find the VM, to make sure the vm config is up-to-date.
88+
vm, err := s.InfraCluster.ProxmoxClient.GetVM(ctx, rsc.Node, vmID)
89+
if err != nil {
90+
return errors.Wrapf(err, "unable to find vm with id %d", rsc.VMID)
91+
}
92+
8093
// Requeue if machine doesn't have a name yet.
8194
// It seems that the Proxmox source API does not always provide
8295
// the latest information about the resources in the cluster.

internal/service/vmservice/find_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,31 @@ func TestFindVM_NotCreated(t *testing.T) {
7676
require.ErrorIs(t, err, ErrVMNotCreated)
7777
}
7878

79+
func TestFindVM_NotInitialized(t *testing.T) {
80+
ctx := context.TODO()
81+
machineScope, proxmoxClient, _ := setupReconcilerTest(t)
82+
vm := newRunningVM()
83+
vm.Name = "bar"
84+
machineScope.ProxmoxMachine.Spec.VirtualMachineID = ptr.To(int64(vm.VMID))
85+
machineScope.ProxmoxMachine.Status.ProxmoxNode = ptr.To("node2")
86+
87+
proxmoxClient.EXPECT().GetVM(ctx, "node2", int64(123)).Return(vm, nil).Once()
88+
89+
_, err := FindVM(ctx, machineScope)
90+
require.ErrorIs(t, err, ErrVMNotInitialized)
91+
}
92+
7993
func TestUpdateVMLocation_MissingName(t *testing.T) {
8094
ctx := context.TODO()
8195
machineScope, proxmoxClient, _ := setupReconcilerTest(t)
8296
vm := newRunningVM()
8397
vmr := newVMResource()
8498
vmr.Name = ""
99+
vm.Name = ""
85100
machineScope.ProxmoxMachine.Spec.VirtualMachineID = ptr.To(int64(vm.VMID))
86101

87102
proxmoxClient.EXPECT().FindVMResource(ctx, uint64(123)).Return(vmr, nil).Once()
103+
proxmoxClient.EXPECT().GetVM(ctx, "node1", int64(123)).Return(vm, nil).Once()
88104

89105
require.Error(t, updateVMLocation(ctx, machineScope))
90106
}
@@ -94,10 +110,13 @@ func TestUpdateVMLocation_NameMismatch(t *testing.T) {
94110
machineScope, proxmoxClient, _ := setupReconcilerTest(t)
95111
vm := newRunningVM()
96112
vmr := newVMResource()
97-
vmr.Name = "foo"
113+
name := "foo"
114+
vmr.Name = name
115+
vm.Name = name
98116
machineScope.ProxmoxMachine.Spec.VirtualMachineID = ptr.To(int64(vm.VMID))
99117

100118
proxmoxClient.EXPECT().FindVMResource(ctx, uint64(123)).Return(vmr, nil).Once()
119+
proxmoxClient.EXPECT().GetVM(ctx, "node1", int64(123)).Return(vm, nil).Once()
101120

102121
require.Error(t, updateVMLocation(ctx, machineScope))
103122
require.True(t, machineScope.HasFailed(), "expected failureReason and failureMessage to be set")
@@ -116,6 +135,7 @@ func TestUpdateVMLocation_UpdateNode(t *testing.T) {
116135
}, false)
117136

118137
proxmoxClient.EXPECT().FindVMResource(ctx, uint64(123)).Return(vmr, nil).Once()
138+
proxmoxClient.EXPECT().GetVM(ctx, "node1", int64(123)).Return(vm, nil).Once()
119139

120140
require.NoError(t, updateVMLocation(ctx, machineScope))
121141
require.Equal(t, vmr.Node, *machineScope.ProxmoxMachine.Status.ProxmoxNode)
@@ -137,11 +157,14 @@ func TestUpdateVMLocation_WithoutTaskNameMismatch(t *testing.T) {
137157
machineScope, proxmoxClient, _ := setupReconcilerTest(t)
138158
vm := newRunningVM()
139159
vmr := newVMResource()
140-
vmr.Name = "foo"
160+
name := "foo"
161+
vmr.Name = name
162+
vm.Name = name
141163
machineScope.ProxmoxMachine.Spec.VirtualMachineID = ptr.To(int64(vm.VMID))
142164
machineScope.ProxmoxMachine.Status.TaskRef = nil
143165

144166
proxmoxClient.EXPECT().FindVMResource(ctx, uint64(123)).Return(vmr, nil).Once()
167+
proxmoxClient.EXPECT().GetVM(ctx, "node1", int64(123)).Return(vm, nil).Once()
145168

146169
require.Error(t, updateVMLocation(ctx, machineScope))
147170
require.True(t, machineScope.HasFailed(), "expected failureReason and failureMessage to be set")

internal/service/vmservice/helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func newVMResource() *proxmox.ClusterResource {
236236
func newRunningVM() *proxmox.VirtualMachine {
237237
return &proxmox.VirtualMachine{
238238
VirtualMachineConfig: &proxmox.VirtualMachineConfig{},
239-
Name: "running",
239+
Name: "test",
240240
Node: "node1",
241241
Status: proxmox.StatusVirtualMachineRunning,
242242
VMID: 123,
@@ -258,7 +258,7 @@ func newPausedVM() *proxmox.VirtualMachine {
258258
func newStoppedVM() *proxmox.VirtualMachine {
259259
return &proxmox.VirtualMachine{
260260
VirtualMachineConfig: &proxmox.VirtualMachineConfig{},
261-
Name: "stopped",
261+
Name: "test",
262262
Node: "node1",
263263
Status: proxmox.StatusVirtualMachineStopped,
264264
QMPStatus: proxmox.StatusVirtualMachineStopped,

internal/service/vmservice/vm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ func ensureVirtualMachine(ctx context.Context, machineScope *scope.MachineScope)
107107

108108
// we always want to trigger reconciliation at this point.
109109
return false, err
110+
case errors.Is(err, ErrVMNotInitialized):
111+
return true, err
110112
case !errors.Is(err, ErrVMNotCreated):
111113
return false, err
112114
}

0 commit comments

Comments
 (0)