From a31654d5f314a15a72a2fc53437bb9c56c9ac123 Mon Sep 17 00:00:00 2001 From: chenrui Date: Fri, 20 Sep 2024 19:09:14 +0800 Subject: [PATCH] feat: add leader election envionment variables to make them configurable Signed-off-by: chenrui --- controllers/cmd/start.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/controllers/cmd/start.go b/controllers/cmd/start.go index 927db0add1..9a82065f98 100644 --- a/controllers/cmd/start.go +++ b/controllers/cmd/start.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "time" "go.uber.org/zap" appv1 "k8s.io/api/apps/v1" @@ -41,6 +42,20 @@ type ArgoEventsControllerOpts struct { HealthPort int32 } +func lookupEnvDurationOr(key string, o time.Duration) *time.Duration { + logger := logging.NewArgoEventsLogger().Named(eventbus.ControllerName) + v, found := os.LookupEnv(key) + if found && v != "" { + d, err := time.ParseDuration(v) + if err != nil { + logger.With(key, v).Fatalw("failed to parse", zap.Error(err)) + } else { + return &d + } + } + return &o +} + func Start(eventsOpts ArgoEventsControllerOpts) { logger := logging.NewArgoEventsLogger().Named(eventbus.ControllerName) config, err := controllers.LoadConfig(func(err error) { @@ -74,6 +89,9 @@ func Start(eventsOpts ArgoEventsControllerOpts) { if eventsOpts.LeaderElection { opts.LeaderElection = true opts.LeaderElectionID = "argo-events-controller" + opts.LeaseDuration = lookupEnvDurationOr("LEADER_ELECTION_LEASE_DURATION", 15*time.Second) + opts.RenewDeadline = lookupEnvDurationOr("LEADER_ELECTION_RENEW_DEADLINE", 10*time.Second) + opts.RetryPeriod = lookupEnvDurationOr("LEADER_ELECTION_RETRY_PERIOD", 5*time.Second) } restConfig := ctrl.GetConfigOrDie() mgr, err := ctrl.NewManager(restConfig, opts)