From 76a147c5491cad2e824b429a2148135fdcd81b2d Mon Sep 17 00:00:00 2001 From: Tim Ebert Date: Tue, 5 Sep 2023 12:57:24 +0200 Subject: [PATCH] Reduce log volume of operator --- .../pkg/controllers/webhosting/common.go | 19 +++++++++++++++++++ .../webhosting/website_controller.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/webhosting-operator/pkg/controllers/webhosting/common.go b/webhosting-operator/pkg/controllers/webhosting/common.go index f86c97cf..f5a60b97 100644 --- a/webhosting-operator/pkg/controllers/webhosting/common.go +++ b/webhosting-operator/pkg/controllers/webhosting/common.go @@ -17,7 +17,26 @@ limitations under the License. package webhosting import ( + "context" + + apierrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/reconcile" ) const fieldOwner = client.FieldOwner("webhosting-operator") + +// SilenceConflicts wraps a reconciler to not return conflict errors. The requests is requeued with exponential backoff +// as if an error was returned but the error will not be logged. +func SilenceConflicts(r reconcile.Reconciler) reconcile.Reconciler { + return reconcile.Func(func(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { + result, err := r.Reconcile(ctx, request) + if apierrors.IsConflict(err) { + result.Requeue = true + // RequeueAfter takes precedence over Requeue, set it to zero in case it was returned alongside a conflict error + result.RequeueAfter = 0 + err = nil + } + return result, err + }) +} diff --git a/webhosting-operator/pkg/controllers/webhosting/website_controller.go b/webhosting-operator/pkg/controllers/webhosting/website_controller.go index f5db6537..12be877c 100644 --- a/webhosting-operator/pkg/controllers/webhosting/website_controller.go +++ b/webhosting-operator/pkg/controllers/webhosting/website_controller.go @@ -488,7 +488,7 @@ func (r *WebsiteReconciler) SetupWithManager(mgr ctrl.Manager) error { WithOptions(controller.Options{ MaxConcurrentReconciles: 5, }). - Build(r) + Build(SilenceConflicts(r)) if err != nil { return err }