Skip to content

Commit 8d82348

Browse files
committed
fix(vm-disk): refuse a disk smaller than the golden image it clones
CDI checks a clone against its source before it creates the claim. When the golden image's PVC is in Block mode and requests more than the disk, it records CloneValidationFailed and leaves the DataVolume pending for good. The DataVolume spec is immutable, so raising storage on the VMDisk afterwards changes nothing, and the resize hook finds no claim to patch. Every image in vm-default-images is 20Gi on replicated, where CDI picks Block mode for the LINSTOR driver, while the chart defaults storage to 5Gi, so a disk created from an image at the default size never comes up. The chart now refuses such a disk at render time and names both sizes, so the user sets storage to the golden's size before any DataVolume exists. It compares what validateClone compares in CDI v1.66.1, the golden PVC's request against the disk's request raised to the target StorageProfile's minimumSupportedPvcSize. A Filesystem golden is not compared, because CDI sizes that clone from the image inside it or with a filesystem overhead, and neither is a golden whose PVC does not exist yet. An existing disk is exempt, as for the other golden checks. Assisted-by: LLM Signed-off-by: Yan Bondarenko <[email protected]>
1 parent aed7fa8 commit 8d82348

2 files changed

Lines changed: 272 additions & 0 deletions

File tree

‎packages/apps/vm-disk/templates/dv.yaml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@
146146
{{- fail (printf "source.image %q: golden image %q lives in StorageClass %q, and a clone into StorageClass %q of the same provisioner gets no host-assisted fallback from CDI. LINSTOR creates the clone next to the source data, and StorageClass %q does not allow remote volume access from every node, so the clone may never bind. Set storageClass to %q, or ask the platform administrator to set cloneStrategyOverride to copy in the kubevirt-cdi package." .Values.source.image.name $desiredSource.pvc.name $goldenSC $targetSC $targetSC $goldenSC) }}
147147
{{- end }}
148148
{{- end }}
149+
{{- /* CDI v1.66.1 refuses to clone a Block source PVC into a smaller
150+
request before it creates the claim (validateClone in
151+
pkg/controller/datavolume/pvc-clone-controller.go), and the
152+
DataVolume then waits forever. It raises the request to the target
153+
StorageProfile's minimumSupportedPvcSize before comparing. A
154+
Filesystem source is not compared, as CDI sizes that clone from
155+
the image inside it or with a filesystem overhead the chart does
156+
not reproduce. */}}
157+
{{- $goldenSize := dig "spec" "resources" "requests" "storage" "" ($goldenPVC | default dict) | toString }}
158+
{{- if and .Values.storage $goldenSize (eq (dig "spec" "volumeMode" "" ($goldenPVC | default dict) | toString) "Block") }}
159+
{{- $goldenBytes := include "cozy-lib.resources.toFloat" $goldenSize | float64 }}
160+
{{- $diskBytes := include "cozy-lib.resources.toFloat" (.Values.storage | toString) | float64 }}
161+
{{- if and (lt $diskBytes $goldenBytes) $targetSC }}
162+
{{- $minimum := dig "metadata" "annotations" "cdi.kubevirt.io/minimumSupportedPvcSize" "" (lookup "cdi.kubevirt.io/v1beta1" "StorageProfile" "" $targetSC | default dict) | toString }}
163+
{{- $diskBytes = maxf $diskBytes (include "cozy-lib.resources.toFloat" $minimum | float64) }}
164+
{{- end }}
165+
{{- if lt $diskBytes $goldenBytes }}
166+
{{- fail (printf "storage %s is smaller than golden image %q (%s), so CDI refuses to clone it and the disk would stay pending forever. Set storage to %s or more." (.Values.storage | toString) $desiredSource.pvc.name $goldenSize $goldenSize) }}
167+
{{- end }}
168+
{{- end }}
149169
{{- end }}
150170
{{- else if hasKey .Values.source "http" }}
151171
{{- $desiredSource = dict "http" (dict "url" (required "A valid .Values.source.http.url entry required!" .Values.source.http.url)) }}
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
suite: vm-disk refuses a disk smaller than the golden image it clones
2+
release:
3+
name: test-vm-disk
4+
namespace: tenant-test
5+
templates:
6+
- templates/dv.yaml
7+
tests:
8+
- it: refuses a new disk at the chart default size from a larger Block golden
9+
set:
10+
source: {image: {name: ubuntu}}
11+
kubernetesProvider:
12+
scheme: &scheme
13+
"cdi.kubevirt.io/v1beta1/DataVolume":
14+
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: datavolumes}
15+
namespaced: true
16+
"cdi.kubevirt.io/v1beta1/StorageProfile":
17+
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: storageprofiles}
18+
namespaced: false
19+
"v1/Namespace":
20+
gvr: {group: "", version: v1, resource: namespaces}
21+
namespaced: false
22+
"v1/PersistentVolumeClaim":
23+
gvr: {group: "", version: v1, resource: persistentvolumeclaims}
24+
namespaced: true
25+
"storage.k8s.io/v1/StorageClass":
26+
gvr: {group: storage.k8s.io, version: v1, resource: storageclasses}
27+
namespaced: false
28+
objects:
29+
- &namespace
30+
kind: Namespace
31+
apiVersion: v1
32+
metadata: {name: tenant-test}
33+
- &goldenPVC
34+
kind: PersistentVolumeClaim
35+
apiVersion: v1
36+
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
37+
spec:
38+
storageClassName: replicated
39+
volumeMode: Block
40+
resources: {requests: {storage: 20Gi}}
41+
asserts:
42+
- failedTemplate:
43+
errorPattern: 'storage 5Gi is smaller than golden image "vm-default-images-ubuntu" \(20Gi\), so CDI refuses to clone it and the disk would stay pending forever\. Set storage to 20Gi or more\.'
44+
45+
- it: clones into a disk the size of the golden
46+
set:
47+
source: {image: {name: ubuntu}}
48+
storage: 20Gi
49+
kubernetesProvider:
50+
scheme: *scheme
51+
objects:
52+
- *namespace
53+
- *goldenPVC
54+
asserts:
55+
- equal:
56+
path: spec.storage.resources.requests.storage
57+
value: 20Gi
58+
59+
- it: clones into a disk larger than the golden
60+
set:
61+
source: {image: {name: ubuntu}}
62+
storage: 25Gi
63+
kubernetesProvider:
64+
scheme: *scheme
65+
objects:
66+
- *namespace
67+
- *goldenPVC
68+
asserts:
69+
- equal:
70+
path: spec.storage.resources.requests.storage
71+
value: 25Gi
72+
73+
- it: compares bytes, so 21G is smaller than a 20Gi golden
74+
set:
75+
source: {image: {name: ubuntu}}
76+
storage: 21G
77+
kubernetesProvider:
78+
scheme: *scheme
79+
objects:
80+
- *namespace
81+
- *goldenPVC
82+
asserts:
83+
- failedTemplate:
84+
errorPattern: 'storage 21G is smaller than golden image "vm-default-images-ubuntu" \(20Gi\)'
85+
86+
- it: compares bytes, so 20480Mi is the size of a 20Gi golden
87+
set:
88+
source: {image: {name: ubuntu}}
89+
storage: 20480Mi
90+
kubernetesProvider:
91+
scheme: *scheme
92+
objects:
93+
- *namespace
94+
- *goldenPVC
95+
asserts:
96+
- equal:
97+
path: spec.storage.resources.requests.storage
98+
value: 20480Mi
99+
100+
- it: clones when the target StorageProfile's minimum PVC size covers the golden
101+
set:
102+
source: {image: {name: ubuntu}}
103+
kubernetesProvider:
104+
scheme: *scheme
105+
objects:
106+
- *namespace
107+
- *goldenPVC
108+
- kind: StorageProfile
109+
apiVersion: cdi.kubevirt.io/v1beta1
110+
metadata:
111+
name: replicated
112+
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 20Gi}
113+
asserts:
114+
- notFailedTemplate: {}
115+
116+
- it: refuses when the target StorageProfile's minimum PVC size is still below the golden
117+
set:
118+
source: {image: {name: ubuntu}}
119+
kubernetesProvider:
120+
scheme: *scheme
121+
objects:
122+
- *namespace
123+
- *goldenPVC
124+
- kind: StorageProfile
125+
apiVersion: cdi.kubevirt.io/v1beta1
126+
metadata:
127+
name: replicated
128+
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 10Gi}
129+
asserts:
130+
- failedTemplate:
131+
errorPattern: 'storage 5Gi is smaller than golden image "vm-default-images-ubuntu" \(20Gi\)'
132+
133+
- it: reads the minimum PVC size of the default class for an empty storageClass
134+
set:
135+
source: {image: {name: ubuntu}}
136+
storageClass: ""
137+
kubernetesProvider:
138+
scheme: *scheme
139+
objects:
140+
- *namespace
141+
- *goldenPVC
142+
- kind: StorageClass
143+
apiVersion: storage.k8s.io/v1
144+
metadata:
145+
name: replicated
146+
annotations: {storageclass.kubernetes.io/is-default-class: "true"}
147+
provisioner: linstor.csi.linbit.com
148+
- kind: StorageProfile
149+
apiVersion: cdi.kubevirt.io/v1beta1
150+
metadata:
151+
name: replicated
152+
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 20Gi}
153+
asserts:
154+
- notFailedTemplate: {}
155+
156+
- it: does not compare sizes for a Filesystem golden
157+
set:
158+
source: {image: {name: ubuntu}}
159+
kubernetesProvider:
160+
scheme: *scheme
161+
objects:
162+
- *namespace
163+
- kind: PersistentVolumeClaim
164+
apiVersion: v1
165+
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
166+
spec:
167+
storageClassName: replicated
168+
volumeMode: Filesystem
169+
resources: {requests: {storage: 20Gi}}
170+
asserts:
171+
- notFailedTemplate: {}
172+
173+
- it: renders without a cluster to look the golden up in
174+
set:
175+
source: {image: {name: ubuntu}}
176+
asserts:
177+
- equal:
178+
path: spec.storage.resources.requests.storage
179+
value: 5Gi
180+
181+
- it: clones a golden whose PVC does not exist yet
182+
set:
183+
source: {image: {name: ubuntu}}
184+
kubernetesProvider:
185+
scheme: *scheme
186+
objects:
187+
- *namespace
188+
- kind: DataVolume
189+
apiVersion: cdi.kubevirt.io/v1beta1
190+
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
191+
status: {phase: ImportScheduled}
192+
asserts:
193+
- notFailedTemplate: {}
194+
195+
- it: leaves a disk without a size to CDI, which takes the golden's
196+
set:
197+
source: {image: {name: ubuntu}}
198+
storage: null
199+
kubernetesProvider:
200+
scheme: *scheme
201+
objects:
202+
- *namespace
203+
- *goldenPVC
204+
asserts:
205+
- notFailedTemplate: {}
206+
207+
- it: leaves an existing disk alone
208+
set:
209+
source: {image: {name: ubuntu}}
210+
storage: 25Gi
211+
kubernetesProvider:
212+
scheme: *scheme
213+
objects:
214+
- *namespace
215+
- *goldenPVC
216+
- kind: DataVolume
217+
apiVersion: cdi.kubevirt.io/v1beta1
218+
metadata:
219+
name: test-vm-disk
220+
namespace: tenant-test
221+
annotations:
222+
vm-disk.cozystack.io/source: '{"image":{"name":"ubuntu"}}'
223+
vm-disk.cozystack.io/storage-class: replicated
224+
spec:
225+
source: {pvc: {name: vm-default-images-ubuntu, namespace: cozy-public}}
226+
storage: {resources: {requests: {storage: 10Gi}}, storageClassName: replicated}
227+
asserts:
228+
- notFailedTemplate: {}
229+
230+
- it: does not compare sizes for an http source
231+
set:
232+
source: {http: {url: "https://example.com/disk.img"}}
233+
storage: 1Gi
234+
kubernetesProvider:
235+
scheme: *scheme
236+
objects:
237+
- *namespace
238+
- *goldenPVC
239+
asserts:
240+
- notFailedTemplate: {}
241+
242+
- it: does not compare sizes for an upload source
243+
set:
244+
source: {upload: {}}
245+
storage: 1Gi
246+
kubernetesProvider:
247+
scheme: *scheme
248+
objects:
249+
- *namespace
250+
- *goldenPVC
251+
asserts:
252+
- notFailedTemplate: {}

0 commit comments

Comments
 (0)