-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reconcile requeue limitter was implemented
- Loading branch information
Bohdan Siryk
authored and
Bohdan Siryk
committed
Sep 21, 2023
1 parent
88ad6c2
commit 4a50ffc
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package reconcileutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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 = 1 | ||
|
||
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) { | ||
fmt.Println("Reconciler with limit") | ||
if !limiter.RequeueAllowed(req.NamespacedName) { | ||
fmt.Println("reconcile is not allowed") | ||
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) | ||
} |