Skip to content

Commit

Permalink
Add Leader Election Resource Lock Options (#6426)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people authored Jun 21, 2023
1 parent b7a4292 commit 2f54287
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 19 deletions.
50 changes: 50 additions & 0 deletions changelog/fragments/add-leader-election-resource-lock-options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# entries is a list of entries to include in
# release notes and/or the migration guide
entries:
- description: >
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.
Changes:
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.
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.
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.
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'.
These changes provide more flexibility and control over leader election behavior, allowing operators to adapt to various deployment scenarios and specific requirements.
# kind is one of:
# - addition
# - change
# - deprecation
# - removal
# - bugfix
kind: "addition"
# Is this a breaking change?
breaking: false
# NOTE: ONLY USE `pull_request_override` WHEN ADDING THIS
# FILE FOR A PREVIOUSLY MERGED PULL_REQUEST!
#
# The generator auto-detects the PR number from the commit
# message in which this file was originally added.
#
# What is the pull request number (without the "#")?
# pull_request_override: 0
# Migration can be defined to automatically add a section to
# the migration guide. This is required for breaking changes.
migration:
header: Header text for the migration section
body: |
Body of the migration section. This should be formatted as markdown and can
span multiple lines.
Using the YAML string '|' operator means that newlines in this string will
be honored and interpretted as newlines in the rendered markdown.
64 changes: 45 additions & 19 deletions internal/ansible/flags/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ import (
"time"

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

// Flags - Options to be used by an ansible operator
type Flags struct {
ReconcilePeriod time.Duration
WatchesFile string
InjectOwnerRef bool
LeaderElection bool
MaxConcurrentReconciles int
AnsibleVerbosity int
AnsibleRolesPath string
AnsibleCollectionsPath string
MetricsBindAddress string
ProbeAddr string
LeaderElectionID string
LeaderElectionNamespace string
GracefulShutdownTimeout time.Duration
AnsibleArgs string
AnsibleLogEvents string
ProxyPort int
ReconcilePeriod time.Duration
WatchesFile string
InjectOwnerRef bool
LeaderElection bool
MaxConcurrentReconciles int
AnsibleVerbosity int
AnsibleRolesPath string
AnsibleCollectionsPath string
MetricsBindAddress string
ProbeAddr string
LeaderElectionResourceLock string
LeaderElectionID string
LeaderElectionNamespace string
LeaseDuration time.Duration
RenewDeadline time.Duration
GracefulShutdownTimeout time.Duration
AnsibleArgs string
AnsibleLogEvents string
ProxyPort int

// Path to a controller-runtime componentconfig file.
// If this is empty, use default values.
Expand Down Expand Up @@ -158,6 +160,24 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
" holding the leader lock (required if running locally with leader"+
" election enabled).",
)
flagSet.StringVar(&f.LeaderElectionResourceLock,
"leader-elect-resource-lock",
"configmapsleases",
"The type of resource object that is used for locking during leader election."+
" Supported options are 'leases', 'endpointsleases' and 'configmapsleases'. Default is configmapsleases.",
)
flagSet.DurationVar(&f.LeaseDuration,
"leader-elect-lease-duration",
15*time.Second,
"LeaseDuration is the duration that non-leader candidates will wait"+
" to force acquire leadership. This is measured against time of last observed ack. Default is 15 seconds.",
)
flagSet.DurationVar(&f.RenewDeadline,
"leader-elect-renew-deadline",
10*time.Second,
"RenewDeadline is the duration that the acting controlplane will retry"+
" refreshing leadership before giving up. Default is 10 seconds.",
)
flagSet.DurationVar(&f.GracefulShutdownTimeout,
"graceful-shutdown-timeout",
30*time.Second,
Expand Down Expand Up @@ -206,8 +226,14 @@ func (f *Flags) ToManagerOptions(options manager.Options) manager.Options {
if changed("leader-election-namespace") || options.LeaderElectionNamespace == "" {
options.LeaderElectionNamespace = f.LeaderElectionNamespace
}
if options.LeaderElectionResourceLock == "" {
options.LeaderElectionResourceLock = resourcelock.ConfigMapsLeasesResourceLock
if changed("leader-elect-lease-duration") || options.LeaseDuration == nil {
options.LeaseDuration = &f.LeaseDuration
}
if changed("leader-elect-renew-deadline") || options.RenewDeadline == nil {
options.RenewDeadline = &f.RenewDeadline
}
if changed("leader-elect-resource-lock") || options.LeaderElectionResourceLock == "" {
options.LeaderElectionResourceLock = f.LeaderElectionResourceLock
}
if changed("graceful-shutdown-timeout") || options.GracefulShutdownTimeout == nil {
options.GracefulShutdownTimeout = &f.GracefulShutdownTimeout
Expand Down

0 comments on commit 2f54287

Please sign in to comment.