@@ -30,7 +30,6 @@ import (
30
30
cnstypes "github.com/vmware/govmomi/cns/types"
31
31
"go.uber.org/zap"
32
32
corev1 "k8s.io/api/core/v1"
33
-
34
33
apierrors "k8s.io/apimachinery/pkg/api/errors"
35
34
"k8s.io/apimachinery/pkg/runtime"
36
35
apitypes "k8s.io/apimachinery/pkg/types"
@@ -57,6 +56,7 @@ import (
57
56
)
58
57
59
58
const (
59
+ MaxBackOffDurationForReconciler = 5 * time .Minute
60
60
defaultMaxWorkerThreadsForVirtualMachineSnapshot = 10
61
61
allowedRetriesToPatchCNSVolumeInfo = 5
62
62
SyncVolumeFinalizer = "cns.vmware.com/syncvolume"
@@ -237,22 +237,31 @@ func (r *ReconcileVirtualMachineSnapshot) Reconcile(ctx context.Context,
237
237
request .Namespace , request .Name )
238
238
err = r .reconcileNormal (ctx , log , vmSnapshot )
239
239
if err != nil {
240
- return reconcile.Result {RequeueAfter : timeout }, err
240
+ recordEvent (ctx , r , vmSnapshot , corev1 .EventTypeWarning , err .Error ())
241
+ log .Errorf ("error while processing virtualmachinesnapshot %s/%s set backOffDuration: %v. error: %v" ,
242
+ request .Namespace , request .Name , backOffDuration [request .NamespacedName ], err )
243
+ return reconcile.Result {RequeueAfter : timeout }, nil
241
244
}
245
+
246
+ msg := fmt .Sprintf ("Successfully successfully processed vmsnapshot %s/%s" ,
247
+ vmSnapshot .Namespace , vmSnapshot .Name )
248
+ recordEvent (ctx , r , vmSnapshot , corev1 .EventTypeNormal , msg )
249
+
242
250
backOffDurationMapMutex .Lock ()
243
251
delete (backOffDuration , request .NamespacedName )
244
252
backOffDurationMapMutex .Unlock ()
253
+ log .Info (msg )
245
254
return reconcile.Result {}, nil
246
255
}
247
256
func (r * ReconcileVirtualMachineSnapshot ) reconcileNormal (ctx context.Context , log * zap.SugaredLogger ,
248
257
vmsnapshot * vmoperatorv1alpha4.VirtualMachineSnapshot ) error {
249
258
deleteVMSnapshot := false
250
259
if vmsnapshot .DeletionTimestamp .IsZero () {
251
- // If the finalizer is not present, add it.
252
- log .Infof ("reconcileNormal: Adding finalizer %s on virtualmachinesnapshot cr %s/%s" ,
253
- SyncVolumeFinalizer , vmsnapshot .Namespace , vmsnapshot .Name )
254
260
vmSnapshotPatch := client .MergeFrom (vmsnapshot .DeepCopy ())
261
+ // If the finalizer is not present, add it.
255
262
if controllerutil .AddFinalizer (vmsnapshot , SyncVolumeFinalizer ) {
263
+ log .Infof ("reconcileNormal: Adding finalizer %s on virtualmachinesnapshot cr %s/%s" ,
264
+ SyncVolumeFinalizer , vmsnapshot .Namespace , vmsnapshot .Name )
256
265
err := r .client .Patch (ctx , vmsnapshot , vmSnapshotPatch )
257
266
if err != nil {
258
267
log .Errorf ("reconcileNormal: error while add finalizer to " +
@@ -335,6 +344,34 @@ func (r *ReconcileVirtualMachineSnapshot) reconcileNormal(ctx context.Context, l
335
344
return nil
336
345
}
337
346
347
+ // recordEvent records the event, sets the backOffDuration for the instance
348
+ // appropriately and logs the message.
349
+ // backOffDuration is reset to 1 second on success and doubled on failure.
350
+ func recordEvent (ctx context.Context , r * ReconcileVirtualMachineSnapshot ,
351
+ instance * vmoperatorv1alpha4.VirtualMachineSnapshot , eventtype string , msg string ) {
352
+ log := logger .GetLogger (ctx )
353
+ log .Debugf ("Event type is %s" , eventtype )
354
+ namespacedName := apitypes.NamespacedName {
355
+ Name : instance .Name ,
356
+ Namespace : instance .Namespace ,
357
+ }
358
+ switch eventtype {
359
+ case corev1 .EventTypeWarning :
360
+ // Double backOff duration.
361
+ backOffDurationMapMutex .Lock ()
362
+ backOffDuration [namespacedName ] = min (backOffDuration [namespacedName ]* 2 ,
363
+ MaxBackOffDurationForReconciler )
364
+ r .recorder .Event (instance , corev1 .EventTypeWarning , "VirtualMachineSnapshotFailed" , msg )
365
+ backOffDurationMapMutex .Unlock ()
366
+ case corev1 .EventTypeNormal :
367
+ // Reset backOff duration to one second.
368
+ backOffDurationMapMutex .Lock ()
369
+ backOffDuration [namespacedName ] = time .Second
370
+ r .recorder .Event (instance , corev1 .EventTypeNormal , "VirtualMachineSnapshotSucceeded" , msg )
371
+ backOffDurationMapMutex .Unlock ()
372
+ }
373
+ }
374
+
338
375
// getMaxWorkerThreadsToReconcileVirtualMachineSnapshot returns the maximum number
339
376
// of worker threads which can be run to reconcile VirtualMachineSnapshot instances.
340
377
// If environment variable WORKER_THREADS_VIRTUAL_MACHINE_SNAPSHOT is set and valid,
0 commit comments