Skip to content

Commit 78aab64

Browse files
authored
Improvement in reconciling logic for vmsnapshot and rbacs (#3515)
1 parent f6416fd commit 78aab64

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

manifests/supervisorcluster/1.30/cns-csi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ rules:
129129
- apiGroups: ["nsx.vmware.com"]
130130
resources: ["namespacenetworkinfos"]
131131
verbs: ["get", "list"]
132+
- apiGroups: ["vmoperator.vmware.com"]
133+
resources: ["virtualmachinesnapshots"]
134+
verbs: ["get", "list", "patch", "update", "watch"]
132135
---
133136
kind: ClusterRoleBinding
134137
apiVersion: rbac.authorization.k8s.io/v1

manifests/supervisorcluster/1.31/cns-csi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ rules:
129129
- apiGroups: ["nsx.vmware.com"]
130130
resources: ["namespacenetworkinfos"]
131131
verbs: ["get", "list"]
132+
- apiGroups: ["vmoperator.vmware.com"]
133+
resources: ["virtualmachinesnapshots"]
134+
verbs: ["get", "list", "patch", "update", "watch"]
132135
---
133136
kind: ClusterRoleBinding
134137
apiVersion: rbac.authorization.k8s.io/v1

pkg/syncer/cnsoperator/controller/virtualmachinesnapshot/virtualmachinesnapshot_controller.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
cnstypes "github.com/vmware/govmomi/cns/types"
3131
"go.uber.org/zap"
3232
corev1 "k8s.io/api/core/v1"
33-
3433
apierrors "k8s.io/apimachinery/pkg/api/errors"
3534
"k8s.io/apimachinery/pkg/runtime"
3635
apitypes "k8s.io/apimachinery/pkg/types"
@@ -57,6 +56,7 @@ import (
5756
)
5857

5958
const (
59+
MaxBackOffDurationForReconciler = 5 * time.Minute
6060
defaultMaxWorkerThreadsForVirtualMachineSnapshot = 10
6161
allowedRetriesToPatchCNSVolumeInfo = 5
6262
SyncVolumeFinalizer = "cns.vmware.com/syncvolume"
@@ -237,22 +237,31 @@ func (r *ReconcileVirtualMachineSnapshot) Reconcile(ctx context.Context,
237237
request.Namespace, request.Name)
238238
err = r.reconcileNormal(ctx, log, vmSnapshot)
239239
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
241244
}
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+
242250
backOffDurationMapMutex.Lock()
243251
delete(backOffDuration, request.NamespacedName)
244252
backOffDurationMapMutex.Unlock()
253+
log.Info(msg)
245254
return reconcile.Result{}, nil
246255
}
247256
func (r *ReconcileVirtualMachineSnapshot) reconcileNormal(ctx context.Context, log *zap.SugaredLogger,
248257
vmsnapshot *vmoperatorv1alpha4.VirtualMachineSnapshot) error {
249258
deleteVMSnapshot := false
250259
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)
254260
vmSnapshotPatch := client.MergeFrom(vmsnapshot.DeepCopy())
261+
// If the finalizer is not present, add it.
255262
if controllerutil.AddFinalizer(vmsnapshot, SyncVolumeFinalizer) {
263+
log.Infof("reconcileNormal: Adding finalizer %s on virtualmachinesnapshot cr %s/%s",
264+
SyncVolumeFinalizer, vmsnapshot.Namespace, vmsnapshot.Name)
256265
err := r.client.Patch(ctx, vmsnapshot, vmSnapshotPatch)
257266
if err != nil {
258267
log.Errorf("reconcileNormal: error while add finalizer to "+
@@ -335,6 +344,34 @@ func (r *ReconcileVirtualMachineSnapshot) reconcileNormal(ctx context.Context, l
335344
return nil
336345
}
337346

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+
338375
// getMaxWorkerThreadsToReconcileVirtualMachineSnapshot returns the maximum number
339376
// of worker threads which can be run to reconcile VirtualMachineSnapshot instances.
340377
// If environment variable WORKER_THREADS_VIRTUAL_MACHINE_SNAPSHOT is set and valid,

0 commit comments

Comments
 (0)