client-go: Index | Files | Directories

package leaderelection

import ""

Package leaderelection implements leader election of a set of endpoints. It uses an annotation in the endpoints object to store the record of the election state.

This implementation does not guarantee that only one client is acting as a leader (a.k.a. fencing). A client observes timestamps captured locally to infer the state of the leader election. Thus the implementation is tolerant to arbitrary clock skew, but is not tolerant to arbitrary clock skew rate.

However the level of tolerance to skew rate can be configured by setting RenewDeadline and LeaseDuration appropriately. The tolerance expressed as a maximum tolerated ratio of time passed on the fastest node to time passed on the slowest node can be approximately achieved with a configuration that sets the same ratio of LeaseDuration to RenewDeadline. For example if a user wanted to tolerate some nodes progressing forward in time twice as fast as other nodes, the user could set LeaseDuration to 60 seconds and RenewDeadline to 30 seconds.

While not required, some method of clock synchronization between nodes in the cluster is highly recommended. It's important to keep in mind when configuring this client that the tolerance to skew rate varies inversely to master availability.

Larger clusters often have a more lenient SLA for API latency. This should be taken into account when configuring the client. The rate of leader transitions should be monitored and RetryPeriod and LeaseDuration should be increased until the rate is stable and acceptably low. It's important to keep in mind when configuring this client that the tolerance to API latency varies inversely to master availability.

DISCLAIMER: this is an alpha API. This library will likely change significantly or even be removed entirely in subsequent releases. Depend on this API at your own risk.


Package Files

healthzadaptor.go leaderelection.go metrics.go


const (
    JitterFactor = 1.2

func RunOrDie Uses

func RunOrDie(ctx context.Context, lec LeaderElectionConfig)

RunOrDie starts a client with the provided config or panics if the config fails to validate.

func SetProvider Uses

func SetProvider(metricsProvider MetricsProvider)

SetProvider sets the metrics provider for all subsequently created work queues. Only the first call has an effect.

type HealthzAdaptor Uses

type HealthzAdaptor struct {
    // contains filtered or unexported fields

HealthzAdaptor associates the /healthz endpoint with the LeaderElection object. It helps deal with the /healthz endpoint being set up prior to the LeaderElection. This contains the code needed to act as an adaptor between the leader election code the health check code. It allows us to provide health status about the leader election. Most specifically about if the leader has failed to renew without exiting the process. In that case we should report not healthy and rely on the kubelet to take down the process.

func NewLeaderHealthzAdaptor Uses

func NewLeaderHealthzAdaptor(timeout time.Duration) *HealthzAdaptor

NewLeaderHealthzAdaptor creates a basic healthz adaptor to monitor a leader election. timeout determines the time beyond the lease expiry to be allowed for timeout. checks within the timeout period after the lease expires will still return healthy.

func (*HealthzAdaptor) Check Uses

func (l *HealthzAdaptor) Check(req *http.Request) error

Check is called by the healthz endpoint handler. It fails (returns an error) if we own the lease but had not been able to renew it.

func (*HealthzAdaptor) Name Uses

func (l *HealthzAdaptor) Name() string

Name returns the name of the health check we are implementing.

func (*HealthzAdaptor) SetLeaderElection Uses

func (l *HealthzAdaptor) SetLeaderElection(le *LeaderElector)

SetLeaderElection ties a leader election object to a HealthzAdaptor

type LeaderCallbacks Uses

type LeaderCallbacks struct {
    // OnStartedLeading is called when a LeaderElector client starts leading
    OnStartedLeading func(context.Context)
    // OnStoppedLeading is called when a LeaderElector client stops leading
    OnStoppedLeading func()
    // OnNewLeader is called when the client observes a leader that is
    // not the previously observed leader. This includes the first observed
    // leader when the client starts.
    OnNewLeader func(identity string)

LeaderCallbacks are callbacks that are triggered during certain lifecycle events of the LeaderElector. These are invoked asynchronously.

possible future callbacks:

* OnChallenge()

type LeaderElectionConfig Uses

type LeaderElectionConfig struct {
    // Lock is the resource that will be used for locking
    Lock rl.Interface

    // LeaseDuration is the duration that non-leader candidates will
    // wait to force acquire leadership. This is measured against time of
    // last observed ack.
    LeaseDuration time.Duration
    // RenewDeadline is the duration that the acting master will retry
    // refreshing leadership before giving up.
    RenewDeadline time.Duration
    // RetryPeriod is the duration the LeaderElector clients should wait
    // between tries of actions.
    RetryPeriod time.Duration

    // Callbacks are callbacks that are triggered during certain lifecycle
    // events of the LeaderElector
    Callbacks LeaderCallbacks

    // WatchDog is the associated health checker
    // WatchDog may be null if its not needed/configured.
    WatchDog *HealthzAdaptor

    // ReleaseOnCancel should be set true if the lock should be released
    // when the run context is cancelled. If you set this to true, you must
    // ensure all code guarded by this lease has successfully completed
    // prior to cancelling the context, or you may have two processes
    // simultaneously acting on the critical path.
    ReleaseOnCancel bool

    // Name is the name of the resource lock for debugging
    Name string

type LeaderElector Uses

type LeaderElector struct {
    // contains filtered or unexported fields

LeaderElector is a leader election client.

func NewLeaderElector Uses

func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error)

NewLeaderElector creates a LeaderElector from a LeaderElectionConfig

func (*LeaderElector) Check Uses

func (le *LeaderElector) Check(maxTolerableExpiredLease time.Duration) error

Check will determine if the current lease is expired by more than timeout.

func (*LeaderElector) GetLeader Uses

func (le *LeaderElector) GetLeader() string

GetLeader returns the identity of the last observed leader or returns the empty string if no leader has yet been observed.

func (*LeaderElector) IsLeader Uses

func (le *LeaderElector) IsLeader() bool

IsLeader returns true if the last observed leader was this client else returns false.

func (*LeaderElector) Run Uses

func (le *LeaderElector) Run(ctx context.Context)

Run starts the leader election loop

type MetricsProvider Uses

type MetricsProvider interface {
    NewLeaderMetric() SwitchMetric

MetricsProvider generates various metrics used by the leader election.

type SwitchMetric Uses

type SwitchMetric interface {
    On(name string)
    Off(name string)

GaugeMetric represents a single numerical value that can arbitrarily go up and down.



Package leaderelection imports 13 packages (graph) and is imported by 70 packages. Updated 2019-03-21. Refresh now. Tools for package owners.