Skip to content

Commit c98b259

Browse files
committed
[snapshot-controller] Retry PVC finalizer removal on conflict
The previous `removePVCFinalizer` function was using the PVC stored in the informer which, in cases where the PVC had been modified since, lead to conflict errors when trying to remove the PVC finalizer through an update. Now the `removePVCFinalizer` function uses the `RetryOnConflict` helper to make sure the update goes through.
1 parent 3a557d6 commit c98b259

File tree

4 files changed

+126
-12
lines changed

4 files changed

+126
-12
lines changed

pkg/common-controller/snapshot_controller.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/apimachinery/pkg/labels"
3131
"k8s.io/client-go/kubernetes/scheme"
3232
ref "k8s.io/client-go/tools/reference"
33+
"k8s.io/client-go/util/retry"
3334
corev1helpers "k8s.io/component-helpers/scheduling/corev1"
3435
klog "k8s.io/klog/v2"
3536

@@ -930,19 +931,22 @@ func (ctrl *csiSnapshotCommonController) ensurePVCFinalizer(snapshot *crdv1.Volu
930931

931932
// removePVCFinalizer removes a Finalizer for VolumeSnapshot Source PVC.
932933
func (ctrl *csiSnapshotCommonController) removePVCFinalizer(pvc *v1.PersistentVolumeClaim) error {
933-
// Get snapshot source which is a PVC
934-
// TODO(xyang): We get PVC from informer but it may be outdated
935-
// Should get it from API server directly before removing finalizer
936-
pvcClone := pvc.DeepCopy()
937-
pvcClone.ObjectMeta.Finalizers = utils.RemoveString(pvcClone.ObjectMeta.Finalizers, utils.PVCFinalizer)
938-
939-
_, err := ctrl.client.CoreV1().PersistentVolumeClaims(pvcClone.Namespace).Update(context.TODO(), pvcClone, metav1.UpdateOptions{})
940-
if err != nil {
941-
return newControllerUpdateError(pvcClone.Name, err.Error())
942-
}
934+
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
935+
// Get snapshot source which is a PVC
936+
newPvc, err := ctrl.client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(context.TODO(), pvc.Name, metav1.GetOptions{})
937+
if err != nil {
938+
return err
939+
}
940+
newPvc = newPvc.DeepCopy()
941+
newPvc.ObjectMeta.Finalizers = utils.RemoveString(newPvc.ObjectMeta.Finalizers, utils.PVCFinalizer)
942+
_, err = ctrl.client.CoreV1().PersistentVolumeClaims(newPvc.Namespace).Update(context.TODO(), newPvc, metav1.UpdateOptions{})
943+
if err != nil {
944+
return newControllerUpdateError(newPvc.Name, err.Error())
945+
}
943946

944-
klog.V(5).Infof("Removed protection finalizer from persistent volume claim %s", pvc.Name)
945-
return nil
947+
klog.V(5).Infof("Removed protection finalizer from persistent volume claim %s", pvc.Name)
948+
return nil
949+
})
946950
}
947951

948952
// isPVCBeingUsed checks if a PVC is being used as a source to create a snapshot.

vendor/k8s.io/client-go/util/retry/OWNERS

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/client-go/util/retry/util.go

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ k8s.io/client-go/util/consistencydetector
770770
k8s.io/client-go/util/flowcontrol
771771
k8s.io/client-go/util/homedir
772772
k8s.io/client-go/util/keyutil
773+
k8s.io/client-go/util/retry
773774
k8s.io/client-go/util/watchlist
774775
k8s.io/client-go/util/workqueue
775776
# k8s.io/component-base v0.31.0-rc.0 => k8s.io/component-base v0.31.0-rc.0

0 commit comments

Comments
 (0)