Skip to content

Commit ab91902

Browse files
Add flags to allow customization of CPU and memory shares, reservations and limits
Signed-off-by: Tommaso <[email protected]>
1 parent 7cac247 commit ab91902

13 files changed

+371
-0
lines changed

apis/v1alpha3/zz_generated.conversion.go

Lines changed: 1 addition & 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: 1 addition & 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/api/resource"
2324
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
2425
)
2526

@@ -167,6 +168,10 @@ type VirtualMachineCloneSpec struct {
167168
// virtual machine is cloned.
168169
// +optional
169170
NumCoresPerSocket int32 `json:"numCoresPerSocket,omitempty"`
171+
// resources is the definition of the VM's cpu and memory
172+
// reservations, limits and shares.
173+
// +optional
174+
Resources VirtualMachineResources `json:"resources,omitempty"`
170175
// MemoryMiB is the size of a virtual machine's memory, in MiB.
171176
// Defaults to the eponymous property value in the template from which the
172177
// virtual machine is cloned.
@@ -211,6 +216,34 @@ type VirtualMachineCloneSpec struct {
211216
DataDisks []VSphereDisk `json:"dataDisks,omitempty"`
212217
}
213218

219+
// VirtualMachineResources is the definition of the VM's cpu and memory
220+
// reservations, limits and shares.
221+
// +kubebuilder:validation:MinProperties=1
222+
type VirtualMachineResources struct {
223+
// requests is the definition of the VM's cpu (in hertz) and memory (in bytes) reservations
224+
// +optional
225+
Requests VirtualMachineResourceSpec `json:"requests,omitempty"`
226+
// limits is the definition of the VM's cpu (in hertz) and memory (in bytes) limits
227+
// +optional
228+
Limits VirtualMachineResourceSpec `json:"limits,omitempty"`
229+
// shares is the definition of the VM's cpu and memory shares
230+
// +optional
231+
Shares VirtualMachineResourceSpec `json:"shares,omitempty"`
232+
}
233+
234+
// VirtualMachineResourceSpec is the numerical definition of memory and cpu quantity for the
235+
// given VM hardware policy.
236+
// +kubebuilder:validation:MinProperties=1
237+
type VirtualMachineResourceSpec struct {
238+
// cpu is the definition of the cpu quantity for the given VM hardware policy
239+
// +optional
240+
Cpu resource.Quantity `json:"cpu,omitempty"` //nolint:revive
241+
242+
// memory is the definition of the memory quantity for the given VM hardware policy
243+
// +optional
244+
Memory resource.Quantity `json:"memory,omitempty"`
245+
}
246+
214247
// VSphereDisk is an additional disk to add to the VM that is not part of the VM OVA template.
215248
type VSphereDisk struct {
216249
// Name is used to identify the disk definition. Name is required and needs to be unique so that it can be used to

apis/v1beta1/zz_generated.deepcopy.go

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

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,63 @@ spec:
14591459
ResourcePool is the name, inventory path, managed object reference or the managed
14601460
object ID in which the virtual machine is created/located.
14611461
type: string
1462+
resources:
1463+
description: |-
1464+
Resources is the definition of the VM's cpu and memory
1465+
reservations, limits and shares.
1466+
properties:
1467+
limits:
1468+
description: Limits is the definition of the VM's cpu (in hertz)
1469+
and memory (in bytes) limits
1470+
properties:
1471+
cpu:
1472+
anyOf:
1473+
- type: integer
1474+
- type: string
1475+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1476+
x-kubernetes-int-or-string: true
1477+
memory:
1478+
anyOf:
1479+
- type: integer
1480+
- type: string
1481+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1482+
x-kubernetes-int-or-string: true
1483+
type: object
1484+
requests:
1485+
description: Requests is the definition of the VM's cpu (in hertz)
1486+
and memory (in bytes) reservations
1487+
properties:
1488+
cpu:
1489+
anyOf:
1490+
- type: integer
1491+
- type: string
1492+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1493+
x-kubernetes-int-or-string: true
1494+
memory:
1495+
anyOf:
1496+
- type: integer
1497+
- type: string
1498+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1499+
x-kubernetes-int-or-string: true
1500+
type: object
1501+
shares:
1502+
description: Shares is the definition of the VM's cpu and memory
1503+
shares
1504+
properties:
1505+
cpu:
1506+
anyOf:
1507+
- type: integer
1508+
- type: string
1509+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1510+
x-kubernetes-int-or-string: true
1511+
memory:
1512+
anyOf:
1513+
- type: integer
1514+
- type: string
1515+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1516+
x-kubernetes-int-or-string: true
1517+
type: object
1518+
type: object
14621519
server:
14631520
description: |-
14641521
Server is the IP address or FQDN of the vSphere server on which

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,63 @@ spec:
13351335
ResourcePool is the name, inventory path, managed object reference or the managed
13361336
object ID in which the virtual machine is created/located.
13371337
type: string
1338+
resources:
1339+
description: |-
1340+
Resources is the definition of the VM's cpu and memory
1341+
reservations, limits and shares.
1342+
properties:
1343+
limits:
1344+
description: Limits is the definition of the VM's cpu
1345+
(in hertz) and memory (in bytes) limits
1346+
properties:
1347+
cpu:
1348+
anyOf:
1349+
- type: integer
1350+
- type: string
1351+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1352+
x-kubernetes-int-or-string: true
1353+
memory:
1354+
anyOf:
1355+
- type: integer
1356+
- type: string
1357+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1358+
x-kubernetes-int-or-string: true
1359+
type: object
1360+
requests:
1361+
description: Requests is the definition of the VM's cpu
1362+
(in hertz) and memory (in bytes) reservations
1363+
properties:
1364+
cpu:
1365+
anyOf:
1366+
- type: integer
1367+
- type: string
1368+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1369+
x-kubernetes-int-or-string: true
1370+
memory:
1371+
anyOf:
1372+
- type: integer
1373+
- type: string
1374+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1375+
x-kubernetes-int-or-string: true
1376+
type: object
1377+
shares:
1378+
description: Shares is the definition of the VM's cpu
1379+
and memory shares
1380+
properties:
1381+
cpu:
1382+
anyOf:
1383+
- type: integer
1384+
- type: string
1385+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1386+
x-kubernetes-int-or-string: true
1387+
memory:
1388+
anyOf:
1389+
- type: integer
1390+
- type: string
1391+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1392+
x-kubernetes-int-or-string: true
1393+
type: object
1394+
type: object
13381395
server:
13391396
description: |-
13401397
Server is the IP address or FQDN of the vSphere server on which

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,63 @@ spec:
15171517
ResourcePool is the name, inventory path, managed object reference or the managed
15181518
object ID in which the virtual machine is created/located.
15191519
type: string
1520+
resources:
1521+
description: |-
1522+
Resources is the definition of the VM's cpu and memory
1523+
reservations, limits and shares.
1524+
properties:
1525+
limits:
1526+
description: Limits is the definition of the VM's cpu (in hertz)
1527+
and memory (in bytes) limits
1528+
properties:
1529+
cpu:
1530+
anyOf:
1531+
- type: integer
1532+
- type: string
1533+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1534+
x-kubernetes-int-or-string: true
1535+
memory:
1536+
anyOf:
1537+
- type: integer
1538+
- type: string
1539+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1540+
x-kubernetes-int-or-string: true
1541+
type: object
1542+
requests:
1543+
description: Requests is the definition of the VM's cpu (in hertz)
1544+
and memory (in bytes) reservations
1545+
properties:
1546+
cpu:
1547+
anyOf:
1548+
- type: integer
1549+
- type: string
1550+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1551+
x-kubernetes-int-or-string: true
1552+
memory:
1553+
anyOf:
1554+
- type: integer
1555+
- type: string
1556+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1557+
x-kubernetes-int-or-string: true
1558+
type: object
1559+
shares:
1560+
description: Shares is the definition of the VM's cpu and memory
1561+
shares
1562+
properties:
1563+
cpu:
1564+
anyOf:
1565+
- type: integer
1566+
- type: string
1567+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1568+
x-kubernetes-int-or-string: true
1569+
memory:
1570+
anyOf:
1571+
- type: integer
1572+
- type: string
1573+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
1574+
x-kubernetes-int-or-string: true
1575+
type: object
1576+
type: object
15201577
server:
15211578
description: |-
15221579
Server is the IP address or FQDN of the vSphere server on which

pkg/services/govmomi/vcenter/clone.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package vcenter
2020
import (
2121
"context"
2222
"fmt"
23+
"math"
2324
"math/rand"
2425
"time"
2526

@@ -212,6 +213,44 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by
212213
Snapshot: snapshotRef,
213214
}
214215

216+
// Set CPU reservations, limits and shares if specified
217+
cpuAllocation := types.ResourceAllocationInfo{}
218+
if !vmCtx.VSphereVM.Spec.Resources.Requests.Cpu.IsZero() {
219+
cpuReservationMhz := int64(math.Ceil(float64(vmCtx.VSphereVM.Spec.Resources.Requests.Cpu.Value()) / float64(1000000)))
220+
cpuAllocation.Reservation = ptr.To(cpuReservationMhz)
221+
}
222+
if !vmCtx.VSphereVM.Spec.Resources.Limits.Cpu.IsZero() {
223+
cpuLimitMhz := int64(math.Ceil(float64(vmCtx.VSphereVM.Spec.Resources.Limits.Cpu.Value()) / float64(1000000)))
224+
cpuAllocation.Limit = ptr.To(cpuLimitMhz)
225+
}
226+
if !vmCtx.VSphereVM.Spec.Resources.Shares.Cpu.IsZero() {
227+
cpuShares := types.SharesInfo{
228+
Shares: int32(vmCtx.VSphereVM.Spec.Resources.Shares.Cpu.Value()),
229+
Level: types.SharesLevelCustom,
230+
}
231+
cpuAllocation.Shares = ptr.To(cpuShares)
232+
}
233+
spec.Config.CpuAllocation = ptr.To(cpuAllocation)
234+
235+
// Set memory reservations, limits and shares if specified
236+
memoryAllocation := types.ResourceAllocationInfo{}
237+
if !vmCtx.VSphereVM.Spec.Resources.Requests.Memory.IsZero() {
238+
memoryReservationMiB := int64(math.Ceil(float64(vmCtx.VSphereVM.Spec.Resources.Requests.Memory.Value()) / float64(1024) / float64(1024)))
239+
memoryAllocation.Reservation = ptr.To(memoryReservationMiB)
240+
}
241+
if !vmCtx.VSphereVM.Spec.Resources.Limits.Memory.IsZero() {
242+
memoryLimitMiB := int64(math.Ceil(float64(vmCtx.VSphereVM.Spec.Resources.Limits.Memory.Value()) / float64(1024) / float64(1024)))
243+
memoryAllocation.Limit = ptr.To(memoryLimitMiB)
244+
}
245+
if !vmCtx.VSphereVM.Spec.Resources.Shares.Memory.IsZero() {
246+
memoryShares := types.SharesInfo{
247+
Shares: int32(vmCtx.VSphereVM.Spec.Resources.Shares.Memory.Value()),
248+
Level: types.SharesLevelCustom,
249+
}
250+
memoryAllocation.Shares = ptr.To(memoryShares)
251+
}
252+
spec.Config.MemoryAllocation = ptr.To(memoryAllocation)
253+
215254
// For PCI devices, the memory for the VM needs to be reserved
216255
// We can replace this once we have another way of reserving memory option
217256
// exposed via the API types.

templates/cluster-template-external-loadbalancer.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ spec:
5656
os: Linux
5757
powerOffMode: trySoft
5858
resourcePool: '${VSPHERE_RESOURCE_POOL}'
59+
resources:
60+
limits:
61+
cpu: "0"
62+
memory: "0"
63+
requests:
64+
cpu: "0"
65+
memory: "0"
66+
shares:
67+
cpu: "0"
68+
memory: "0"
5969
server: '${VSPHERE_SERVER}'
6070
storagePolicyName: '${VSPHERE_STORAGE_POLICY}'
6171
template: '${VSPHERE_TEMPLATE}'
@@ -82,6 +92,16 @@ spec:
8292
os: Linux
8393
powerOffMode: trySoft
8494
resourcePool: '${VSPHERE_RESOURCE_POOL}'
95+
resources:
96+
limits:
97+
cpu: "0"
98+
memory: "0"
99+
requests:
100+
cpu: "0"
101+
memory: "0"
102+
shares:
103+
cpu: "0"
104+
memory: "0"
85105
server: '${VSPHERE_SERVER}'
86106
storagePolicyName: '${VSPHERE_STORAGE_POLICY}'
87107
template: '${VSPHERE_TEMPLATE}'

0 commit comments

Comments
 (0)