From 9cec76143d92355bac9f2df261029ddf880bef4c Mon Sep 17 00:00:00 2001 From: Maxim Snezhkov Date: Sun, 29 Oct 2023 15:15:32 +0700 Subject: [PATCH] Add flag to reserve memory to the virtual machine memory size --- apis/v1beta1/types.go | 7 ++++++ apis/v1beta1/zz_generated.deepcopy.go | 5 ++++ ...ture.cluster.x-k8s.io_vspheremachines.yaml | 7 ++++++ ...ster.x-k8s.io_vspheremachinetemplates.yaml | 7 ++++++ ...structure.cluster.x-k8s.io_vspherevms.yaml | 7 ++++++ pkg/services/govmomi/vcenter/clone.go | 23 ++++++++++++------- 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/apis/v1beta1/types.go b/apis/v1beta1/types.go index 99885028dd..ebaedfaa63 100644 --- a/apis/v1beta1/types.go +++ b/apis/v1beta1/types.go @@ -172,6 +172,13 @@ type VirtualMachineCloneSpec struct { // virtual machine is cloned. // +optional MemoryMiB int64 `json:"memoryMiB,omitempty"` + // MemoryReservationLockedToMax is a flag that indicates whether or not the + // memory resource reservation for this virtual machine will always be + // equal to the virtual machine's memory size. + // Defaults to the eponymous property value in the template from which the + // virtual machine is cloned. + // +optional + MemoryReservationLockedToMax *bool `json:"memoryReservationLockedToMax,omitempty"` // DiskGiB is the size of a virtual machine's disk, in GiB. // Defaults to the eponymous property value in the template from which the // virtual machine is cloned. diff --git a/apis/v1beta1/zz_generated.deepcopy.go b/apis/v1beta1/zz_generated.deepcopy.go index 44d12a65fe..de88e3784b 100644 --- a/apis/v1beta1/zz_generated.deepcopy.go +++ b/apis/v1beta1/zz_generated.deepcopy.go @@ -1297,6 +1297,11 @@ func (in *VirtualMachine) DeepCopy() *VirtualMachine { func (in *VirtualMachineCloneSpec) DeepCopyInto(out *VirtualMachineCloneSpec) { *out = *in in.Network.DeepCopyInto(&out.Network) + if in.MemoryReservationLockedToMax != nil { + in, out := &in.MemoryReservationLockedToMax, &out.MemoryReservationLockedToMax + *out = new(bool) + **out = **in + } if in.AdditionalDisksGiB != nil { in, out := &in.AdditionalDisksGiB, &out.AdditionalDisksGiB *out = make([]int32, len(*in)) 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 47ddaf8329..789ea99b2d 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 @@ -125,6 +125,13 @@ spec: from which the virtual machine is cloned. format: int64 type: integer + memoryReservationLockedToMax: + description: MemoryReservationLockedToMax is a flag that indicates + whether or not the memory resource reservation for this virtual + machine will always be equal to the virtual machine's memory size. + Defaults to the eponymous property value in the template from which + the virtual machine is cloned. + type: boolean network: description: Network is the network configuration for this machine's VM. 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 dd1a667a34..783d1a8959 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 @@ -137,6 +137,13 @@ spec: in the template from which the virtual machine is cloned. format: int64 type: integer + memoryReservationLockedToMax: + description: MemoryReservationLockedToMax is a flag that indicates + whether or not the memory resource reservation for this + virtual machine will always be equal to the virtual machine's + memory size. Defaults to the eponymous property value in + the template from which the virtual machine is cloned. + type: boolean network: description: Network is the network configuration for this machine's VM. 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 6d152dc748..e3bedb7e6d 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 @@ -141,6 +141,13 @@ spec: from which the virtual machine is cloned. format: int64 type: integer + memoryReservationLockedToMax: + description: MemoryReservationLockedToMax is a flag that indicates + whether or not the memory resource reservation for this virtual + machine will always be equal to the virtual machine's memory size. + Defaults to the eponymous property value in the template from which + the virtual machine is cloned. + type: boolean network: description: Network is the network configuration for this machine's VM. diff --git a/pkg/services/govmomi/vcenter/clone.go b/pkg/services/govmomi/vcenter/clone.go index 16e3bc41d9..30e4d36cdb 100644 --- a/pkg/services/govmomi/vcenter/clone.go +++ b/pkg/services/govmomi/vcenter/clone.go @@ -167,6 +167,12 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by memMiB = 2048 } + var memLockedToMax *bool + if vmCtx.VSphereVM.Spec.MemoryReservationLockedToMax != nil { + memLockedToMax = new(bool) + *memLockedToMax = *vmCtx.VSphereVM.Spec.MemoryReservationLockedToMax + } + // Disable the vAppConfig during VM creation to ensure Cloud-Init inside of the guest does not // activate and prefer the OVF datasource over the VMware datasource. vappConfigRemoved := true @@ -176,14 +182,15 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by // Assign the clone's InstanceUUID the value of the Kubernetes Machine // object's UID. This allows lookup of the cloned VM prior to knowing // the VM's UUID. - InstanceUuid: string(vmCtx.VSphereVM.UID), - Flags: newVMFlagInfo(), - DeviceChange: deviceSpecs, - ExtraConfig: extraConfig, - NumCPUs: numCPUs, - NumCoresPerSocket: numCoresPerSocket, - MemoryMB: memMiB, - VAppConfigRemoved: &vappConfigRemoved, + InstanceUuid: string(vmCtx.VSphereVM.UID), + Flags: newVMFlagInfo(), + DeviceChange: deviceSpecs, + ExtraConfig: extraConfig, + NumCPUs: numCPUs, + NumCoresPerSocket: numCoresPerSocket, + MemoryMB: memMiB, + MemoryReservationLockedToMax: memLockedToMax, + VAppConfigRemoved: &vappConfigRemoved, }, Location: types.VirtualMachineRelocateSpec{ DiskMoveType: string(diskMoveType),