Skip to content

Commit 1211095

Browse files
committed
fix(kubernetes-nodes): refuse a partial resources spec on every pool
The resources.cpu/resources.memory pair check ran only when an instanceType was set, while the field description promised a render-time error for a partial spec on any pool. Without an instancetype a cpu-only pool renders a VM with no memory, which KubeVirt refuses when the Machine is created, and a memory-only pool silently sizes the guest at one vCPU. Run the check on every kubevirt pool; the proxmox branch already requires both. A kubevirt pool with neither an instanceType nor both resources now fails at render with a message naming both, the way vm-instance does; no such pool ever passed admission, so nothing that runs is affected. A float64 core count of one million or more printed in exponent form, which the CRD's integer field cannot take. Print it as an integer; every smaller whole number prints the same bytes, so no template name moves. Closes #4546 Signed-off-by: Marian Koreniuk <[email protected]> Assisted-by: LLM
1 parent aed7fa8 commit 1211095

3 files changed

Lines changed: 222 additions & 12 deletions

File tree

‎packages/apps/kubernetes-nodes/templates/nodegroup.yaml‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,13 @@ spec:
567567
{{- if and .group.resources .group.resources.cpu }}
568568
cpu:
569569
threads: 1
570-
cores: {{ .group.resources.cpu }}
570+
{{- /* An unquoted value arrives as float64, which Go prints in
571+
exponent form from 1e6 upward (1000000 -> 1e+06), and the
572+
CRD field is an integer. %.0f prints the same bytes as the
573+
default formatting for every whole number below that, so
574+
no existing template name moves; a string is emitted as
575+
written, as it always was. */}}
576+
cores: {{ if kindIs "float64" .group.resources.cpu }}{{ printf "%.0f" .group.resources.cpu }}{{ else }}{{ .group.resources.cpu }}{{ end }}
571577
sockets: 1
572578
{{- end }}
573579
devices:
@@ -650,12 +656,27 @@ spec:
650656
valid, resource-sized group. Explicit resources are all-or-nothing: a
651657
partial spec (only one of cpu/memory) would emit one domain override
652658
alongside the instancetype, which KubeVirt rejects, so it fails here with
653-
a clear render-time error instead of a late admission rejection. */}}
659+
a clear render-time error instead of a late admission rejection. The
660+
same rule holds without an instancetype, which is what the field
661+
description promises: cpu alone renders a VM with no memory, which
662+
KubeVirt refuses when the Machine is created, and memory alone sizes the
663+
guest at KubeVirt's default of one vCPU with nothing saying so. The
664+
proxmox branch requires both up front, so this is the kubevirt half of
665+
one rule. Zero is falsy here as everywhere in this chart (see the
666+
resources.cpu check above), so an unquoted zero counts as unset. */}}
654667
{{- $hasCpu := and $group.resources $group.resources.cpu }}
655668
{{- $hasMem := and $group.resources $group.resources.memory }}
656-
{{- if and $group.instanceType (or (and $hasCpu (not $hasMem)) (and $hasMem (not $hasCpu))) }}
669+
{{- if or (and $hasCpu (not $hasMem)) (and $hasMem (not $hasCpu)) }}
657670
{{- fail (printf "nodeGroup %s: set both resources.cpu and resources.memory, or neither" $groupName) }}
658671
{{- end }}
672+
{{- /* With neither sizing source the VM has no memory at all, and KubeVirt
673+
refuses it at admission, long after helm has reported success. No pool
674+
could ever have run in this state, so refusing it here costs nothing
675+
and names the choice. Proxmox has no instancetype and checks the same
676+
thing at the top of this file. */}}
677+
{{- if and (not $isProxmox) (not $group.instanceType) (not (and $hasCpu $hasMem)) }}
678+
{{- fail (printf "nodeGroup %s: set instanceType, or both resources.cpu and resources.memory: with neither the worker VM has no size, and KubeVirt refuses a VirtualMachine that declares no memory" $groupName) }}
679+
{{- end }}
659680
{{- /* Presence, not truth, for both fields below: zero is a quantity the
660681
schema accepts, and a guard keyed on truthiness would wave through
661682
exactly the value it exists to catch. */}}

‎packages/apps/kubernetes-nodes/tests/resources_cpu_test.yaml‎

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,61 @@ tests:
158158
- failedTemplate:
159159
errorPattern: 'nodeGroup md0: resources.cpu \(1.5\) must be a whole number of vCPU cores between 1 and 4294967295'
160160

161-
# An unquoted zero is falsy, so it has always rendered as if the field were
162-
# unset: no domain.cpu, sizing left to the instancetype. The template name
163-
# below is what the chart rendered for it before this guard existed.
164-
- it: keeps rendering an unquoted zero as an unset field
161+
# An unquoted zero is falsy, so it has always counted as an unset field.
162+
# Beside a memory value it is therefore a partial spec, which the chart
163+
# refuses on every pool, and alone it leaves the instancetype to size the
164+
# guest: no domain.cpu, and the matcher stays on the VM.
165+
- it: reads an unquoted zero as an unset field
165166
set:
167+
instanceType: u1.medium
166168
resources:
167169
cpu: 0
168-
memory: 4Gi
170+
# The instancetype the lookup resolves, plus the LIST kinds the
171+
# old-template-preservation loop registers with the fake client, as in
172+
# worker_image_source_test.yaml.
173+
kubernetesProvider:
174+
scheme:
175+
"instancetype.kubevirt.io/v1beta1/VirtualMachineClusterInstancetype":
176+
gvr: {group: instancetype.kubevirt.io, version: v1beta1, resource: virtualmachineclusterinstancetypes}
177+
namespaced: false
178+
"cluster.x-k8s.io/v1beta1/MachineSet":
179+
gvr: {group: cluster.x-k8s.io, version: v1beta1, resource: machinesets}
180+
namespaced: true
181+
"infrastructure.cluster.x-k8s.io/v1alpha1/KubevirtMachineTemplate":
182+
gvr: {group: infrastructure.cluster.x-k8s.io, version: v1alpha1, resource: kubevirtmachinetemplates}
183+
namespaced: true
184+
"cdi.kubevirt.io/v1beta1/DataVolume":
185+
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: datavolumes}
186+
namespaced: true
187+
objects:
188+
- kind: VirtualMachineClusterInstancetype
189+
apiVersion: instancetype.kubevirt.io/v1beta1
190+
metadata: {name: u1.medium}
191+
- kind: MachineSet
192+
apiVersion: cluster.x-k8s.io/v1beta1
193+
metadata: {name: ignored-ms, namespace: tenant-test}
194+
spec: {template: {spec: {infrastructureRef: {kind: Other, name: ignored}}}}
195+
- kind: KubevirtMachineTemplate
196+
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
197+
metadata: {name: ignored-kmt, namespace: tenant-test}
169198
documentSelector:
170199
path: kind
171200
value: KubevirtMachineTemplate
172201
asserts:
173202
- notExists:
174203
path: spec.template.spec.virtualMachineTemplate.spec.template.spec.domain.cpu
175204
- equal:
176-
path: metadata.name
177-
value: kubernetes-myk8s-md0-28d812
205+
path: spec.template.spec.virtualMachineTemplate.spec.instancetype.name
206+
value: u1.medium
207+
208+
- it: refuses an unquoted zero beside memory on a pool with no instancetype
209+
set:
210+
resources:
211+
cpu: 0
212+
memory: 4Gi
213+
asserts:
214+
- failedTemplate:
215+
errorPattern: 'nodeGroup md0: set both resources.cpu and resources.memory, or neither'
178216

179217
# A quoted zero is truthy, so it never took the unset path. It was refused
180218
# before this guard existed as well, by the kubelet CPU reservation check,
@@ -234,10 +272,40 @@ tests:
234272
- failedTemplate:
235273
errorPattern: 'nodeGroup md0: resources.cpu \(1e10\) must be a whole number of vCPU cores between 1 and 4294967295'
236274

275+
# Memory alone is a partial spec on every pool now (resources_partial_test.yaml),
276+
# so the unset field is read on a pool its instancetype sizes.
237277
- it: leaves domain.cpu out when resources.cpu is unset
238278
set:
239-
resources:
240-
memory: 4Gi
279+
instanceType: u1.medium
280+
resources: {}
281+
# The instancetype the lookup resolves, plus the LIST kinds the
282+
# old-template-preservation loop registers with the fake client, as in
283+
# worker_image_source_test.yaml.
284+
kubernetesProvider:
285+
scheme:
286+
"instancetype.kubevirt.io/v1beta1/VirtualMachineClusterInstancetype":
287+
gvr: {group: instancetype.kubevirt.io, version: v1beta1, resource: virtualmachineclusterinstancetypes}
288+
namespaced: false
289+
"cluster.x-k8s.io/v1beta1/MachineSet":
290+
gvr: {group: cluster.x-k8s.io, version: v1beta1, resource: machinesets}
291+
namespaced: true
292+
"infrastructure.cluster.x-k8s.io/v1alpha1/KubevirtMachineTemplate":
293+
gvr: {group: infrastructure.cluster.x-k8s.io, version: v1alpha1, resource: kubevirtmachinetemplates}
294+
namespaced: true
295+
"cdi.kubevirt.io/v1beta1/DataVolume":
296+
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: datavolumes}
297+
namespaced: true
298+
objects:
299+
- kind: VirtualMachineClusterInstancetype
300+
apiVersion: instancetype.kubevirt.io/v1beta1
301+
metadata: {name: u1.medium}
302+
- kind: MachineSet
303+
apiVersion: cluster.x-k8s.io/v1beta1
304+
metadata: {name: ignored-ms, namespace: tenant-test}
305+
spec: {template: {spec: {infrastructureRef: {kind: Other, name: ignored}}}}
306+
- kind: KubevirtMachineTemplate
307+
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
308+
metadata: {name: ignored-kmt, namespace: tenant-test}
241309
documentSelector:
242310
path: kind
243311
value: KubevirtMachineTemplate
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
suite: worker pool resources are all-or-nothing
2+
3+
# resources.cpu and resources.memory size the worker VM together or not at
4+
# all. With an instancetype a lone override is refused by KubeVirt; without one
5+
# cpu alone renders a VM with no memory, which KubeVirt refuses at admission,
6+
# and memory alone sizes the guest at one vCPU with nothing saying so. The
7+
# field description promises a render-time error for a partial spec, and this
8+
# file holds the chart to it on every pool. A pool with neither sizing source
9+
# is refused too, the way vm-instance refuses it: no such pool ever passed
10+
# admission, so nothing that runs today is affected.
11+
12+
templates:
13+
- templates/nodegroup.yaml
14+
release:
15+
name: kubernetes-nodes-myk8s-md0
16+
namespace: tenant-test
17+
set:
18+
cluster: myk8s
19+
_cluster:
20+
cluster-domain: cozy.local
21+
version: "v1.35"
22+
minReplicas: 0
23+
maxReplicas: 3
24+
instanceType: ""
25+
diskSize: 20Gi
26+
storageClass: replicated
27+
tests:
28+
- it: refuses cpu without memory on a pool with no instancetype
29+
set:
30+
resources:
31+
cpu: "2"
32+
asserts:
33+
- failedTemplate:
34+
errorPattern: 'nodeGroup md0: set both resources.cpu and resources.memory, or neither'
35+
36+
- it: refuses memory without cpu on a pool with no instancetype
37+
set:
38+
resources:
39+
memory: 4Gi
40+
asserts:
41+
- failedTemplate:
42+
errorPattern: 'nodeGroup md0: set both resources.cpu and resources.memory, or neither'
43+
44+
# The case the check always caught. Pinned so the two halves cannot drift:
45+
# the partial check runs before the instancetype lookup, so no stub is needed.
46+
- it: refuses cpu without memory on a pool sized by an instancetype
47+
set:
48+
instanceType: u1.medium
49+
resources:
50+
cpu: "2"
51+
asserts:
52+
- failedTemplate:
53+
errorPattern: 'nodeGroup md0: set both resources.cpu and resources.memory, or neither'
54+
55+
- it: refuses memory without cpu on a pool sized by an instancetype
56+
set:
57+
instanceType: u1.medium
58+
resources:
59+
memory: 4Gi
60+
asserts:
61+
- failedTemplate:
62+
errorPattern: 'nodeGroup md0: set both resources.cpu and resources.memory, or neither'
63+
64+
- it: refuses a pool with neither an instancetype nor explicit resources
65+
asserts:
66+
- failedTemplate:
67+
errorPattern: 'nodeGroup md0: set instanceType, or both resources.cpu and resources.memory'
68+
69+
- it: refuses an empty resources block beside an empty instancetype
70+
set:
71+
resources: {}
72+
asserts:
73+
- failedTemplate:
74+
errorPattern: 'nodeGroup md0: set instanceType, or both resources.cpu and resources.memory'
75+
76+
- it: sizes the guest from both fields when both are set
77+
set:
78+
resources:
79+
cpu: "2"
80+
memory: 4Gi
81+
documentSelector:
82+
path: kind
83+
value: KubevirtMachineTemplate
84+
asserts:
85+
- equal:
86+
path: spec.template.spec.virtualMachineTemplate.spec.template.spec.domain.cpu.cores
87+
value: 2
88+
- equal:
89+
path: spec.template.spec.virtualMachineTemplate.spec.template.spec.domain.memory.guest
90+
value: 4Gi
91+
- notExists:
92+
path: spec.template.spec.virtualMachineTemplate.spec.template.spec.instancetype
93+
94+
# An unquoted value is decoded as float64, which Go prints in exponent form
95+
# from one million upward. The CRD field is an integer, so the chart prints
96+
# the whole number; every smaller value prints the same bytes as before.
97+
- it: prints a float64 core count of a million as an integer
98+
set:
99+
resources:
100+
cpu: 1000000
101+
memory: 4Gi
102+
documentSelector:
103+
path: kind
104+
value: KubevirtMachineTemplate
105+
asserts:
106+
- equal:
107+
path: spec.template.spec.virtualMachineTemplate.spec.template.spec.domain.cpu.cores
108+
value: 1000000
109+
110+
- it: prints a whole float64 below a million the way it always did
111+
set:
112+
resources:
113+
cpu: 4.0
114+
memory: 4Gi
115+
documentSelector:
116+
path: kind
117+
value: KubevirtMachineTemplate
118+
asserts:
119+
- equal:
120+
path: spec.template.spec.virtualMachineTemplate.spec.template.spec.domain.cpu.cores
121+
value: 4

0 commit comments

Comments
 (0)