Skip to content

Commit 1b92784

Browse files
authored
fix: read the default TTL at call time (#24)
Trying to read the environment variable at package load time doesn't workt with Obot. Signed-off-by: Donnie Adams <[email protected]>
1 parent 17b79ba commit 1b92784

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

pkg/leader/leader.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@ import (
1313
"k8s.io/client-go/tools/leaderelection/resourcelock"
1414
)
1515

16-
var defaultLeaderTTL = time.Minute
17-
1816
const (
19-
devLeaderTTL = time.Hour
17+
defaultLeaderTTL = time.Minute
18+
devLeaderTTL = time.Hour
2019

2120
FileLockType = "file"
2221
)
2322

24-
func init() {
25-
if os.Getenv("NAH_DEV_MODE") != "" {
26-
defaultLeaderTTL = devLeaderTTL
27-
}
28-
}
29-
3023
type OnLeader func(context.Context) error
3124
type OnNewLeader func(string)
3225

@@ -38,7 +31,7 @@ type ElectionConfig struct {
3831

3932
func NewDefaultElectionConfig(namespace, name string, cfg *rest.Config) *ElectionConfig {
4033
return &ElectionConfig{
41-
TTL: defaultLeaderTTL,
34+
TTL: defaultElectionTTL(),
4235
Namespace: namespace,
4336
Name: name,
4437
ResourceLockType: resourcelock.LeasesResourceLock,
@@ -48,7 +41,7 @@ func NewDefaultElectionConfig(namespace, name string, cfg *rest.Config) *Electio
4841

4942
func NewFileElectionConfig(fileName string) *ElectionConfig {
5043
return &ElectionConfig{
51-
TTL: defaultLeaderTTL,
44+
TTL: defaultElectionTTL(),
5245
Name: fileName,
5346
ResourceLockType: FileLockType,
5447
}
@@ -72,6 +65,13 @@ func NewElectionConfig(ttl time.Duration, namespace, name, lockType string, cfg
7265
}
7366
}
7467

68+
func defaultElectionTTL() time.Duration {
69+
if os.Getenv("NAH_DEV_MODE") != "" {
70+
return devLeaderTTL
71+
}
72+
return defaultLeaderTTL
73+
}
74+
7575
func (ec *ElectionConfig) Run(ctx context.Context, id string, onLeader OnLeader, onSwitchLeader OnNewLeader, signalDone func()) error {
7676
if ec == nil {
7777
// Don't start leader election if there is no config.

0 commit comments

Comments
 (0)