Skip to content

Commit 7b57f6f

Browse files
committed
fix(vm-disk): fail on storageClass/source edit of existing disk
Editing a VMDisk's storageClass or source after its DataVolume already exists was silently ignored: dv.yaml reused the existing DataVolume.spec verbatim (DataVolume.spec is immutable in CDI), so the edit rendered as a no-op while the HelmRelease reported success and the CR diverged from the cluster. Build the desired source into a shared variable and, when a DataVolume already exists, fail with a clear message if the requested storageClass or source differs from the immutable existing spec, mirroring the explicit size handling in pvc-resize-hook.yaml. Storage growth is unaffected and still handled by the resize hook. Also mark source as immutable in values.yaml so the generated schema carries the same x-kubernetes-validations transition rule already present on storageClass, and regenerate types/schema/CRD. Add helm-unittest coverage for both the immutability guard (via a mocked existing DataVolume) and the initial-creation source rendering. Fixes #2985 Signed-off-by: Alexey Artamonov <[email protected]>
1 parent eef95f4 commit 7b57f6f

7 files changed

Lines changed: 208 additions & 25 deletions

File tree

‎api/apps/v1alpha1/vmdisk/types.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.

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
{{- $existingDV := lookup "cdi.kubevirt.io/v1beta1" "DataVolume" .Release.Namespace .Release.Name }}
2+
{{- /* Build the desired source spec from .Values so it can be both rendered and compared. */}}
3+
{{- $desiredSource := dict }}
4+
{{- if .Values.source }}
5+
{{- if gt (len .Values.source) 1 }}
6+
{{- fail "Exactly one type of source is expected!" }}
7+
{{- end }}
8+
{{- if hasKey .Values.source "image" }}
9+
{{- $desiredSource = dict "pvc" (dict "name" (printf "vm-default-images-%s" (required "A valid .Values.source.image.name entry required!" .Values.source.image.name)) "namespace" "cozy-public") }}
10+
{{- else if hasKey .Values.source "http" }}
11+
{{- $desiredSource = dict "http" (dict "url" (required "A valid .Values.source.http.url entry required!" .Values.source.http.url)) }}
12+
{{- else if hasKey .Values.source "upload" }}
13+
{{- $desiredSource = dict "upload" dict }}
14+
{{- else if hasKey .Values.source "disk" }}
15+
{{- $desiredSource = dict "pvc" (dict "name" (printf "vm-disk-%s" (required "A valid .Values.source.disk.name entry required!" .Values.source.disk.name)) "namespace" .Release.Namespace) }}
16+
{{- end }}
17+
{{- else }}
18+
{{- $desiredSource = dict "blank" dict }}
19+
{{- end }}
20+
{{- /*
21+
DataVolume.spec is immutable in CDI. If the existing DataVolume was created with a
22+
different storageClass or source, silently reusing the old spec would report success
23+
while the cluster keeps diverging from the CR. Fail loudly instead, mirroring the
24+
explicit size handling in pvc-resize-hook.yaml. Size growth is intentionally excluded —
25+
that is handled by the resize hook.
26+
*/}}
27+
{{- if $existingDV }}
28+
{{- $existingSC := $existingDV.spec.storage.storageClassName | toString }}
29+
{{- if and .Values.storageClass (ne ($existingSC) (.Values.storageClass | toString)) }}
30+
{{- fail (printf "VMDisk storageClass is immutable after creation (current: %q, requested: %q). Delete and recreate the disk to change it." $existingSC .Values.storageClass) }}
31+
{{- end }}
32+
{{- if ne (toYaml $existingDV.spec.source) (toYaml $desiredSource) }}
33+
{{- fail (printf "VMDisk source is immutable after creation (current: %s, requested: %s). Delete and recreate the disk to change it." (toJson $existingDV.spec.source) (toJson $desiredSource)) }}
34+
{{- end }}
35+
{{- end }}
236
apiVersion: cdi.kubevirt.io/v1beta1
337
kind: DataVolume
438
metadata:
@@ -13,31 +47,10 @@ metadata:
1347
spec:
1448
{{- if $existingDV }}
1549
{{- toYaml $existingDV.spec | nindent 2 }}
16-
{{- else }}
17-
contentType: kubevirt
18-
{{- if .Values.source }}
19-
{{- if gt (len .Values.source) 1 }}
20-
{{- fail "Exactly one type of source is expected!" }}
21-
{{- end }}
22-
source:
23-
{{- if hasKey .Values.source "image" }}
24-
pvc:
25-
name: vm-default-images-{{ required "A valid .Values.source.image.name entry required!" .Values.source.image.name }}
26-
namespace: cozy-public
27-
{{- else if hasKey .Values.source "http" }}
28-
http:
29-
url: {{ required "A valid .Values.source.http.url entry required!" .Values.source.http.url }}
30-
{{- else if hasKey .Values.source "upload" }}
31-
upload: {}
32-
{{- else if hasKey .Values.source "disk" }}
33-
pvc:
34-
name: vm-disk-{{ required "A valid .Values.source.disk.name entry required!" .Values.source.disk.name }}
35-
namespace: {{ $.Release.Namespace }}
36-
{{- end }}
3750
{{- else }}
51+
contentType: kubevirt
3852
source:
39-
blank: {}
40-
{{- end }}
53+
{{- toYaml $desiredSource | nindent 4 }}
4154
storage:
4255
resources:
4356
requests:
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
suite: vm-disk DataVolume immutable fields (storageClass/source)
2+
3+
# Regression for #2985: editing storageClass/source on an existing VMDisk used
4+
# to be silently ignored — dv.yaml reused the existing DataVolume.spec verbatim
5+
# and the edit rendered as a no-op while the HelmRelease reported success. The
6+
# template now fails loudly when the requested storageClass/source differs from
7+
# the existing (immutable) DataVolume. Mock the existing DataVolume via the
8+
# kubernetes provider so the lookup in dv.yaml resolves without a live cluster.
9+
release:
10+
name: test-vm-disk
11+
namespace: tenant-test
12+
templates:
13+
- templates/dv.yaml
14+
kubernetesProvider:
15+
scheme:
16+
"cdi.kubevirt.io/v1beta1/DataVolume":
17+
gvr:
18+
group: cdi.kubevirt.io
19+
version: v1beta1
20+
resource: datavolumes
21+
namespaced: true
22+
objects:
23+
- kind: DataVolume
24+
apiVersion: cdi.kubevirt.io/v1beta1
25+
metadata:
26+
name: test-vm-disk
27+
namespace: tenant-test
28+
spec:
29+
contentType: kubevirt
30+
source:
31+
http:
32+
url: https://example.com/original.qcow2
33+
storage:
34+
resources:
35+
requests:
36+
storage: 5Gi
37+
storageClassName: replicated
38+
tests:
39+
- it: rejects a storageClass change on an existing DataVolume
40+
set:
41+
storageClass: other
42+
source:
43+
http:
44+
url: https://example.com/original.qcow2
45+
asserts:
46+
- failedTemplate:
47+
errorPattern: 'VMDisk storageClass is immutable after creation'
48+
49+
- it: rejects a source change on an existing DataVolume
50+
set:
51+
storageClass: replicated
52+
source:
53+
http:
54+
url: https://example.com/changed.qcow2
55+
asserts:
56+
- failedTemplate:
57+
errorPattern: 'VMDisk source is immutable after creation'
58+
59+
- it: reuses the existing spec when storageClass and source are unchanged
60+
set:
61+
storageClass: replicated
62+
source:
63+
http:
64+
url: https://example.com/original.qcow2
65+
asserts:
66+
- hasDocuments:
67+
count: 1
68+
- equal:
69+
path: spec.storage.storageClassName
70+
value: replicated
71+
- equal:
72+
path: spec.source.http.url
73+
value: https://example.com/original.qcow2
74+
75+
- it: allows a storage grow while immutable fields are unchanged
76+
set:
77+
storageClass: replicated
78+
storage: 10Gi
79+
source:
80+
http:
81+
url: https://example.com/original.qcow2
82+
asserts:
83+
- notFailedTemplate: {}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
suite: vm-disk DataVolume source rendering on initial creation
2+
3+
# When no DataVolume exists yet, dv.yaml builds spec.source from .Values.source.
4+
# This pins the refactor that routes every source type through the shared
5+
# $desiredSource variable (used both for rendering and for the immutability
6+
# comparison), so a regression there is caught without a live cluster.
7+
release:
8+
name: test-vm-disk
9+
namespace: tenant-test
10+
templates:
11+
- templates/dv.yaml
12+
tests:
13+
- it: renders a blank source by default
14+
asserts:
15+
- equal:
16+
path: spec.source.blank
17+
value: {}
18+
- equal:
19+
path: spec.storage.storageClassName
20+
value: replicated
21+
22+
- it: renders an http source from values
23+
set:
24+
source:
25+
http:
26+
url: https://example.com/img.qcow2
27+
asserts:
28+
- equal:
29+
path: spec.source.http.url
30+
value: https://example.com/img.qcow2
31+
32+
- it: renders an image source as a cozy-public pvc clone
33+
set:
34+
source:
35+
image:
36+
name: ubuntu
37+
asserts:
38+
- equal:
39+
path: spec.source.pvc.name
40+
value: vm-default-images-ubuntu
41+
- equal:
42+
path: spec.source.pvc.namespace
43+
value: cozy-public
44+
45+
- it: renders a disk source as an in-namespace pvc clone
46+
set:
47+
source:
48+
disk:
49+
name: other
50+
asserts:
51+
- equal:
52+
path: spec.source.pvc.name
53+
value: vm-disk-other
54+
- equal:
55+
path: spec.source.pvc.namespace
56+
value: tenant-test
57+
58+
- it: renders an upload source and requests immediate bind
59+
set:
60+
source:
61+
upload: {}
62+
asserts:
63+
- equal:
64+
path: spec.source.upload
65+
value: {}
66+
- equal:
67+
path: metadata.annotations["cdi.kubevirt.io/storage.bind.immediate.requested"]
68+
value: ""
69+
70+
- it: rejects more than one source type
71+
set:
72+
source:
73+
http:
74+
url: https://example.com/img.qcow2
75+
image:
76+
name: ubuntu
77+
asserts:
78+
- failedTemplate:
79+
errorMessage: Exactly one type of source is expected!

‎packages/apps/vm-disk/values.schema.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@
5353
"description": "Upload local image.",
5454
"type": "object"
5555
}
56-
}
56+
},
57+
"x-kubernetes-validations": [
58+
{
59+
"rule": "self == oldSelf",
60+
"message": "source is immutable"
61+
}
62+
]
5763
},
5864
"optical": {
5965
"description": "Defines if disk should be considered optical.",

‎packages/apps/vm-disk/values.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
## @field {*SourceDisk} [disk] - Clone an existing vm-disk.
2222

2323
## @param {Source} source - The source image location used to create a disk.
24+
## @immutable
2425
source: {}
2526

2627
## @param {bool} optical - Defines if disk should be considered optical.

‎packages/system/vm-disk-rd/cozyrds/vm-disk.yaml‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)