Skip to content

Commit

Permalink
Decorate reconcilers only when leader election is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 11, 2024
1 parent de8687b commit 99c2d07
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pkg/controller/core/leader_aware_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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{
Expand Down

0 comments on commit 99c2d07

Please sign in to comment.