diff --git a/apis/v1alpha3/zz_generated.conversion.go b/apis/v1alpha3/zz_generated.conversion.go index 489f69d30f..a7ed448425 100644 --- a/apis/v1alpha3/zz_generated.conversion.go +++ b/apis/v1alpha3/zz_generated.conversion.go @@ -1802,6 +1802,8 @@ func autoConvert_v1beta1_VirtualMachineCloneSpec_To_v1alpha3_VirtualMachineClone } out.NumCPUs = in.NumCPUs out.NumCoresPerSocket = in.NumCoresPerSocket + // WARNING: in.CPUReservationMhz requires manual conversion: does not exist in peer-type + // WARNING: in.CPUShares requires manual conversion: does not exist in peer-type out.MemoryMiB = in.MemoryMiB out.DiskGiB = in.DiskGiB // WARNING: in.AdditionalDisksGiB requires manual conversion: does not exist in peer-type diff --git a/apis/v1alpha4/zz_generated.conversion.go b/apis/v1alpha4/zz_generated.conversion.go index fd9ddcf91e..224902ee1c 100644 --- a/apis/v1alpha4/zz_generated.conversion.go +++ b/apis/v1alpha4/zz_generated.conversion.go @@ -1956,6 +1956,8 @@ func autoConvert_v1beta1_VirtualMachineCloneSpec_To_v1alpha4_VirtualMachineClone } out.NumCPUs = in.NumCPUs out.NumCoresPerSocket = in.NumCoresPerSocket + // WARNING: in.CPUReservationMhz requires manual conversion: does not exist in peer-type + // WARNING: in.CPUShares requires manual conversion: does not exist in peer-type out.MemoryMiB = in.MemoryMiB out.DiskGiB = in.DiskGiB // WARNING: in.AdditionalDisksGiB requires manual conversion: does not exist in peer-type diff --git a/apis/v1beta1/types.go b/apis/v1beta1/types.go index 143b09f149..5856238530 100644 --- a/apis/v1beta1/types.go +++ b/apis/v1beta1/types.go @@ -167,6 +167,16 @@ type VirtualMachineCloneSpec struct { // virtual machine is cloned. // +optional NumCoresPerSocket int32 `json:"numCoresPerSocket,omitempty"` + // CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine. + // Defaults to the eponymous property value in the template from which the + // virtual machine is cloned. + // +optional + CPUReservationMhz int64 `json:"cpuReservationMhz,omitempty"` + // CPUShares are a relative priority to other virtual machines used in case of resource contention. + // Defaults to the eponymous property value in the template from which the + // virtual machine is cloned. + // +optional + CPUShares int32 `json:"cpuShares,omitempty"` // MemoryMiB is the size of a virtual machine's memory, in MiB. // Defaults to the eponymous property value in the template from which the // virtual machine is cloned. diff --git a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml index ee54bd28d0..5cad474a7e 100644 --- a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml +++ b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml @@ -967,6 +967,20 @@ spec: Defaults to LinkedClone, but fails gracefully to FullClone if the source of the clone operation has no snapshots. type: string + cpuReservationMhz: + description: |- + CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine. + Defaults to the eponymous property value in the template from which the + virtual machine is cloned. + format: int64 + type: integer + cpuShares: + description: |- + CPUShares are a relative priority to other virtual machines used in case of resource contention. + Defaults to the eponymous property value in the template from which the + virtual machine is cloned. + format: int32 + type: integer customVMXKeys: additionalProperties: type: string diff --git a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml index dbd1af75a0..e6b0c1fa35 100644 --- a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml +++ b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml @@ -837,6 +837,20 @@ spec: Defaults to LinkedClone, but fails gracefully to FullClone if the source of the clone operation has no snapshots. type: string + cpuReservationMhz: + description: |- + CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine. + Defaults to the eponymous property value in the template from which the + virtual machine is cloned. + format: int64 + type: integer + cpuShares: + description: |- + CPUShares are a relative priority to other virtual machines used in case of resource contention. + Defaults to the eponymous property value in the template from which the + virtual machine is cloned. + format: int32 + type: integer customVMXKeys: additionalProperties: type: string diff --git a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspherevms.yaml b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspherevms.yaml index 6e02b89df7..76c10fe06e 100644 --- a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspherevms.yaml +++ b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspherevms.yaml @@ -1057,6 +1057,20 @@ spec: Defaults to LinkedClone, but fails gracefully to FullClone if the source of the clone operation has no snapshots. type: string + cpuReservationMhz: + description: |- + CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine. + Defaults to the eponymous property value in the template from which the + virtual machine is cloned. + format: int64 + type: integer + cpuShares: + description: |- + CPUShares are a relative priority to other virtual machines used in case of resource contention. + Defaults to the eponymous property value in the template from which the + virtual machine is cloned. + format: int32 + type: integer customVMXKeys: additionalProperties: type: string diff --git a/pkg/services/govmomi/vcenter/clone.go b/pkg/services/govmomi/vcenter/clone.go index 47b11651db..cd3d6c5ae8 100644 --- a/pkg/services/govmomi/vcenter/clone.go +++ b/pkg/services/govmomi/vcenter/clone.go @@ -212,6 +212,20 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by Snapshot: snapshotRef, } + // Set CPU reservation and shares if specified + cpuAllocation := types.ResourceAllocationInfo{} + if vmCtx.VSphereVM.Spec.CPUReservationMhz > 0 { + cpuAllocation.Reservation = ptr.To(vmCtx.VSphereVM.Spec.CPUReservationMhz) + } + if vmCtx.VSphereVM.Spec.CPUShares > 0 { + cpuShares := types.SharesInfo{ + Shares: vmCtx.VSphereVM.Spec.CPUShares, + Level: types.SharesLevelCustom, + } + cpuAllocation.Shares = ptr.To(cpuShares) + } + spec.Config.CpuAllocation = ptr.To(cpuAllocation) + // For PCI devices, the memory for the VM needs to be reserved // We can replace this once we have another way of reserving memory option // exposed via the API types.