Skip to content

Commit 3d5a4db

Browse files
Add flags to allow customization of CPU shares and reservations
for each cloned VM Signed-off-by: Tommaso <[email protected]>
1 parent 5d33235 commit 3d5a4db

File tree

7 files changed

+70
-0
lines changed

7 files changed

+70
-0
lines changed

apis/v1alpha3/zz_generated.conversion.go

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

apis/v1alpha4/zz_generated.conversion.go

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

apis/v1beta1/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ type VirtualMachineCloneSpec struct {
167167
// virtual machine is cloned.
168168
// +optional
169169
NumCoresPerSocket int32 `json:"numCoresPerSocket,omitempty"`
170+
// CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine.
171+
// Defaults to the eponymous property value in the template from which the
172+
// virtual machine is cloned.
173+
// +optional
174+
CPUReservationMhz int64 `json:"cpuReservationMhz,omitempty"`
175+
// CPUShares are a relative priority to other virtual machines used in case of resource contention.
176+
// Defaults to the eponymous property value in the template from which the
177+
// virtual machine is cloned.
178+
// +optional
179+
CPUShares int32 `json:"cpuShares,omitempty"`
170180
// MemoryMiB is the size of a virtual machine's memory, in MiB.
171181
// Defaults to the eponymous property value in the template from which the
172182
// virtual machine is cloned.

config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,20 @@ spec:
967967
Defaults to LinkedClone, but fails gracefully to FullClone if the source
968968
of the clone operation has no snapshots.
969969
type: string
970+
cpuReservationMhz:
971+
description: |-
972+
CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine.
973+
Defaults to the eponymous property value in the template from which the
974+
virtual machine is cloned.
975+
format: int64
976+
type: integer
977+
cpuShares:
978+
description: |-
979+
CPUShares are a relative priority to other virtual machines used in case of resource contention.
980+
Defaults to the eponymous property value in the template from which the
981+
virtual machine is cloned.
982+
format: int32
983+
type: integer
970984
customVMXKeys:
971985
additionalProperties:
972986
type: string

config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,20 @@ spec:
837837
Defaults to LinkedClone, but fails gracefully to FullClone if the source
838838
of the clone operation has no snapshots.
839839
type: string
840+
cpuReservationMhz:
841+
description: |-
842+
CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine.
843+
Defaults to the eponymous property value in the template from which the
844+
virtual machine is cloned.
845+
format: int64
846+
type: integer
847+
cpuShares:
848+
description: |-
849+
CPUShares are a relative priority to other virtual machines used in case of resource contention.
850+
Defaults to the eponymous property value in the template from which the
851+
virtual machine is cloned.
852+
format: int32
853+
type: integer
840854
customVMXKeys:
841855
additionalProperties:
842856
type: string

config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspherevms.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,20 @@ spec:
10571057
Defaults to LinkedClone, but fails gracefully to FullClone if the source
10581058
of the clone operation has no snapshots.
10591059
type: string
1060+
cpuReservationMhz:
1061+
description: |-
1062+
CPUReservationMhz is the amount of CPU in MHz that is guaranteed available to the virtual machine.
1063+
Defaults to the eponymous property value in the template from which the
1064+
virtual machine is cloned.
1065+
format: int64
1066+
type: integer
1067+
cpuShares:
1068+
description: |-
1069+
CPUShares are a relative priority to other virtual machines used in case of resource contention.
1070+
Defaults to the eponymous property value in the template from which the
1071+
virtual machine is cloned.
1072+
format: int32
1073+
type: integer
10601074
customVMXKeys:
10611075
additionalProperties:
10621076
type: string

pkg/services/govmomi/vcenter/clone.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,20 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by
212212
Snapshot: snapshotRef,
213213
}
214214

215+
// Set CPU reservation and shares if specified
216+
cpuAllocation := types.ResourceAllocationInfo{}
217+
if vmCtx.VSphereVM.Spec.CPUReservationMhz > 0 {
218+
cpuAllocation.Reservation = ptr.To(vmCtx.VSphereVM.Spec.CPUReservationMhz)
219+
}
220+
if vmCtx.VSphereVM.Spec.CPUShares > 0 {
221+
cpuShares := types.SharesInfo{
222+
Shares: vmCtx.VSphereVM.Spec.CPUShares,
223+
Level: types.SharesLevelCustom,
224+
}
225+
cpuAllocation.Shares = ptr.To(cpuShares)
226+
}
227+
spec.Config.CpuAllocation = ptr.To(cpuAllocation)
228+
215229
// For PCI devices, the memory for the VM needs to be reserved
216230
// We can replace this once we have another way of reserving memory option
217231
// exposed via the API types.

0 commit comments

Comments
 (0)