fix(vm-disk): refuse a disk smaller than the golden image it clones (#4960)

## What this PR does

A VMDisk created from `source.image` with a `storage` smaller than its
golden image never comes up. CDI v1.66.1 checks a clone against its
source in `validateClone`
(`pkg/controller/datavolume/pvc-clone-controller.go`) before it creates
the target claim. When the source PVC is in `Block` mode and requests
more than the DataVolume, CDI records `CloneValidationFailed` ("target
resources requests storage size is smaller than the source"), creates no
claim and leaves the DataVolume `Pending` for good. The webhook admits
such a DataVolume, so nothing fails at install. Raising `storage`
afterwards does not help either, because the chart renders an existing
DataVolume's spec as it is (CDI does not let it change) and the resize
hook only patches a claim that exists.

This is the default path. Every image in `vm-default-images` is 20Gi on
`replicated`, the StorageProfile CDI builds for the LINSTOR driver puts
`Block` first, and the chart defaults `storage` to 5Gi. Seen on a v1.6.4
cluster with CDI v1.66.1. A 10Gi disk from a 20Gi golden image sat
`Pending` with `CloneValidationFailed` while its release kept timing
out, and raising the disk to 25Gi left the DataVolume at 10Gi. The only
way out is to delete the disk and create it again at 20Gi or more.

The chart now refuses such a disk at render time, before any DataVolume
exists, with a message that names both sizes and the minimum to set, for
example `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
compares what CDI compares, the golden PVC's request against the disk's
request in bytes, and first raises the disk's request to the target
StorageProfile's `cdi.kubevirt.io/minimumSupportedPvcSize` the way
`GetEffectiveStorageResources` does. For an empty `storageClass` the
target is the class the check from #4796 already resolves, the default
virt class or else the Kubernetes default.

Nothing is compared, and the disk renders as before, when the golden PVC
cannot be read (under `helm template`, or before CDI created it), when
the golden PVC is in `Filesystem` mode, and for any source other than
`image`. For a `Filesystem` source CDI sizes a `Block` target from the
virtual size of the image inside it and otherwise compares requests
inflated by a per-class filesystem overhead, neither of which the chart
reproduces. An existing disk is exempt, as for the other golden checks,
since its DataVolume spec cannot change anyway.

Not covered here. A `source.disk` clone into a smaller disk goes through
the same CDI check and still renders. The chart default of 5Gi stays
below every default image, so a disk created from one at the default
size is now refused with the message above instead of hanging. Whether
the default should follow the images is a separate decision.

A new helm-unittest suite, `tests/dv_golden_size_test.yaml`, has 15
cases. It pins the refusal at the chart default against a 20Gi `Block`
golden with the whole message, the equal and the larger size, the
comparison in bytes (`21G` is refused against `20Gi`, `20480Mi`
renders), a StorageProfile minimum that covers the golden and one that
stays below it, the minimum read from the default class for an empty
`storageClass`, and the cases that render as before, a `Filesystem`
golden, no cluster, a golden PVC that does not exist yet, a disk without
a size, an existing disk, an `http` and an `upload` source. Against the
template on main the 3 refusal cases fail and the other 12 pass. Six
mutations of the new block each turn at least one case red (any volume
mode compared, the minimum dropped, the minimum read from the requested
class only, no check that `storage` is set, an equal size refused, sizes
compared as strings). `helm unittest` passes 129 of 129 for vm-disk,
against 114 on main, with helm-unittest 1.1.2 as CI pins it. Merged with
the current heads of #4802 or #4955, which touch the same template,
vm-disk passes 137 and 131 cases. `helm template` without a cluster
renders an image disk at 5Gi as before. I have not run it on a live
cluster.

The commit does not cherry-pick onto release-1.6 or release-1.7. Neither
carries the check from #4796 it builds on, and release-1.6 has none of
the golden checks, so a backport would be an adapted change.

### Screenshots

Not a UI change.

### Downstream repositories

Template and tests only. No values, schema, README, defaults or
`ApplicationDefinition` change. The golden images guide on the website
already creates its example disk at the image's 20Gi.

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(vm-disk): a new disk from a golden image is now refused at render time when its storage is smaller than the image, with a message that names both sizes. Before, CDI refused the clone with CloneValidationFailed, the disk stayed pending forever, and raising its storage afterwards did not help. The default images are 20Gi and the default storage is 5Gi, so a disk from one of them needs storage set to 20Gi or more.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Disk sizes are now checked against the size of Block-mode golden
images when cloning. If the requested size is too small, it is increased
to the target storage profile’s minimum supported size when possible;
otherwise, rendering fails with an error that states the required size.
* Size comparisons account for actual byte capacity, so equivalent size
units are handled correctly. Filesystem-mode sources and other disk
sources are unaffected.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-13 15:41:51 +03:00
commit a5c58e5e98
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: {}