fix(backups): delete a restored disk's PVC only once the restore can run

Deleting the original PVC of a keepOriginalPVC=false restore is only
safe when Velero can put the disk back. The Backup artifact outlives
the Velero Backup it points to, which Velero garbage-collects at TTL,
and nothing checked it before prepareForRestore halted the VM and
deleted the disks: a restore of an expired backup lost the volume where
it used to fail with the disk intact. The Velero Backup must now exist
and be Completed before any preparation starts.

A DataVolume that survives its deletion makes CDI recreate the claim
under the original name, which Velero then skips as an existing PVC,
so the restore would end Succeeded on a freshly imported disk. A failed
DataVolume deletion now fails the restore, and the PVCs are deleted
only once every DataVolume is gone. Reading the DataVolume back needs
get on datavolumes, which the ClusterRole did not grant.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
(cherry picked from commit 6d064a8602)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
Mattia Eleuteri authored and Myasnikov Daniil committed 2026-10-22 10:37:52 +00:00
1 parent 2844cc7595
commit 8fcf66892b
4 files changed
+300 -33

No files matched your search

@@ -49,10 +49,12 @@ rules:
- apiGroups: ["kubevirt.io"]
resources: ["virtualmachineinstances"]
verbs: ["get"]
# CDI DataVolumes: pre-restore deletes DVs so CDI doesn't recreate PVCs after rename
# CDI DataVolumes: pre-restore deletes DVs so CDI doesn't recreate PVCs after rename,
# and reads each one back to hold the restore until it is gone (a point Get
# through the dynamic client, so no list/watch)
- apiGroups: ["cdi.kubevirt.io"]
resources: ["datavolumes"]
verbs: ["delete"]
verbs: ["get", "delete"]
# HelmReleases: pre-restore suspends HRs; post-restore rename creates new HR
# and deletes old. patch additionally lets the DefaultObjectsGate stamp
# reconcile.fluxcd.io/forceAt + requestedAt on THIS chart's own release to
@@ -0,0 +1,23 @@
suite: RBAC of the Velero restore preparation
# prepareForRestore reads every DataVolume back after deleting it and holds
# the Velero Restore until it is gone. The read goes through the dynamic
# client, which the Go unit tests run with no authorization, so a missing verb
# only shows up on a cluster, where it fails every in-place VM restore.
templates:
- templates/rbac.yaml
release:
name: backupstrategy-controller
namespace: cozy-backup-controller
tests:
- it: "ClusterRole can read and delete DataVolumes"
asserts:
- contains:
path: rules
content:
apiGroups: ["cdi.kubevirt.io"]
resources: ["datavolumes"]
verbs: ["get", "delete"]