Skip to content

Commit

Permalink
feat: add leader election envionment variables to make them configurable
Browse files Browse the repository at this point in the history
Signed-off-by: chenrui <[email protected]>
  • Loading branch information
chenrui committed Sep 20, 2024
1 parent 7b44edd commit a31654d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions controllers/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"time"

"go.uber.org/zap"
appv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a31654d

Please sign in to comment.