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 <202671653+yankawai@users.noreply.github.com>
This commit is contained in:
Yan Bondarenko committed 2026-10-19 16:04:58 +03:00
1 parent aed7fa896e
commit 8d82348e85
2 files changed
+272

No files matched your search

+20
View File
@@ -146,6 +146,26 @@
{{- 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) }}
{{- end }}
{{- end }}
{{- /* CDI v1.66.1 refuses to clone a Block source PVC into a smaller
request before it creates the claim (validateClone in
pkg/controller/datavolume/pvc-clone-controller.go), and the
DataVolume then waits forever. It raises the request to the target
StorageProfile's minimumSupportedPvcSize before comparing. A
Filesystem source is not compared, as CDI sizes that clone from
the image inside it or with a filesystem overhead the chart does
not reproduce. */}}
{{- $goldenSize := dig "spec" "resources" "requests" "storage" "" ($goldenPVC | default dict) | toString }}
{{- if and .Values.storage $goldenSize (eq (dig "spec" "volumeMode" "" ($goldenPVC | default dict) | toString) "Block") }}
{{- $goldenBytes := include "cozy-lib.resources.toFloat" $goldenSize | float64 }}
{{- $diskBytes := include "cozy-lib.resources.toFloat" (.Values.storage | toString) | float64 }}
{{- if and (lt $diskBytes $goldenBytes) $targetSC }}
{{- $minimum := dig "metadata" "annotations" "cdi.kubevirt.io/minimumSupportedPvcSize" "" (lookup "cdi.kubevirt.io/v1beta1" "StorageProfile" "" $targetSC | default dict) | toString }}
{{- $diskBytes = maxf $diskBytes (include "cozy-lib.resources.toFloat" $minimum | float64) }}
{{- end }}
{{- if lt $diskBytes $goldenBytes }}
{{- 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) }}
{{- end }}
{{- end }}
{{- end }}
{{- else if hasKey .Values.source "http" }}
{{- $desiredSource = dict "http" (dict "url" (required "A valid .Values.source.http.url entry required!" .Values.source.http.url)) }}
@@ -0,0 +1,252 @@
suite: vm-disk refuses a disk smaller than the golden image it clones
release:
name: test-vm-disk
namespace: tenant-test
templates:
- templates/dv.yaml
tests:
- it: refuses a new disk at the chart default size from a larger Block golden
set:
source: {image: {name: ubuntu}}
kubernetesProvider:
scheme: &scheme
"cdi.kubevirt.io/v1beta1/DataVolume":
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: datavolumes}
namespaced: true
"cdi.kubevirt.io/v1beta1/StorageProfile":
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: storageprofiles}
namespaced: false
"v1/Namespace":
gvr: {group: "", version: v1, resource: namespaces}
namespaced: false
"v1/PersistentVolumeClaim":
gvr: {group: "", version: v1, resource: persistentvolumeclaims}
namespaced: true
"storage.k8s.io/v1/StorageClass":
gvr: {group: storage.k8s.io, version: v1, resource: storageclasses}
namespaced: false
objects:
- &namespace
kind: Namespace
apiVersion: v1
metadata: {name: tenant-test}
- &goldenPVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
spec:
storageClassName: replicated
volumeMode: Block
resources: {requests: {storage: 20Gi}}
asserts:
- failedTemplate:
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\.'
- it: clones into a disk the size of the golden
set:
source: {image: {name: ubuntu}}
storage: 20Gi
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- equal:
path: spec.storage.resources.requests.storage
value: 20Gi
- it: clones into a disk larger than the golden
set:
source: {image: {name: ubuntu}}
storage: 25Gi
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- equal:
path: spec.storage.resources.requests.storage
value: 25Gi
- it: compares bytes, so 21G is smaller than a 20Gi golden
set:
source: {image: {name: ubuntu}}
storage: 21G
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- failedTemplate:
errorPattern: 'storage 21G is smaller than golden image "vm-default-images-ubuntu" \(20Gi\)'
- it: compares bytes, so 20480Mi is the size of a 20Gi golden
set:
source: {image: {name: ubuntu}}
storage: 20480Mi
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- equal:
path: spec.storage.resources.requests.storage
value: 20480Mi
- it: clones when the target StorageProfile's minimum PVC size covers the golden
set:
source: {image: {name: ubuntu}}
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
- kind: StorageProfile
apiVersion: cdi.kubevirt.io/v1beta1
metadata:
name: replicated
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 20Gi}
asserts:
- notFailedTemplate: {}
- it: refuses when the target StorageProfile's minimum PVC size is still below the golden
set:
source: {image: {name: ubuntu}}
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
- kind: StorageProfile
apiVersion: cdi.kubevirt.io/v1beta1
metadata:
name: replicated
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 10Gi}
asserts:
- failedTemplate:
errorPattern: 'storage 5Gi is smaller than golden image "vm-default-images-ubuntu" \(20Gi\)'
- it: reads the minimum PVC size of the default class for an empty storageClass
set:
source: {image: {name: ubuntu}}
storageClass: ""
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
- kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: replicated
annotations: {storageclass.kubernetes.io/is-default-class: "true"}
provisioner: linstor.csi.linbit.com
- kind: StorageProfile
apiVersion: cdi.kubevirt.io/v1beta1
metadata:
name: replicated
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 20Gi}
asserts:
- notFailedTemplate: {}
- it: does not compare sizes for a Filesystem golden
set:
source: {image: {name: ubuntu}}
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- kind: PersistentVolumeClaim
apiVersion: v1
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
spec:
storageClassName: replicated
volumeMode: Filesystem
resources: {requests: {storage: 20Gi}}
asserts:
- notFailedTemplate: {}
- it: renders without a cluster to look the golden up in
set:
source: {image: {name: ubuntu}}
asserts:
- equal:
path: spec.storage.resources.requests.storage
value: 5Gi
- it: clones a golden whose PVC does not exist yet
set:
source: {image: {name: ubuntu}}
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- kind: DataVolume
apiVersion: cdi.kubevirt.io/v1beta1
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
status: {phase: ImportScheduled}
asserts:
- notFailedTemplate: {}
- it: leaves a disk without a size to CDI, which takes the golden's
set:
source: {image: {name: ubuntu}}
storage: null
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- notFailedTemplate: {}
- it: leaves an existing disk alone
set:
source: {image: {name: ubuntu}}
storage: 25Gi
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
- kind: DataVolume
apiVersion: cdi.kubevirt.io/v1beta1
metadata:
name: test-vm-disk
namespace: tenant-test
annotations:
vm-disk.cozystack.io/source: '{"image":{"name":"ubuntu"}}'
vm-disk.cozystack.io/storage-class: replicated
spec:
source: {pvc: {name: vm-default-images-ubuntu, namespace: cozy-public}}
storage: {resources: {requests: {storage: 10Gi}}, storageClassName: replicated}
asserts:
- notFailedTemplate: {}
- it: does not compare sizes for an http source
set:
source: {http: {url: "https://example.com/disk.img"}}
storage: 1Gi
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- notFailedTemplate: {}
- it: does not compare sizes for an upload source
set:
source: {upload: {}}
storage: 1Gi
kubernetesProvider:
scheme: *scheme
objects:
- *namespace
- *goldenPVC
asserts:
- notFailedTemplate: {}