Skip to content

Commit

Permalink
add annotation cdi.kubevirt.io/garbageCollected to PVCs when DVs are …
Browse files Browse the repository at this point in the history
…garbage collected

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
  • Loading branch information
mhenriks committed Jan 18, 2024
1 parent 97a5b39 commit 3858375
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,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"

Expand Down
1 change: 1 addition & 0 deletions pkg/controller/datavolume/garbagecollect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/controller/datavolume/import-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,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: pointer.Bool(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: pointer.Bool(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 = pointer.Int32(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() {
Expand Down

0 comments on commit 3858375

Please sign in to comment.