From 61368754b159bbe341c61b8d415150ac4c751d9a Mon Sep 17 00:00:00 2001 From: Michael Henriksen Date: Sun, 14 Jan 2024 06:11:57 -0500 Subject: [PATCH] add annotation cdi.kubevirt.io/garbageCollected to PVCs when DVs are garbage collected (#3059) Signed-off-by: Michael Henriksen --- pkg/controller/common/util.go | 3 ++ pkg/controller/datavolume/garbagecollect.go | 1 + .../datavolume/import-controller_test.go | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/pkg/controller/common/util.go b/pkg/controller/common/util.go index 4e238b791..38362dac6 100644 --- a/pkg/controller/common/util.go +++ b/pkg/controller/common/util.go @@ -245,6 +245,9 @@ const ( // be dynamically provisioned. Its value is the name of the selected node. AnnSelectedNode = "volume.kubernetes.io/selected-node" + // AnnGarbageCollected is a PVC annotation indicating that the PVC was garbage collected + AnnGarbageCollected = AnnAPIGroup + "/garbageCollected" + // CloneUniqueID is used as a special label to be used when we search for the pod CloneUniqueID = "cdi.kubevirt.io/storage.clone.cloneUniqeId" diff --git a/pkg/controller/datavolume/garbagecollect.go b/pkg/controller/datavolume/garbagecollect.go index 99016cc7e..2d6bf3a95 100644 --- a/pkg/controller/datavolume/garbagecollect.go +++ b/pkg/controller/datavolume/garbagecollect.go @@ -104,6 +104,7 @@ func (r *ReconcilerBase) canUpdateFinalizers(ownerRef metav1.OwnerReference) (bo func (r *ReconcilerBase) detachPvcDeleteDv(syncState *dvSyncState) error { updatePvcOwnerRefs(syncState.pvc, syncState.dv) delete(syncState.pvc.Annotations, cc.AnnPopulatedFor) + cc.AddAnnotation(syncState.pvc, cc.AnnGarbageCollected, "true") if err := r.updatePVC(syncState.pvc); err != nil { return err } diff --git a/pkg/controller/datavolume/import-controller_test.go b/pkg/controller/datavolume/import-controller_test.go index 2468dcd76..d6358fb5f 100644 --- a/pkg/controller/datavolume/import-controller_test.go +++ b/pkg/controller/datavolume/import-controller_test.go @@ -1624,6 +1624,34 @@ var _ = Describe("All DataVolume Tests", func() { Expect(pvc.OwnerReferences).To(HaveLen(4)) Expect(pvc.OwnerReferences).To(Equal([]metav1.OwnerReference{ref("1"), ref("2"), ref("3"), vmOwnerRef})) }) + + It("should update PVC when garbage collecting", func() { + dv := NewImportDataVolume("test-dv") + AddAnnotation(dv, AnnDeleteAfterCompletion, "true") + dv.Status.Phase = cdiv1.Succeeded + vmOwnerRef := metav1.OwnerReference{Kind: "VirtualMachine", Name: "test-vm", UID: "test-vm-uid", Controller: ptr.To(true)} + dv.OwnerReferences = append(dv.OwnerReferences, vmOwnerRef) + + pvc := CreatePvc("test-dv", metav1.NamespaceDefault, nil, nil) + dvOwnerRef := metav1.OwnerReference{Kind: "DataVolume", Name: "test-dv", UID: dv.UID, Controller: ptr.To(true)} + pvc.OwnerReferences = append(pvc.OwnerReferences, dvOwnerRef) + + cdiConfig := MakeEmptyCDIConfigSpec(common.ConfigName) + cdiConfig.Status = cdiv1.CDIConfigStatus{ + ScratchSpaceStorageClass: testStorageClass, + } + cdiConfig.Spec.FeatureGates = []string{featuregates.HonorWaitForFirstConsumer} + cdiConfig.Spec.DataVolumeTTLSeconds = ptr.To(int32(0)) + + reconciler = createImportReconcilerWithoutConfig(dv, pvc, cdiConfig) + _, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "test-dv", Namespace: metav1.NamespaceDefault}}) + Expect(err).ToNot(HaveOccurred()) + pvc = &corev1.PersistentVolumeClaim{} + err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "test-dv", Namespace: metav1.NamespaceDefault}, pvc) + Expect(err).ToNot(HaveOccurred()) + Expect(pvc.OwnerReferences).To(Equal([]metav1.OwnerReference{vmOwnerRef})) + Expect(pvc.Annotations[AnnGarbageCollected]).To(Equal("true")) + }) }) var _ = Describe("shouldUseCDIPopulator", func() {