From c82c8e0c7eae9d8f40803d4dfc500e713f664e6d Mon Sep 17 00:00:00 2001 From: xing-yang Date: Thu, 2 Apr 2020 02:01:52 +0000 Subject: [PATCH] Add a check to see if PVC has a finalizer --- pkg/common-controller/snapshot_controller.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/common-controller/snapshot_controller.go b/pkg/common-controller/snapshot_controller.go index ebd472d7..eaaf1235 100644 --- a/pkg/common-controller/snapshot_controller.go +++ b/pkg/common-controller/snapshot_controller.go @@ -167,9 +167,10 @@ func (ctrl *csiSnapshotCommonController) syncSnapshot(snapshot *crdv1.VolumeSnap } // Proceed with snapshot deletion only if snapshot is not in the middled of being - // created from a PVC. This is to ensure that the PVC finalizer can be removed even - // if a delete snapshot request is received before create snapshot has completed. - if snapshot.ObjectMeta.DeletionTimestamp != nil && !ctrl.isPVCInUseByCurrentSnapshot(snapshot) { + // created from a PVC with a finalizer. This is to ensure that the PVC finalizer + // can be removed even if a delete snapshot request is received before create + // snapshot has completed. + if snapshot.ObjectMeta.DeletionTimestamp != nil && !ctrl.isPVCwithFinalizerInUseByCurrentSnapshot(snapshot) { err := ctrl.processSnapshotWithDeletionTimestamp(snapshot) if err != nil { return err @@ -193,15 +194,21 @@ func (ctrl *csiSnapshotCommonController) syncSnapshot(snapshot *crdv1.VolumeSnap return nil } -// Check if PVC is being used by the current snapshot as source -func (ctrl *csiSnapshotCommonController) isPVCInUseByCurrentSnapshot(snapshot *crdv1.VolumeSnapshot) bool { +// Check if PVC has a finalizer and if it is being used by the current snapshot as source. +func (ctrl *csiSnapshotCommonController) isPVCwithFinalizerInUseByCurrentSnapshot(snapshot *crdv1.VolumeSnapshot) bool { // Get snapshot source which is a PVC pvc, err := ctrl.getClaimFromVolumeSnapshot(snapshot) if err != nil { klog.Infof("cannot get claim from snapshot [%s]: [%v] Claim may be deleted already.", snapshot.Name, err) return false } - if snapshot.Spec.Source.PersistentVolumeClaimName != nil && pvc.Name == *snapshot.Spec.Source.PersistentVolumeClaimName && !utils.IsSnapshotReady(snapshot) { + + // Check if there is a Finalizer on PVC. If not, return false + if !slice.ContainsString(pvc.ObjectMeta.Finalizers, utils.PVCFinalizer, nil) { + return false + } + + if !utils.IsSnapshotReady(snapshot) { klog.V(2).Infof("PVC %s/%s is being used by snapshot %s/%s as source", pvc.Namespace, pvc.Name, snapshot.Namespace, snapshot.Name) return true }