Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Callbacks ¶
type Callbacks struct {
// OnStartedLeading is called when the instance becomes the leader
OnStartedLeading func(ctx context.Context)
// OnStoppedLeading is called when the instance stops being the leader
OnStoppedLeading func()
// OnNewLeader is called when a new leader is observed (may be self or another instance)
OnNewLeader func(identity string)
}
Callbacks contains callback functions for leader election events.
type Config ¶
type Config struct {
// Enabled determines if leader election is active
Enabled bool
// Identity is the unique identifier of this instance (usually pod name)
Identity string
// LeaseName is the name of the Lease resource
LeaseName string
// LeaseNamespace is the namespace of the Lease resource
LeaseNamespace string
// LeaseDuration is the duration that non-leader candidates will wait to force acquire leadership
LeaseDuration time.Duration
// RenewDeadline is the duration that the acting leader 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
// ReleaseOnCancel should be true to release leadership when the context is cancelled
ReleaseOnCancel bool
}
Config contains configuration for leader election.
type Elector ¶
type Elector struct {
// contains filtered or unexported fields
}
Elector manages leader election using Kubernetes Lease resources.
This is a pure component that wraps k8s.io/client-go/tools/leaderelection with a clean interface. It has no dependencies on the event bus or controller coordination logic.
func New ¶
func New( config *Config, clientset kubernetes.Interface, callbacks Callbacks, logger *slog.Logger, ) (*Elector, error)
New creates a new leader elector.
The elector is not started until Start() is called.
func (*Elector) KeepLeaseOnStop ¶
func (e *Elector) KeepLeaseOnStop()
KeepLeaseOnStop makes the next stop leave the Lease held instead of releasing it, so a successor elector with the same identity resumes leadership on its first acquire and no other replica sees a vacancy.
func (*Elector) ReleaseLease ¶
func (e *Elector) ReleaseLease()
ReleaseLease vacates the Lease if it still names this identity. It undoes KeepLeaseOnStop when no successor elector will run to resume the Lease.
func (*Elector) Start ¶
Start starts the leader election loop.
This function blocks until the context is cancelled or an error occurs. It should be run in a goroutine.
Lost-lease semantics: client-go's LeaderElector.Run returns permanently when an acquired lease is lost (renewal missed its deadline), after invoking OnStoppedLeading — it does NOT re-enter the acquire loop. In that case Start returns nil while the caller's context is still alive. Callers that need leadership to ever be re-acquired must call Start again (each call builds a fresh LeaderElector); the controller re-enters election in place without disturbing the rest of the process — see pkg/controller/leader.go's superviseElection.