@@ -42,6 +42,9 @@ const (
42
42
logLevelFlag = "log-level"
43
43
leaderElectionFlag = "leader-election"
44
44
podNamespace = "pod-namespace"
45
+ leaseDurationFlag = "lease-duration"
46
+ renewDeadlineFlag = "renew-deadline"
47
+ retryPeriodFlag = "retry-period"
45
48
)
46
49
47
50
var (
@@ -52,16 +55,25 @@ type processorConfig struct {
52
55
LogLevel string
53
56
LeaderElection bool
54
57
PodNamespace string
58
+ LeaseDuration time.Duration
59
+ RenewDeadline time.Duration
60
+ RetryPeriod time.Duration
55
61
}
56
62
57
63
func parseEnvFlags () processorConfig {
58
64
viper .SetDefault (logLevelFlag , "Info" )
59
65
viper .SetDefault (leaderElectionFlag , false )
60
66
viper .SetDefault (podNamespace , "" )
67
+ viper .SetDefault (leaseDurationFlag , 15 * time .Second )
68
+ viper .SetDefault (renewDeadlineFlag , 10 * time .Second )
69
+ viper .SetDefault (retryPeriodFlag , 2 * time .Second )
61
70
62
71
pflag .String (logLevelFlag , viper .GetString (logLevelFlag ), "Log level" )
63
72
pflag .Bool (leaderElectionFlag , viper .GetBool (leaderElectionFlag ), "Enable leader election" )
64
73
pflag .String (podNamespace , viper .GetString (podNamespace ), "Pod namespace" )
74
+ pflag .Duration (leaseDurationFlag , viper .GetDuration (leaseDurationFlag ), "Leader election lease duration" )
75
+ pflag .Duration (renewDeadlineFlag , viper .GetDuration (renewDeadlineFlag ), "Leader election renew deadline" )
76
+ pflag .Duration (retryPeriodFlag , viper .GetDuration (retryPeriodFlag ), "Leader election retry period" )
65
77
pflag .Parse ()
66
78
67
79
viper .SetEnvKeyReplacer (strings .NewReplacer ("-" , "_" ))
@@ -72,6 +84,9 @@ func parseEnvFlags() processorConfig {
72
84
LogLevel : viper .GetString (logLevelFlag ),
73
85
LeaderElection : viper .GetBool (leaderElectionFlag ),
74
86
PodNamespace : viper .GetString (podNamespace ),
87
+ LeaseDuration : viper .GetDuration (leaseDurationFlag ),
88
+ RenewDeadline : viper .GetDuration (renewDeadlineFlag ),
89
+ RetryPeriod : viper .GetDuration (retryPeriodFlag ),
75
90
}
76
91
}
77
92
@@ -120,7 +135,7 @@ func main() {
120
135
os .Exit (0 )
121
136
})
122
137
123
- whenLeader (ctx , cancelCtx , logger , conf . LeaderElection , kubeClient , conf . PodNamespace , func (ctx context.Context ) {
138
+ whenLeader (ctx , cancelCtx , logger , conf , kubeClient , func (ctx context.Context ) {
124
139
logger .Info ("Starting processor work as leader" )
125
140
126
141
// Simulate processor work (to ensure the leader is working)
@@ -141,8 +156,9 @@ func main() {
141
156
logger .Info ("Processor exited gracefully." )
142
157
}
143
158
144
- func whenLeader (ctx context.Context , cancel context.CancelFunc , logger * logrus.Entry , doLeaderElection bool , kubeClient * kubernetes.Clientset , namespace string , start func (_ context.Context )) {
145
- if ! doLeaderElection {
159
+ func whenLeader (ctx context.Context , cancel context.CancelFunc , logger * logrus.Entry ,
160
+ conf processorConfig , kubeClient * kubernetes.Clientset , start func (_ context.Context )) {
161
+ if ! conf .LeaderElection {
146
162
start (ctx )
147
163
return
148
164
}
@@ -152,7 +168,7 @@ func whenLeader(ctx context.Context, cancel context.CancelFunc, logger *logrus.E
152
168
lock := & resourcelock.LeaseLock {
153
169
LeaseMeta : metav1.ObjectMeta {
154
170
Name : "agones-processor-lock" ,
155
- Namespace : namespace ,
171
+ Namespace : conf . PodNamespace ,
156
172
},
157
173
Client : kubeClient .CoordinationV1 (),
158
174
LockConfig : resourcelock.ResourceLockConfig {
@@ -171,9 +187,9 @@ func whenLeader(ctx context.Context, cancel context.CancelFunc, logger *logrus.E
171
187
// get elected before your background loop finished, violating
172
188
// the stated goal of the lease.
173
189
ReleaseOnCancel : true ,
174
- LeaseDuration : 15 * time . Second ,
175
- RenewDeadline : 10 * time . Second ,
176
- RetryPeriod : 2 * time . Second ,
190
+ LeaseDuration : conf . LeaseDuration ,
191
+ RenewDeadline : conf . RenewDeadline ,
192
+ RetryPeriod : conf . RetryPeriod ,
177
193
Callbacks : leaderelection.LeaderCallbacks {
178
194
OnStartedLeading : start ,
179
195
OnStoppedLeading : func () {
0 commit comments