Skip to content

Commit 838ca62

Browse files
committed
feat: add lease config
1 parent 100c1bb commit 838ca62

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

cmd/processor/main.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const (
4242
logLevelFlag = "log-level"
4343
leaderElectionFlag = "leader-election"
4444
podNamespace = "pod-namespace"
45+
leaseDurationFlag = "lease-duration"
46+
renewDeadlineFlag = "renew-deadline"
47+
retryPeriodFlag = "retry-period"
4548
)
4649

4750
var (
@@ -52,16 +55,25 @@ type processorConfig struct {
5255
LogLevel string
5356
LeaderElection bool
5457
PodNamespace string
58+
LeaseDuration time.Duration
59+
RenewDeadline time.Duration
60+
RetryPeriod time.Duration
5561
}
5662

5763
func parseEnvFlags() processorConfig {
5864
viper.SetDefault(logLevelFlag, "Info")
5965
viper.SetDefault(leaderElectionFlag, false)
6066
viper.SetDefault(podNamespace, "")
67+
viper.SetDefault(leaseDurationFlag, 15*time.Second)
68+
viper.SetDefault(renewDeadlineFlag, 10*time.Second)
69+
viper.SetDefault(retryPeriodFlag, 2*time.Second)
6170

6271
pflag.String(logLevelFlag, viper.GetString(logLevelFlag), "Log level")
6372
pflag.Bool(leaderElectionFlag, viper.GetBool(leaderElectionFlag), "Enable leader election")
6473
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")
6577
pflag.Parse()
6678

6779
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
@@ -72,6 +84,9 @@ func parseEnvFlags() processorConfig {
7284
LogLevel: viper.GetString(logLevelFlag),
7385
LeaderElection: viper.GetBool(leaderElectionFlag),
7486
PodNamespace: viper.GetString(podNamespace),
87+
LeaseDuration: viper.GetDuration(leaseDurationFlag),
88+
RenewDeadline: viper.GetDuration(renewDeadlineFlag),
89+
RetryPeriod: viper.GetDuration(retryPeriodFlag),
7590
}
7691
}
7792

@@ -120,7 +135,7 @@ func main() {
120135
os.Exit(0)
121136
})
122137

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) {
124139
logger.Info("Starting processor work as leader")
125140

126141
// Simulate processor work (to ensure the leader is working)
@@ -141,8 +156,9 @@ func main() {
141156
logger.Info("Processor exited gracefully.")
142157
}
143158

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 {
146162
start(ctx)
147163
return
148164
}
@@ -152,7 +168,7 @@ func whenLeader(ctx context.Context, cancel context.CancelFunc, logger *logrus.E
152168
lock := &resourcelock.LeaseLock{
153169
LeaseMeta: metav1.ObjectMeta{
154170
Name: "agones-processor-lock",
155-
Namespace: namespace,
171+
Namespace: conf.PodNamespace,
156172
},
157173
Client: kubeClient.CoordinationV1(),
158174
LockConfig: resourcelock.ResourceLockConfig{
@@ -171,9 +187,9 @@ func whenLeader(ctx context.Context, cancel context.CancelFunc, logger *logrus.E
171187
// get elected before your background loop finished, violating
172188
// the stated goal of the lease.
173189
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,
177193
Callbacks: leaderelection.LeaderCallbacks{
178194
OnStartedLeading: start,
179195
OnStoppedLeading: func() {

install/helm/agones/templates/processor-deployment.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ spec:
113113
{{- if gt (int .Values.agones.processor.replicas) 1 }}
114114
- name: LEADER_ELECTION
115115
value: "true"
116+
- name: LEASE_DURATION
117+
value: {{ .Values.agones.processor.leaderElection.leaseDuration | default "15s" | quote }}
118+
- name: RENEW_DEADLINE
119+
value: {{ .Values.agones.processor.leaderElection.renewDeadline | default "10s" | quote }}
120+
- name: RETRY_PERIOD
121+
value: {{ .Values.agones.processor.leaderElection.retryPeriod | default "2s" | quote }}
116122
{{- end }}
117123
livenessProbe:
118124
httpGet:

install/helm/agones/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ agones:
300300
affinity: {}
301301
topologySpreadConstraints: []
302302
logLevel: info
303+
leaderElection: {}
303304
http:
304305
port: 8080
305306
readiness:

0 commit comments

Comments
 (0)