@@ -13,20 +13,13 @@ import (
13
13
"k8s.io/client-go/tools/leaderelection/resourcelock"
14
14
)
15
15
16
- var defaultLeaderTTL = time .Minute
17
-
18
16
const (
19
- devLeaderTTL = time .Hour
17
+ defaultLeaderTTL = time .Minute
18
+ devLeaderTTL = time .Hour
20
19
21
20
FileLockType = "file"
22
21
)
23
22
24
- func init () {
25
- if os .Getenv ("NAH_DEV_MODE" ) != "" {
26
- defaultLeaderTTL = devLeaderTTL
27
- }
28
- }
29
-
30
23
type OnLeader func (context.Context ) error
31
24
type OnNewLeader func (string )
32
25
@@ -38,7 +31,7 @@ type ElectionConfig struct {
38
31
39
32
func NewDefaultElectionConfig (namespace , name string , cfg * rest.Config ) * ElectionConfig {
40
33
return & ElectionConfig {
41
- TTL : defaultLeaderTTL ,
34
+ TTL : defaultElectionTTL () ,
42
35
Namespace : namespace ,
43
36
Name : name ,
44
37
ResourceLockType : resourcelock .LeasesResourceLock ,
@@ -48,7 +41,7 @@ func NewDefaultElectionConfig(namespace, name string, cfg *rest.Config) *Electio
48
41
49
42
func NewFileElectionConfig (fileName string ) * ElectionConfig {
50
43
return & ElectionConfig {
51
- TTL : defaultLeaderTTL ,
44
+ TTL : defaultElectionTTL () ,
52
45
Name : fileName ,
53
46
ResourceLockType : FileLockType ,
54
47
}
@@ -72,6 +65,13 @@ func NewElectionConfig(ttl time.Duration, namespace, name, lockType string, cfg
72
65
}
73
66
}
74
67
68
+ func defaultElectionTTL () time.Duration {
69
+ if os .Getenv ("NAH_DEV_MODE" ) != "" {
70
+ return devLeaderTTL
71
+ }
72
+ return defaultLeaderTTL
73
+ }
74
+
75
75
func (ec * ElectionConfig ) Run (ctx context.Context , id string , onLeader OnLeader , onSwitchLeader OnNewLeader , signalDone func ()) error {
76
76
if ec == nil {
77
77
// Don't start leader election if there is no config.
0 commit comments