Skip to content

Commit f42d969

Browse files
authored
Merge pull request #1303 from jsafrane/fix-finalizer-removal-requeue
Fix finalizer removal requeue
2 parents a388bb6 + df101f7 commit f42d969

File tree

2 files changed

+33
-45
lines changed

2 files changed

+33
-45
lines changed

pkg/sidecar-controller/snapshot_controller.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,22 @@ func (ctrl *csiSnapshotSideCarController) syncContent(content *crdv1.VolumeSnaps
6767
// Note that the deletion snapshot operation will update content SnapshotHandle
6868
// to nil upon a successful deletion. At this
6969
// point, the finalizer on content should NOT be removed to avoid leaking.
70-
err := ctrl.deleteCSISnapshot(content)
70+
var err error
71+
content, err = ctrl.deleteCSISnapshot(content)
7172
if err != nil {
7273
return true, err
7374
}
74-
return false, nil
75+
// Continue removing the finalizer
7576
}
76-
// otherwise, either the snapshot has been deleted from the underlying
77-
// storage system, or it belongs to a volumegroupsnapshot, or the deletion policy is Retain,
77+
// the snapshot has been deleted from the underlying storage system, or
78+
// it belongs to a volumegroupsnapshot, or the deletion policy is Retain,
7879
// remove the finalizer if there is one so that API server could delete
7980
// the object if there is no other finalizer.
8081
err := ctrl.removeContentFinalizer(content)
8182
if err != nil {
8283
return true, err
8384
}
8485
return false, nil
85-
8686
}
8787

8888
// Create snapshot calling the CSI driver only if it is a dynamic
@@ -109,7 +109,7 @@ func (ctrl *csiSnapshotSideCarController) syncContent(content *crdv1.VolumeSnaps
109109
}
110110

111111
// deleteCSISnapshot starts delete action.
112-
func (ctrl *csiSnapshotSideCarController) deleteCSISnapshot(content *crdv1.VolumeSnapshotContent) error {
112+
func (ctrl *csiSnapshotSideCarController) deleteCSISnapshot(content *crdv1.VolumeSnapshotContent) (*crdv1.VolumeSnapshotContent, error) {
113113
klog.V(5).Infof("Deleting snapshot for content: %s", content.Name)
114114
return ctrl.deleteCSISnapshotOperation(content)
115115
}
@@ -406,31 +406,29 @@ func (ctrl *csiSnapshotSideCarController) createSnapshotWrapper(content *crdv1.V
406406
}
407407

408408
// Delete a snapshot: Ask the backend to remove the snapshot device
409-
func (ctrl *csiSnapshotSideCarController) deleteCSISnapshotOperation(content *crdv1.VolumeSnapshotContent) error {
409+
func (ctrl *csiSnapshotSideCarController) deleteCSISnapshotOperation(content *crdv1.VolumeSnapshotContent) (*crdv1.VolumeSnapshotContent, error) {
410410
klog.V(5).Infof("deleteCSISnapshotOperation [%s] started", content.Name)
411411

412412
snapshotterCredentials, err := ctrl.GetCredentialsFromAnnotation(content)
413413
if err != nil {
414414
ctrl.eventRecorder.Event(content, v1.EventTypeWarning, "SnapshotDeleteError", "Failed to get snapshot credentials")
415-
return fmt.Errorf("failed to get input parameters to delete snapshot for content %s: %q", content.Name, err)
415+
return content, fmt.Errorf("failed to get input parameters to delete snapshot for content %s: %q", content.Name, err)
416416
}
417417

418418
err = ctrl.handler.DeleteSnapshot(content, snapshotterCredentials)
419419
if err != nil {
420420
ctrl.eventRecorder.Event(content, v1.EventTypeWarning, "SnapshotDeleteError", "Failed to delete snapshot")
421-
return fmt.Errorf("failed to delete snapshot %#v, err: %v", content.Name, err)
421+
return content, fmt.Errorf("failed to delete snapshot %#v, err: %v", content.Name, err)
422422
}
423423
// the snapshot has been deleted from the underlying storage system, update
424424
// content status to remove snapshot handle etc.
425+
// This triggers a re-sync of the content object, which will continue cleaning the object (e.g. finalizers)
425426
newContent, err := ctrl.clearVolumeContentStatus(content.Name)
426427
if err != nil {
427428
ctrl.eventRecorder.Event(content, v1.EventTypeWarning, "SnapshotDeleteError", "Failed to clear content status")
428-
return err
429+
return content, err
429430
}
430-
// trigger syncContent
431-
// TODO: just enqueue the content object instead of calling syncContent directly
432-
ctrl.updateContentInInformerCache(newContent)
433-
return nil
431+
return newContent, nil
434432
}
435433

436434
// clearVolumeContentStatus resets all fields to nil related to a snapshot in

pkg/sidecar-controller/snapshot_controller_base.go

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,27 @@ func (ctrl *csiSnapshotSideCarController) syncContentByKey(key string) (requeue
260260
// been add/update/sync
261261
if err == nil {
262262
if ctrl.isDriverMatch(content) {
263-
requeue, err = ctrl.updateContentInInformerCache(content)
264-
}
265-
if err != nil {
266-
// If error occurs we add this item back to the queue
267-
return true, err
263+
// Store the new content version in the cache and do not process it if this is
264+
// an old version.
265+
new, err := ctrl.storeContentUpdate(content)
266+
if err != nil {
267+
klog.Errorf("%v", err)
268+
}
269+
if !new {
270+
return false, nil
271+
}
272+
requeue, err = ctrl.syncContent(content)
273+
if err != nil {
274+
if errors.IsConflict(err) {
275+
// Version conflict error happens quite often and the controller
276+
// recovers from it easily.
277+
klog.V(3).Infof("could not sync content %q: %+v", content.Name, err)
278+
} else {
279+
klog.Errorf("could not sync content %q: %+v", content.Name, err)
280+
}
281+
// If error occurs we add this item back to the queue
282+
return true, err
283+
}
268284
}
269285
return requeue, nil
270286
}
@@ -339,32 +355,6 @@ func (ctrl *csiSnapshotSideCarController) isDriverMatch(object interface{}) bool
339355
return false
340356
}
341357

342-
// updateContentInInformerCache runs in worker thread and handles "content added",
343-
// "content updated" and "periodic sync" events.
344-
func (ctrl *csiSnapshotSideCarController) updateContentInInformerCache(content *crdv1.VolumeSnapshotContent) (requeue bool, err error) {
345-
// Store the new content version in the cache and do not process it if this is
346-
// an old version.
347-
new, err := ctrl.storeContentUpdate(content)
348-
if err != nil {
349-
klog.Errorf("%v", err)
350-
}
351-
if !new {
352-
return false, nil
353-
}
354-
requeue, err = ctrl.syncContent(content)
355-
if err != nil {
356-
if errors.IsConflict(err) {
357-
// Version conflict error happens quite often and the controller
358-
// recovers from it easily.
359-
klog.V(3).Infof("could not sync content %q: %+v", content.Name, err)
360-
} else {
361-
klog.Errorf("could not sync content %q: %+v", content.Name, err)
362-
}
363-
return requeue, err
364-
}
365-
return requeue, nil
366-
}
367-
368358
// deleteContent runs in worker thread and handles "content deleted" event.
369359
func (ctrl *csiSnapshotSideCarController) deleteContentInCacheStore(content *crdv1.VolumeSnapshotContent) {
370360
_ = ctrl.contentStore.Delete(content)

0 commit comments

Comments
 (0)