Skip to content

Commit 2f54287

Browse files
IzzyMusaoceanc80everettraven
authored
Add Leader Election Resource Lock Options (#6426)
Signed-off-by: Izzy Musa <[email protected]> Signed-off-by: Izzy Musa <[email protected]> Co-authored-by: oceanc80 <[email protected]> Co-authored-by: Bryce Palmer <[email protected]>
1 parent b7a4292 commit 2f54287

File tree

2 files changed

+95
-19
lines changed

2 files changed

+95
-19
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: >
5+
Currently, the operator SDK for the Ansible operator lacks options to customize the leader election resource lock behavior. This limits the flexibility of the operator and prevents users from adjusting lease duration and renew deadline according to their specific needs.
6+
7+
Changes:
8+
Implemented enhancements to the operator SDK to include additional flags for configuring leader election resource lock options. These changes provide users with the ability to specify the type of resource object used for locking during leader election and customize the lease duration and renew deadline.
9+
10+
Introduced --leader-elect-lease-duration flag to allow users to define the duration that non-leader candidates will wait to force acquire leadership. The default duration is set to 15 seconds.
11+
12+
Introduced --leader-elect-renew-deadline flag, enabling users to set the renew deadline, which determines the duration that the acting control plane will retry refreshing leadership before giving up. The default duration is set to 10 seconds.
13+
14+
Introduced --leader-elect-resource-lock flag to allow users to define the type of resource object that is used for locking during leader election. Supported options are 'leases', 'endpointsleases', and 'configmapsleases'. The default option is 'leases'.
15+
16+
These changes provide more flexibility and control over leader election behavior, allowing operators to adapt to various deployment scenarios and specific requirements.
17+
18+
# kind is one of:
19+
# - addition
20+
# - change
21+
# - deprecation
22+
# - removal
23+
# - bugfix
24+
kind: "addition"
25+
26+
# Is this a breaking change?
27+
breaking: false
28+
29+
# NOTE: ONLY USE `pull_request_override` WHEN ADDING THIS
30+
# FILE FOR A PREVIOUSLY MERGED PULL_REQUEST!
31+
#
32+
# The generator auto-detects the PR number from the commit
33+
# message in which this file was originally added.
34+
#
35+
# What is the pull request number (without the "#")?
36+
# pull_request_override: 0
37+
38+
39+
# Migration can be defined to automatically add a section to
40+
# the migration guide. This is required for breaking changes.
41+
migration:
42+
header: Header text for the migration section
43+
body: |
44+
Body of the migration section. This should be formatted as markdown and can
45+
span multiple lines.
46+
47+
Using the YAML string '|' operator means that newlines in this string will
48+
be honored and interpretted as newlines in the rendered markdown.
49+
50+

internal/ansible/flags/flag.go

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,30 @@ import (
1919
"time"
2020

2121
"github.com/spf13/pflag"
22-
"k8s.io/client-go/tools/leaderelection/resourcelock"
2322
"sigs.k8s.io/controller-runtime/pkg/manager"
2423
)
2524

2625
// Flags - Options to be used by an ansible operator
2726
type Flags struct {
28-
ReconcilePeriod time.Duration
29-
WatchesFile string
30-
InjectOwnerRef bool
31-
LeaderElection bool
32-
MaxConcurrentReconciles int
33-
AnsibleVerbosity int
34-
AnsibleRolesPath string
35-
AnsibleCollectionsPath string
36-
MetricsBindAddress string
37-
ProbeAddr string
38-
LeaderElectionID string
39-
LeaderElectionNamespace string
40-
GracefulShutdownTimeout time.Duration
41-
AnsibleArgs string
42-
AnsibleLogEvents string
43-
ProxyPort int
27+
ReconcilePeriod time.Duration
28+
WatchesFile string
29+
InjectOwnerRef bool
30+
LeaderElection bool
31+
MaxConcurrentReconciles int
32+
AnsibleVerbosity int
33+
AnsibleRolesPath string
34+
AnsibleCollectionsPath string
35+
MetricsBindAddress string
36+
ProbeAddr string
37+
LeaderElectionResourceLock string
38+
LeaderElectionID string
39+
LeaderElectionNamespace string
40+
LeaseDuration time.Duration
41+
RenewDeadline time.Duration
42+
GracefulShutdownTimeout time.Duration
43+
AnsibleArgs string
44+
AnsibleLogEvents string
45+
ProxyPort int
4446

4547
// Path to a controller-runtime componentconfig file.
4648
// If this is empty, use default values.
@@ -158,6 +160,24 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
158160
" holding the leader lock (required if running locally with leader"+
159161
" election enabled).",
160162
)
163+
flagSet.StringVar(&f.LeaderElectionResourceLock,
164+
"leader-elect-resource-lock",
165+
"configmapsleases",
166+
"The type of resource object that is used for locking during leader election."+
167+
" Supported options are 'leases', 'endpointsleases' and 'configmapsleases'. Default is configmapsleases.",
168+
)
169+
flagSet.DurationVar(&f.LeaseDuration,
170+
"leader-elect-lease-duration",
171+
15*time.Second,
172+
"LeaseDuration is the duration that non-leader candidates will wait"+
173+
" to force acquire leadership. This is measured against time of last observed ack. Default is 15 seconds.",
174+
)
175+
flagSet.DurationVar(&f.RenewDeadline,
176+
"leader-elect-renew-deadline",
177+
10*time.Second,
178+
"RenewDeadline is the duration that the acting controlplane will retry"+
179+
" refreshing leadership before giving up. Default is 10 seconds.",
180+
)
161181
flagSet.DurationVar(&f.GracefulShutdownTimeout,
162182
"graceful-shutdown-timeout",
163183
30*time.Second,
@@ -206,8 +226,14 @@ func (f *Flags) ToManagerOptions(options manager.Options) manager.Options {
206226
if changed("leader-election-namespace") || options.LeaderElectionNamespace == "" {
207227
options.LeaderElectionNamespace = f.LeaderElectionNamespace
208228
}
209-
if options.LeaderElectionResourceLock == "" {
210-
options.LeaderElectionResourceLock = resourcelock.ConfigMapsLeasesResourceLock
229+
if changed("leader-elect-lease-duration") || options.LeaseDuration == nil {
230+
options.LeaseDuration = &f.LeaseDuration
231+
}
232+
if changed("leader-elect-renew-deadline") || options.RenewDeadline == nil {
233+
options.RenewDeadline = &f.RenewDeadline
234+
}
235+
if changed("leader-elect-resource-lock") || options.LeaderElectionResourceLock == "" {
236+
options.LeaderElectionResourceLock = f.LeaderElectionResourceLock
211237
}
212238
if changed("graceful-shutdown-timeout") || options.GracefulShutdownTimeout == nil {
213239
options.GracefulShutdownTimeout = &f.GracefulShutdownTimeout

0 commit comments

Comments
 (0)