Skip to content

Commit 91b1097

Browse files
committed
feat: make leader election lease duration configurable
Signed-off-by: Dasmat13 <gptdasmat@gmail.com>
1 parent e8b3496 commit 91b1097

5 files changed

Lines changed: 47 additions & 0 deletions

File tree

helm/aws-load-balancer-controller/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ The default values set by the application itself can be confirmed [here](https:/
250250
| `globalAcceleratorMaxConcurrentReconciles` | Maximum number of concurrently running reconcile loops for GlobalAccelerator objects | None |
251251
| `globalAcceleratorMaxExponentialBackoffDelay` | Maximum duration of exponential backoff for GlobalAccelerator reconcile failures | None |
252252
| `syncPeriod` | Period at which the controller forces the repopulation of its local object stores | None |
253+
| `leaderElectionLeaseDuration` | The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot. | None |
253254
| `watchNamespace` | Namespace the controller watches for updates to Kubernetes objects, If empty, all namespaces are watched | None |
254255
| `disableIngressClassAnnotation` | Disables the usage of kubernetes.io/ingress.class annotation | None |
255256
| `disableIngressGroupNameAnnotation` | Disables the usage of alb.ingress.kubernetes.io/group.name annotation | None |

helm/aws-load-balancer-controller/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ spec:
139139
{{- if .Values.syncPeriod }}
140140
- --sync-period={{ .Values.syncPeriod }}
141141
{{- end }}
142+
{{- if .Values.leaderElectionLeaseDuration }}
143+
- --leader-election-lease-duration={{ .Values.leaderElectionLeaseDuration }}
144+
{{- end }}
142145
{{- if .Values.watchNamespace }}
143146
- --watch-namespace={{ .Values.watchNamespace }}
144147
{{- end }}

helm/aws-load-balancer-controller/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ lbStabilizationMonitorInterval:
291291
# Period at which the controller forces the repopulation of its local object stores. (default 10h0m0s)
292292
syncPeriod:
293293

294+
# The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot.
295+
leaderElectionLeaseDuration:
296+
294297
# Namespace the controller watches for updates to Kubernetes objects, If empty, all namespaces are watched.
295298
watchNamespace:
296299

pkg/config/runtime_config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
flagEnableLeaderElection = "enable-leader-election"
2828
flagLeaderElectionID = "leader-election-id"
2929
flagLeaderElectionNamespace = "leader-election-namespace"
30+
flagLeaderElectionLeaseDuration = "leader-election-lease-duration"
3031
flagWatchNamespace = "watch-namespace"
3132
flagSyncPeriod = "sync-period"
3233
flagKubeconfig = "kubeconfig"
@@ -64,6 +65,7 @@ type RuntimeConfig struct {
6465
EnableLeaderElection bool
6566
LeaderElectionID string
6667
LeaderElectionNamespace string
68+
LeaderElectionLeaseDuration time.Duration
6769
WatchNamespace string
6870
SyncPeriod time.Duration
6971
WebhookCertDir string
@@ -89,6 +91,8 @@ func (c *RuntimeConfig) BindFlags(fs *pflag.FlagSet) {
8991
"Name of the leader election ID to use for this controller")
9092
fs.StringVar(&c.LeaderElectionNamespace, flagLeaderElectionNamespace, defaultLeaderElectionNamespace,
9193
"Name of the leader election ID to use for this controller")
94+
fs.DurationVar(&c.LeaderElectionLeaseDuration, flagLeaderElectionLeaseDuration, 0,
95+
"The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot. This is effectively the maximum duration that a leader can be stopped before it is replaced by another candidate. This is only applicable if leader election is enabled.")
9296
fs.StringVar(&c.WatchNamespace, flagWatchNamespace, defaultWatchNamespace,
9397
"Namespace the controller watches for updates to Kubernetes objects, If empty, all namespaces are watched.")
9498
fs.DurationVar(&c.SyncPeriod, flagSyncPeriod, defaultSyncPeriod,
@@ -181,6 +185,10 @@ func BuildRuntimeOptions(rtCfg RuntimeConfig, scheme *runtime.Scheme) (ctrl.Opti
181185
}),
182186
}
183187

188+
if rtCfg.LeaderElectionLeaseDuration > 0 {
189+
opt.LeaseDuration = &rtCfg.LeaderElectionLeaseDuration
190+
}
191+
184192
// cannot set DefaultNamespaces = corev1.NamespaceAll
185193
// https://github.com/kubernetes-sigs/controller-runtime/issues/2628
186194
if rtCfg.WatchNamespace != corev1.NamespaceAll {

pkg/config/runtime_config_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
)
10+
11+
func TestBuildRuntimeOptions_LeaderElectionLeaseDuration(t *testing.T) {
12+
scheme := runtime.NewScheme()
13+
14+
t.Run("default lease duration (not set)", func(t *testing.T) {
15+
cfg := RuntimeConfig{
16+
LeaderElectionLeaseDuration: 0,
17+
}
18+
opts, err := BuildRuntimeOptions(cfg, scheme)
19+
assert.NoError(t, err)
20+
assert.Nil(t, opts.LeaseDuration)
21+
})
22+
23+
t.Run("configured lease duration", func(t *testing.T) {
24+
cfg := RuntimeConfig{
25+
LeaderElectionLeaseDuration: 30 * time.Second,
26+
}
27+
opts, err := BuildRuntimeOptions(cfg, scheme)
28+
assert.NoError(t, err)
29+
assert.NotNil(t, opts.LeaseDuration)
30+
assert.Equal(t, 30*time.Second, *opts.LeaseDuration)
31+
})
32+
}

0 commit comments

Comments
 (0)