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 23, 2024
1 parent 7b44edd commit 993b611
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("parsing %s duration", key, 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 993b611

Please sign in to comment.