@@ -35,6 +35,15 @@ import (
3535 "github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
3636)
3737
38+ const (
39+ // See following link for a list of available config options:
40+ // https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/qemu/{vmid}/config
41+
42+ optionSockets = "sockets"
43+ optionCores = "cores"
44+ optionMemory = "memory"
45+ )
46+
3847// ReconcileVM makes sure that the VM is in the desired state by:
3948// 1. Creating the VM if it does not exist, then...
4049// 2. Updating the VM with the bootstrap data, such as the cloud-init meta and user data, before...
@@ -98,19 +107,23 @@ func ReconcileVM(ctx context.Context, scope *scope.MachineScope) (infrav1alpha1.
98107 return vm , err
99108 }
100109
101- if ok , err := reconcileIPAddresses (ctx , scope ); err != nil || ! ok {
110+ if requeue , err := reconcileIPAddresses (ctx , scope ); err != nil || requeue {
102111 return vm , err
103112 }
104113
105- if ok , err := reconcileBootstrapData (ctx , scope ); err != nil || ! ok {
114+ if requeue , err := reconcileBootstrapData (ctx , scope ); err != nil || requeue {
106115 return vm , err
107116 }
108117
109118 if err := reconcileDisks (scope ); err != nil {
110119 return vm , err
111120 }
112121
113- if ok , err := reconcilePowerState (scope ); err != nil || ! ok {
122+ if requeue , err := reconcileVirtualMachineConfig (scope ); err != nil || requeue {
123+ return vm , err
124+ }
125+
126+ if requeue , err := reconcilePowerState (scope ); err != nil || requeue {
114127 return vm , err
115128 }
116129
@@ -145,14 +158,47 @@ func reconcileDisks(machineScope *scope.MachineScope) error {
145158 return nil
146159}
147160
148- func reconcilePowerState (machineScope * scope.MachineScope ) (bool , error ) {
161+ func reconcileVirtualMachineConfig (machineScope * scope.MachineScope ) (requeue bool , err error ) {
162+ if machineScope .VirtualMachine .IsRunning () || machineScope .ProxmoxMachine .Status .Ready {
163+ // We only want to do this before the machine was started or is ready
164+ return false , nil
165+ }
166+
167+ vmConfig := machineScope .VirtualMachine .VirtualMachineConfig
168+
169+ var vmOptions []proxmox.VirtualMachineOption
170+ if value := machineScope .ProxmoxMachine .Spec .NumSockets ; value > 0 && vmConfig .Sockets != int (value ) {
171+ vmOptions = append (vmOptions , proxmox.VirtualMachineOption {Name : optionSockets , Value : value })
172+ }
173+ if value := machineScope .ProxmoxMachine .Spec .NumCores ; value > 0 && vmConfig .Cores != int (value ) {
174+ vmOptions = append (vmOptions , proxmox.VirtualMachineOption {Name : optionCores , Value : value })
175+ }
176+ if value := machineScope .ProxmoxMachine .Spec .MemoryMiB ; value > 0 && vmConfig .Memory != int (value ) {
177+ vmOptions = append (vmOptions , proxmox.VirtualMachineOption {Name : optionMemory , Value : value })
178+ }
179+ if len (vmOptions ) == 0 {
180+ return false , nil
181+ }
182+
183+ machineScope .V (4 ).Info ("reconciling virtual machine config" )
184+
185+ task , err := machineScope .InfraCluster .ProxmoxClient .ConfigureVM (machineScope .VirtualMachine , vmOptions ... )
186+ if err != nil {
187+ return false , errors .Wrapf (err , "failed to configure VM %s" , machineScope .Name ())
188+ }
189+
190+ machineScope .ProxmoxMachine .Status .TaskRef = pointer .String (string (task .UPID ))
191+ return true , nil
192+ }
193+
194+ func reconcilePowerState (machineScope * scope.MachineScope ) (requeue bool , err error ) {
149195 ipAddr := machineScope .ProxmoxMachine .Status .IPAddr
150196
151197 if ipAddr == nil || * ipAddr == "" {
152198 machineScope .V (4 ).Info ("ip address not set for machine" )
153199 // machine doesn't have an ip address yet
154200 // needs to reconcile again
155- return false , nil
201+ return true , nil
156202 }
157203
158204 machineScope .V (4 ).Info ("ensuring machine is started" )
@@ -166,10 +212,10 @@ func reconcilePowerState(machineScope *scope.MachineScope) (bool, error) {
166212
167213 if t != nil {
168214 machineScope .ProxmoxMachine .Status .TaskRef = pointer .String (string (t .UPID ))
169- return false , nil
215+ return true , nil
170216 }
171217
172- return true , nil
218+ return false , nil
173219}
174220
175221//nolint:unparam
@@ -180,10 +226,10 @@ func reconcileNetworkStatus(machineScope *scope.MachineScope) error {
180226 return nil
181227}
182228
183- func reconcileIPAddresses (ctx context.Context , machineScope * scope.MachineScope ) (bool , error ) {
229+ func reconcileIPAddresses (ctx context.Context , machineScope * scope.MachineScope ) (requeue bool , err error ) {
184230 if machineScope .ProxmoxMachine .Status .IPAddr != nil {
185231 // skip machine has an IpAddress already.
186- return true , nil
232+ return false , nil
187233 }
188234 machineScope .Logger .V (4 ).Info ("reconciling IPAddresses." )
189235 conditions .MarkFalse (machineScope .ProxmoxMachine , infrav1alpha1 .VMProvisionedCondition , infrav1alpha1 .WaitingForStaticIPAllocationReason , clusterv1 .ConditionSeverityInfo , "" )
@@ -197,7 +243,7 @@ func reconcileIPAddresses(ctx context.Context, machineScope *scope.MachineScope)
197243 if err != nil {
198244 return false , errors .Wrapf (err , "unable to create Ip address claim for machine %s" , machineScope .Name ())
199245 }
200- return false , nil
246+ return true , nil
201247 }
202248 }
203249
@@ -214,17 +260,17 @@ func reconcileIPAddresses(ctx context.Context, machineScope *scope.MachineScope)
214260 machineScope .Logger .V (4 ).Info ("adding virtual machine ip tag." )
215261 t , err := machineScope .VirtualMachine .AddTag (ipTag )
216262 if err != nil {
217- return false , errors .Wrapf (err , "unable to add Ip tag to VirtualMachine %s" , machineScope .VirtualMachine . Name )
263+ return false , errors .Wrapf (err , "unable to add Ip tag to VirtualMachine %s" , machineScope .Name () )
218264 }
219265 machineScope .ProxmoxMachine .Status .TaskRef = pointer .String (string (t .UPID ))
220- return false , nil
266+ return true , nil
221267 }
222268
223269 // update the status.IpAddr.
224270 machineScope .Logger .V (4 ).Info ("updating ProxmoxMachine.status.ipAddr." )
225271 machineScope .ProxmoxMachine .Status .IPAddr = pointer .String (ip )
226272
227- return true , nil
273+ return false , nil
228274}
229275
230276func reconcileMachineAddresses (scope * scope.MachineScope ) error {
@@ -303,8 +349,8 @@ func createVM(scope *scope.MachineScope) (proxmox.VMCloneResponse, error) {
303349 options .Target = selectNextNode (scope )
304350 }
305351
306- templateID := * scope .ProxmoxMachine .Spec . TemplateID
307- res , err := scope .InfraCluster .ProxmoxClient .CloneVM (templateID , options )
352+ templateID := scope .ProxmoxMachine .GetTemplateID ()
353+ res , err := scope .InfraCluster .ProxmoxClient .CloneVM (int ( templateID ) , options )
308354 if err != nil {
309355 return res , err
310356 }
0 commit comments