Skip to content

Commit

Permalink
reconcile requeue limiter was implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Siryk authored and Bohdan Siryk committed Sep 22, 2023
1 parent 1fa48eb commit f438ce8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/instaclustr/operator/pkg/exposeservice"
"github.com/instaclustr/operator/pkg/instaclustr"
"github.com/instaclustr/operator/pkg/models"
"github.com/instaclustr/operator/pkg/reconcilerutils"
"github.com/instaclustr/operator/pkg/scheduler"
)

Expand Down Expand Up @@ -1307,5 +1308,5 @@ func (r *CassandraReconciler) SetupWithManager(mgr ctrl.Manager) error {
event.Object.GetAnnotations()[models.ResourceStateAnnotation] = models.GenericEvent
return true
},
})).Complete(r)
})).Complete(reconcilerutils.ReconcilerWithLimit(r))
}
58 changes: 58 additions & 0 deletions pkg/reconcilerutils/reconcile-requeue-limiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package reconcilerutils

import (
"context"
"github.com/instaclustr/operator/pkg/models"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const MaxReconcileRequeueRetries = 5

func ReconcilerWithLimit(r reconcile.Reconciler) reconcile.Func {
limiter := &ReconcileRequeueLimiter{
maxRetries: MaxReconcileRequeueRetries,
m: make(map[types.NamespacedName]int),
}

return func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
if !limiter.RequeueAllowed(req.NamespacedName) {
l := log.FromContext(ctx)
l.Info("Amount of reconcile requeue retries reached its maximum for the resource",
"req", req.NamespacedName,
)

limiter.Reset(req.NamespacedName)

return models.ExitReconcile, nil
}

result, err := r.Reconcile(ctx, req)
if err != nil || result == models.ReconcileRequeue {
limiter.Requeue(req.NamespacedName)
return result, err
}

limiter.Reset(req.NamespacedName)

return result, nil
}
}

type ReconcileRequeueLimiter struct {
maxRetries int
m map[types.NamespacedName]int
}

func (r *ReconcileRequeueLimiter) Requeue(key types.NamespacedName) {
r.m[key]++
}

func (r *ReconcileRequeueLimiter) RequeueAllowed(key types.NamespacedName) bool {
return r.m[key] < r.maxRetries
}

func (r *ReconcileRequeueLimiter) Reset(key types.NamespacedName) {
delete(r.m, key)
}

0 comments on commit f438ce8

Please sign in to comment.