@@ -47,8 +47,8 @@ import (
47
47
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
48
48
k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes"
49
49
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer"
50
- "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/util"
51
50
cnsoptypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/types"
51
+ "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/util"
52
52
)
53
53
54
54
const (
@@ -196,6 +196,9 @@ func (r *Reconciler) Reconcile(ctx context.Context,
196
196
log .Info ("finished reconciling instance" )
197
197
}()
198
198
199
+ backoff := getBackoffDuration (ctx , request .NamespacedName )
200
+ log .Info ("backoff duration is " , backoff )
201
+
199
202
// Handle deletion of the instance.
200
203
// The finalizer will be removed only if the volume is unregistered
201
204
// or if the input parameters are invalid or if the volume is in use.
@@ -204,14 +207,14 @@ func (r *Reconciler) Reconcile(ctx context.Context,
204
207
err = r .reconcileDelete (ctx , * instance , request )
205
208
if err != nil {
206
209
log .Error ("failed to reconcile with error " , err )
207
- return reconcile.Result {}, err
210
+ return reconcile.Result {RequeueAfter : backoff }, nil
208
211
}
209
212
210
213
log .Info ("removing finalizer and allowing deletion of the instance" )
211
214
err := removeFinalizer (ctx , r .client , instance )
212
215
if err != nil {
213
216
log .Error ("failed to remove finalizer from the instance with error " , err )
214
- return reconcile.Result {}, err
217
+ return reconcile.Result {RequeueAfter : backoff }, nil
215
218
}
216
219
217
220
deleteBackoffEntry (ctx , request .NamespacedName )
@@ -229,15 +232,15 @@ func (r *Reconciler) Reconcile(ctx context.Context,
229
232
if err != nil {
230
233
log .Error ("failed to reconcile with error " , err )
231
234
setInstanceError (ctx , r , instance , err .Error ())
232
- return reconcile.Result {RequeueAfter : getBackoffDuration ( ctx , request . NamespacedName ) }, nil
235
+ return reconcile.Result {RequeueAfter : backoff }, nil
233
236
}
234
237
235
238
msg := "successfully unregistered the volume"
236
239
err = setInstanceSuccess (ctx , r , instance , msg )
237
240
if err != nil {
238
- log .Warn ("failed to set instance to success with error " , err )
239
- setInstanceError (ctx , r , instance , "failed to set instance to success" )
240
- return reconcile.Result {RequeueAfter : getBackoffDuration ( ctx , request . NamespacedName ) }, nil
241
+ log .Warn ("failed to update status to success with error " , err )
242
+ setInstanceError (ctx , r , instance , "failed to update status to success" )
243
+ return reconcile.Result {RequeueAfter : backoff }, nil
241
244
}
242
245
243
246
deleteBackoffEntry (ctx , request .NamespacedName )
@@ -343,43 +346,44 @@ func unregisterVolume(ctx context.Context, volMgr volumes.Manager,
343
346
k8sClient , err := newK8sClient (ctx )
344
347
if err != nil {
345
348
log .Error ("failed to init K8s client for volume unregistration with error " , err )
346
- return err
349
+ return errors . New ( "failed to init K8s client for volume unregistration" )
347
350
}
348
351
349
352
err = protectPVC (ctx , k8sClient , params .pvcName , params .namespace ,
350
353
cnsoptypes .CNSUnregisterProtectionFinalizer )
351
354
if err != nil {
352
- log .Error ("failed to add finalizer on PVC with error " , err )
353
- return errors . New ("failed to protect PVC" )
355
+ log .Error ("failed to protect associated PVC with error " , err )
356
+ return fmt . Errorf ("failed to protect associated PVC %s/%s" , params . namespace , params . pvcName )
354
357
}
355
358
356
359
err = deletePVC (ctx , k8sClient , params .pvcName , params .namespace )
357
360
if err != nil {
358
- log .Error ("failed to delete PVC with error " , err )
359
- return errors . New ("failed to delete PVC" )
361
+ log .Error ("failed to delete associated PVC with error " , err )
362
+ return fmt . Errorf ("failed to delete associated PVC %s/%s" , params . namespace , params . pvcName )
360
363
}
361
364
362
365
err = deletePV (ctx , k8sClient , params .pvName )
363
366
if err != nil {
364
- log .Error ("failed to delete PV with error " , err )
365
- return errors . New ("failed to delete PV" )
367
+ log .Error ("failed to delete associated PV with error " , err )
368
+ return fmt . Errorf ("failed to delete associated PV %s" , params . pvName )
366
369
}
367
370
368
371
unregDisk := ! params .retainFCD // If retainFCD is false, unregister the FCD too.
369
372
err = volMgr .UnregisterVolume (ctx , params .volumeID , unregDisk )
370
373
if err != nil {
371
- log .Error ("failed to unregister volume with error " , err )
372
- return errors . New ("failed to unregister volume" )
374
+ log .Error ("failed to unregister associated volume with error " , err )
375
+ return fmt . Errorf ("failed to unregister associated volume %s" , params . volumeID )
373
376
}
374
377
375
378
err = removeFinalizerFromPVC (ctx , k8sClient , params .pvcName , params .namespace ,
376
379
cnsoptypes .CNSUnregisterProtectionFinalizer )
377
380
if err != nil {
378
- log .Error ("failed to remove finalizer from PVC with error " , err )
379
- return errors .New ("failed to remove finalizer from PVC" )
381
+ log .Error ("failed to remove finalizer from associated PVC with error " , err )
382
+ return fmt .Errorf ("failed to remove finalizer from associated PVC %s/%s" ,
383
+ params .namespace , params .pvcName )
380
384
}
381
385
382
- log .Infof ("successfully unregistered CNS volume %s " , params .volumeID )
386
+ log .Debug ("successfully unregistered CNS volume " , params .volumeID )
383
387
return nil
384
388
}
385
389
@@ -422,17 +426,12 @@ func recordEvent(ctx context.Context, r *Reconciler,
422
426
switch eventtype {
423
427
case v1 .EventTypeWarning :
424
428
// Double backOff duration.
425
- backOffDurationMapMutex .Lock ()
426
- backOffDuration [namespacedName ] = min (backOffDuration [namespacedName ]* 2 ,
427
- cnsoptypes .MaxBackOffDurationForReconciler )
429
+ doubleBackoffDuration (ctx , namespacedName )
428
430
r .recorder .Event (instance , v1 .EventTypeWarning , "CnsUnregisterVolumeFailed" , msg )
429
- backOffDurationMapMutex .Unlock ()
430
431
case v1 .EventTypeNormal :
431
432
// Reset backOff duration to one second.
432
- backOffDurationMapMutex .Lock ()
433
- backOffDuration [namespacedName ] = time .Second
433
+ updateBackoffEntry (ctx , namespacedName , time .Second )
434
434
r .recorder .Event (instance , v1 .EventTypeNormal , "CnsUnregisterVolumeSucceeded" , msg )
435
- backOffDurationMapMutex .Unlock ()
436
435
}
437
436
}
438
437
@@ -494,18 +493,27 @@ func _getValidatedParams(ctx context.Context, instance v1a1.CnsUnregisterVolume)
494
493
}
495
494
496
495
func getBackoffDuration (ctx context.Context , name types.NamespacedName ) time.Duration {
497
- log := logger .GetLogger (ctx ).With ("name" , name )
498
496
backOffDurationMapMutex .Lock ()
499
497
defer backOffDurationMapMutex .Unlock ()
500
498
if _ , exists := backOffDuration [name ]; ! exists {
501
- log .Debug ("initializing backoff duration to 1 second" )
502
499
backOffDuration [name ] = time .Second
503
500
}
504
501
505
- log .Infof ("backoff duration is %s" , backOffDuration [name ])
506
502
return backOffDuration [name ]
507
503
}
508
504
505
+ func doubleBackoffDuration (ctx context.Context , name types.NamespacedName ) {
506
+ d := getBackoffDuration (ctx , name )
507
+ d = min (d * 2 , cnsoptypes .MaxBackOffDurationForReconciler )
508
+ updateBackoffEntry (ctx , name , d )
509
+ }
510
+
511
+ func updateBackoffEntry (ctx context.Context , name types.NamespacedName , duration time.Duration ) {
512
+ backOffDurationMapMutex .Lock ()
513
+ defer backOffDurationMapMutex .Unlock ()
514
+ backOffDuration [name ] = duration
515
+ }
516
+
509
517
func deleteBackoffEntry (ctx context.Context , name types.NamespacedName ) {
510
518
backOffDurationMapMutex .Lock ()
511
519
defer backOffDurationMapMutex .Unlock ()
0 commit comments