From 99c2d07bdd5f32c6f5a9b260e55afff6c0c5ca18 Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Thu, 11 Jan 2024 11:40:00 +0100 Subject: [PATCH] Decorate reconcilers only when leader election is enabled --- pkg/controller/core/leader_aware_reconciler.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/controller/core/leader_aware_reconciler.go b/pkg/controller/core/leader_aware_reconciler.go index 9a3367f601..7da7766583 100644 --- a/pkg/controller/core/leader_aware_reconciler.go +++ b/pkg/controller/core/leader_aware_reconciler.go @@ -21,6 +21,7 @@ import ( "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -42,14 +43,17 @@ import ( // - Transition to actually reconciling requests in the replica that may acquire // the leader election lease, in case the previously leading replica failed to renew it. func WithLeadingManager(mgr ctrl.Manager, reconciler reconcile.Reconciler, cfg *config.Configuration) reconcile.Reconciler { + // Do not decorate the reconciler if leader election is disabled + if cfg.LeaderElection == nil || !ptr.Deref(cfg.LeaderElection.LeaderElect, false) { + return reconciler + } + // Default to the recommended lease duration, that's used for core components requeueDuration := 15 * time.Second // Otherwise used the configured lease duration for the manager - if le := cfg.LeaderElection; le != nil { - zero := metav1.Duration{} - if duration := le.LeaseDuration; duration != zero { - requeueDuration = duration.Duration - } + zero := metav1.Duration{} + if duration := cfg.LeaderElection.LeaseDuration; duration != zero { + requeueDuration = duration.Duration } return &leaderAwareReconciler{