Skip to content

Commit 993b611

Browse files
author
chenrui
committed
feat: add leader election envionment variables to make them configurable
Signed-off-by: chenrui <[email protected]>
1 parent 7b44edd commit 993b611

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

controllers/cmd/start.go

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
"go.uber.org/zap"
89
appv1 "k8s.io/api/apps/v1"
@@ -41,6 +42,20 @@ type ArgoEventsControllerOpts struct {
4142
HealthPort int32
4243
}
4344

45+
func lookupEnvDurationOr(key string, o time.Duration) *time.Duration {
46+
logger := logging.NewArgoEventsLogger().Named(eventbus.ControllerName)
47+
v, found := os.LookupEnv(key)
48+
if found && v != "" {
49+
d, err := time.ParseDuration(v)
50+
if err != nil {
51+
logger.With(key, v).Fatalw("parsing %s duration", key, zap.Error(err))
52+
} else {
53+
return &d
54+
}
55+
}
56+
return &o
57+
}
58+
4459
func Start(eventsOpts ArgoEventsControllerOpts) {
4560
logger := logging.NewArgoEventsLogger().Named(eventbus.ControllerName)
4661
config, err := controllers.LoadConfig(func(err error) {
@@ -74,6 +89,9 @@ func Start(eventsOpts ArgoEventsControllerOpts) {
7489
if eventsOpts.LeaderElection {
7590
opts.LeaderElection = true
7691
opts.LeaderElectionID = "argo-events-controller"
92+
opts.LeaseDuration = lookupEnvDurationOr("LEADER_ELECTION_LEASE_DURATION", 15*time.Second)
93+
opts.RenewDeadline = lookupEnvDurationOr("LEADER_ELECTION_RENEW_DEADLINE", 10*time.Second)
94+
opts.RetryPeriod = lookupEnvDurationOr("LEADER_ELECTION_RETRY_PERIOD", 5*time.Second)
7795
}
7896
restConfig := ctrl.GetConfigOrDie()
7997
mgr, err := ctrl.NewManager(restConfig, opts)

0 commit comments

Comments
 (0)