Documentation
¶
Overview ¶
Package leader provides a thin Kubernetes Lease-based leader election wrapper. It is used by `serve` to ensure only one replica fires side effects (scheduled scans, controller reconciliation, outbound webhook dispatch) at a time when fleetsweeper is deployed with replicaCount > 1 against a shared Postgres backend.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsInCluster ¶
func IsInCluster() bool
IsInCluster reports whether the process appears to be running inside a Kubernetes pod. Leader election uses Kubernetes Leases, so it is a no-op outside the cluster and should be skipped.
func Run ¶
Run blocks executing leader election until ctx is canceled, building the coordination client from in-cluster config. On success the supplied callbacks fire as documented.
func RunWithClient ¶
func RunWithClient(ctx context.Context, cfg Config, cb Callbacks, coordination coordinationv1client.CoordinationV1Interface) error
RunWithClient blocks executing leader election against the supplied coordination client until ctx is canceled. Split from Run so tests can inject a fake clientset.
Types ¶
type Callbacks ¶
type Callbacks struct {
// OnStartedLeading is called once when this replica becomes leader.
// The context expires when leadership is lost so the work can cancel.
OnStartedLeading func(ctx context.Context)
// OnStoppedLeading is called when leadership is voluntarily released
// or revoked.
OnStoppedLeading func()
// OnNewLeader is called whenever a leader change is observed,
// including when this replica becomes leader. identity is the new
// leader's identity string.
OnNewLeader func(identity string)
}
Callbacks define what happens when leadership state transitions.
type Config ¶
type Config struct {
// Namespace is where the Lease object lives. Required; usually the
// deployment's own namespace, available as the POD_NAMESPACE env var.
Namespace string
// Name is the Lease object name. Defaults to "fleetsweeper" when empty.
Name string
// Identity uniquely identifies this replica. Usually the pod name from
// the POD_NAME env var. Defaults to the hostname when empty.
Identity string
// LeaseDuration is the lease validity window. Default 30s.
LeaseDuration time.Duration
// RenewDeadline is how long the leader has to renew before losing
// leadership. Default 20s.
RenewDeadline time.Duration
// RetryPeriod is how often non-leaders attempt acquisition. Default 5s.
RetryPeriod time.Duration
// Log is the structured logger.
Log *zap.Logger
}
Config configures a leader election session.