leaderelection

package
v0.44.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2024 License: Apache-2.0 Imports: 4 Imported by: 23

Documentation

Overview

Package leaderelection provides runtime configuration options for leader election, making it easier to consistently have the same configuration options and flags across GitOps Toolkit components.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateID added in v0.35.0

func GenerateID(base string, additions ...string) string

GenerateID generates a unique leader election ID based on the given base and additional strings. The base is used as the base and suffixed with a short hash of the additional strings if provided. If the name is empty, an empty string is returned.

Types

type Options

type Options struct {
	// Enable determines whether or not to use leader election when starting the manager.
	Enable bool

	// ReleaseOnCancel defines if the leader should step down voluntarily when the Manager ends. This requires the
	// binary to immediately end when the Manager is stopped, otherwise this setting is unsafe. Setting this
	// significantly speeds up voluntary leader transitions as the new leader doesn't have to wait LeaseDuration time
	// first.
	ReleaseOnCancel bool

	// 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 35 seconds.
	LeaseDuration time.Duration

	// RenewDeadline is the duration that the acting controlplane will retry refreshing leadership before giving up.
	// Default is 30 seconds.
	RenewDeadline time.Duration

	// RetryPeriod is the duration the LeaderElector clients should wait between tries of actions. Default is 5 seconds.
	RetryPeriod time.Duration
}

Options contains the runtime configuration for leader election.

The struct can be used in the main.go file of your controller by binding it to the main flag set, and then utilizing the configured options later:

func main() {
	var (
		// other controller specific configuration variables
		leaderElectionOptions leaderelection.Options
	)

	// Bind the options to the main flag set, and parse it
	leaderElectionOptions.BindFlags(flag.CommandLine)
	flag.Parse()

	// Use the values during the initialisation of the manager
	mgr, err := ctrl.NewManager(cfg, ctrl.Options{
		...other options
		LeaderElection:                leaderElectionOptions.Enable,
		LeaderElectionReleaseOnCancel: leaderElectionOptions.ReleaseOnCancel,
		LeaseDuration:                 &leaderElectionOptions.LeaseDuration,
		RenewDeadline:                 &leaderElectionOptions.RenewDeadline,
		RetryPeriod:                   &leaderElectionOptions.RetryPeriod,
	})
}

func (*Options) BindFlags

func (o *Options) BindFlags(fs *pflag.FlagSet)

BindFlags will parse the given pflag.FlagSet for leader election option flags and set the Options accordingly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL