Skip to content

Commit 2751440

Browse files
65278wikkyk
authored andcommitted
v1alpha2: remove ProxmoxMachine.Spec.Target (mostly redundant with AllowedNodes of 1)
1 parent 88142f3 commit 2751440

11 files changed

Lines changed: 42 additions & 47 deletions

api/v1alpha1/conversion.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ func Convert_v1beta1_ObjectMeta_To_v1beta2_ObjectMeta(in *clusterv1beta1.ObjectM
499499
return nil
500500
}
501501

502+
func Convert_v1alpha1_VirtualMachineCloneSpec_To_v1alpha2_VirtualMachineCloneSpec(in *VirtualMachineCloneSpec, out *v1alpha2.VirtualMachineCloneSpec, s conversion.Scope) error {
503+
err := autoConvert_v1alpha1_VirtualMachineCloneSpec_To_v1alpha2_VirtualMachineCloneSpec(in, out, s)
504+
// Target is honored as a single item slice in ConvertTo.
505+
return err
506+
}
507+
502508
// //
503509
// helpers
504510
// //

api/v1alpha1/conversion_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ func spokeProxmoxMachineSpec(in *ProxmoxMachineSpec, c randfill.Continue) {
282282
in.Network = &NetworkSpec{}
283283
}
284284

285+
// Target is converted to AllowedNodes[0] in hub and not restored on down-conversion.
286+
// Normalize by moving Target to AllowedNodes (Target dominates AllowedNodes in v1alpha1).
287+
if in.Target != nil {
288+
in.AllowedNodes = []string{*in.Target}
289+
in.Target = nil
290+
}
291+
285292
// Handle Default Network Device
286293
if in.Network.Default == nil {
287294
in.Network.Default = &NetworkDevice{

api/v1alpha1/proxmoxmachine_conversion.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ func restoreProxmoxMachineSpec(src *ProxmoxMachineSpec, dst *v1alpha2.ProxmoxMac
8787
clusterv1.Convert_int32_To_Pointer_int32(src.NumSockets, ok, restored.NumSockets, &dst.NumSockets)
8888
clusterv1.Convert_int32_To_Pointer_int32(src.MemoryMiB, ok, restored.MemoryMiB, &dst.MemoryMiB)
8989

90+
// Turn ProxmoxMachineSpec.Target into allowedNodes. in v1alpha1, target will
91+
// ignore AllowedNodes, so we can literally overwrite these.
92+
if src.Target != nil {
93+
dst.AllowedNodes = []string{*src.Target}
94+
}
95+
9096
// restore fields that don't exist in v1alpha1
9197
if dst.Network != nil && restored.Network != nil {
9298
dst.Network.Zone = restored.Network.Zone

api/v1alpha1/zz_generated.conversion.go

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha2/proxmoxmachine_types.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ type VirtualMachineCloneSpec struct {
249249
// storage for full clone.
250250
// +optional
251251
Storage *string `json:"storage,omitempty"`
252-
253-
// target node. Only allowed if the original VM is on shared storage.
254-
// +optional
255-
Target *string `json:"target,omitempty"`
256252
}
257253

258254
// TemplateMatchPolicy defines how MatchTags are evaluated against template tags.
@@ -702,8 +698,8 @@ func (r *ProxmoxMachine) GetTemplateMatchPolicy() TemplateMatchPolicy {
702698
return TemplateMatchPolicyExact
703699
}
704700

705-
// GetNode get the Proxmox node used to provision this machine.
706-
func (r *ProxmoxMachine) GetNode() string {
701+
// GetSourceNode gets the Proxmox node used to clone this machine from.
702+
func (r *ProxmoxMachine) GetSourceNode() string {
707703
return ptr.Deref(r.Spec.SourceNode, "")
708704
}
709705

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachines.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,6 @@ spec:
13451345
minItems: 1
13461346
type: array
13471347
x-kubernetes-list-type: set
1348-
target:
1349-
description: target node. Only allowed if the original VM is on shared
1350-
storage.
1351-
type: string
13521348
templateID:
13531349
description: templateID the vm_template vmid used for cloning a new
13541350
VM.

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachinetemplates.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,10 +1221,6 @@ spec:
12211221
minItems: 1
12221222
type: array
12231223
x-kubernetes-list-type: set
1224-
target:
1225-
description: target node. Only allowed if the original VM
1226-
is on shared storage.
1227-
type: string
12281224
templateID:
12291225
description: templateID the vm_template vmid used for cloning
12301226
a new VM.

internal/service/vmservice/vm.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func createVM(ctx context.Context, scope *scope.MachineScope) (proxmox.VMCloneRe
434434
}
435435

436436
options := proxmox.VMCloneRequest{
437-
Node: scope.ProxmoxMachine.GetNode(),
437+
Node: scope.ProxmoxMachine.GetSourceNode(),
438438
NewID: int(vmid),
439439
Name: scope.ProxmoxMachine.GetName(),
440440
}
@@ -459,19 +459,12 @@ func createVM(ctx context.Context, scope *scope.MachineScope) (proxmox.VMCloneRe
459459
if scope.ProxmoxMachine.Spec.Storage != nil {
460460
options.Storage = *scope.ProxmoxMachine.Spec.Storage
461461
}
462-
if scope.ProxmoxMachine.Spec.Target != nil {
463-
options.Target = *scope.ProxmoxMachine.Spec.Target
464-
}
465462

466463
if scope.InfraCluster.ProxmoxCluster.Status.NodeLocations == nil {
467464
scope.InfraCluster.ProxmoxCluster.Status.NodeLocations = new(infrav1.NodeLocations)
468465
}
469466

470-
// if no target was specified but we have a set of nodes defined in the spec, we want to evenly distribute
471-
// the nodes across the cluster.
472-
if scope.ProxmoxMachine.Spec.Target == nil &&
473-
(len(scope.InfraCluster.ProxmoxCluster.Spec.AllowedNodes) > 0 || len(scope.ProxmoxMachine.Spec.AllowedNodes) > 0) {
474-
// select next node as a target
467+
if len(scope.InfraCluster.ProxmoxCluster.Spec.AllowedNodes) > 0 || len(scope.ProxmoxMachine.Spec.AllowedNodes) > 0 {
475468
var err error
476469
options.Target, err = selectNextNode(ctx, scope)
477470
if err != nil {

internal/service/vmservice/vm_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestEnsureVirtualMachine_CreateVM_FullOptions(t *testing.T) {
119119
machineScope.ProxmoxMachine.Spec.Pool = ptr.To("pool")
120120
machineScope.ProxmoxMachine.Spec.SnapName = ptr.To("snap")
121121
machineScope.ProxmoxMachine.Spec.Storage = ptr.To("storage")
122-
machineScope.ProxmoxMachine.Spec.Target = ptr.To("node2")
122+
machineScope.ProxmoxMachine.Spec.AllowedNodes = []string{"node2"}
123123
expectedOptions := proxmox.VMCloneRequest{
124124
Node: "node1",
125125
Name: "test",
@@ -133,6 +133,7 @@ func TestEnsureVirtualMachine_CreateVM_FullOptions(t *testing.T) {
133133
}
134134
response := proxmox.VMCloneResponse{NewID: 123, Task: newTask()}
135135
proxmoxClient.EXPECT().CloneVM(context.Background(), 123, expectedOptions).Return(response, nil).Once()
136+
proxmoxClient.EXPECT().GetReservableMemoryBytes(context.Background(), "node2", int64(100)).Return(^uint64(0), nil).Once()
136137

137138
requeue, err := ensureVirtualMachine(context.Background(), machineScope)
138139
require.NoError(t, err)
@@ -154,13 +155,14 @@ func TestEnsureVirtualMachine_CreateVM_FullOptions_TemplateSelector(t *testing.T
154155
},
155156
},
156157
}
158+
157159
machineScope.ProxmoxMachine.Spec.Description = ptr.To("test vm")
158160
machineScope.ProxmoxMachine.Spec.Format = ptr.To(infrav1.TargetStorageFormatRaw)
159161
machineScope.ProxmoxMachine.Spec.Full = ptr.To(true)
160162
machineScope.ProxmoxMachine.Spec.Pool = ptr.To("pool")
161163
machineScope.ProxmoxMachine.Spec.SnapName = ptr.To("snap")
162164
machineScope.ProxmoxMachine.Spec.Storage = ptr.To("storage")
163-
machineScope.ProxmoxMachine.Spec.Target = ptr.To("node2")
165+
machineScope.ProxmoxMachine.Spec.AllowedNodes = []string{"node1", "node2"}
164166
expectedOptions := proxmox.VMCloneRequest{
165167
Node: "node1",
166168
Name: "test",
@@ -175,10 +177,15 @@ func TestEnsureVirtualMachine_CreateVM_FullOptions_TemplateSelector(t *testing.T
175177

176178
// ResolutionPolicy is not set on the TemplateSelector in this test, so the default
177179
// policy is "exact". The vmservice must therefore pass "exact" to FindVMTemplateByTags.
178-
proxmoxClient.EXPECT().FindVMTemplateByTags(context.Background(), vmTemplateTags, "exact").Return("node1", 123, nil).Once()
180+
proxmoxClient.EXPECT().
181+
FindVMTemplateByTags(context.Background(), vmTemplateTags, string(infrav1.TemplateMatchPolicyExact)).
182+
Return("node1", 123, nil).
183+
Once()
179184

180185
response := proxmox.VMCloneResponse{NewID: 123, Task: newTask()}
181186
proxmoxClient.EXPECT().CloneVM(context.Background(), 123, expectedOptions).Return(response, nil).Once()
187+
proxmoxClient.EXPECT().GetReservableMemoryBytes(context.Background(), "node1", int64(100)).Return(0, nil).Once()
188+
proxmoxClient.EXPECT().GetReservableMemoryBytes(context.Background(), "node2", int64(100)).Return(^uint64(0), nil).Once()
182189

183190
requeue, err := ensureVirtualMachine(context.Background(), machineScope)
184191
require.NoError(t, err)
@@ -207,11 +214,10 @@ func TestEnsureVirtualMachine_CreateVM_FullOptions_TemplateSelector_VMTemplateNo
207214
machineScope.ProxmoxMachine.Spec.Pool = ptr.To("pool")
208215
machineScope.ProxmoxMachine.Spec.SnapName = ptr.To("snap")
209216
machineScope.ProxmoxMachine.Spec.Storage = ptr.To("storage")
210-
machineScope.ProxmoxMachine.Spec.Target = ptr.To("node2")
217+
machineScope.ProxmoxMachine.Spec.AllowedNodes = []string{"node2"}
211218

212-
// As above, no ResolutionPolicy is set on the TemplateSelector, so "exact" is the default
213-
// and must be passed to FindVMTemplateByTags. In this scenario the template lookup fails.
214219
proxmoxClient.EXPECT().FindVMTemplateByTags(context.Background(), vmTemplateTags, "exact").Return("", -1, goproxmox.ErrTemplateNotFound).Once()
220+
proxmoxClient.EXPECT().GetReservableMemoryBytes(context.Background(), "node2", int64(100)).Return(^uint64(0), nil).Once()
215221

216222
_, err := createVM(ctx, machineScope)
217223

0 commit comments

Comments
 (0)