konductor

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

README

pkg/konductor

konductor wraps Kubernetes leader election so that only one Orkestra runtime pod processes reconcile events at a time. When a pod wins the election it becomes the "konductor" and its run function is called. When it loses the lease, the run context is cancelled and the controllers shut down cleanly.

ko := konductor.NewKonductorElection(kube, event, run, onElected, konductor.Options{
    Namespace:     kfg.Konductor().Namespace,
    LeaseDuration: kfg.Konductor().LeaseDuration,
    RenewDeadline: kfg.Konductor().RenewDeadline,
    RetryPeriod:   kfg.Konductor().RetryPeriod,
})

ko.Start(ctx)

How it works

KonductorElection uses client-go/tools/leaderelection backed by a coordination.k8s.io/v1 Lease object. Three callbacks drive the lifecycle:

Callback Triggered when Action
OnStartedLeading This pod wins the lease Calls run(ctx) with a cancellable context; records a Kubernetes event
OnStoppedLeading This pod loses the lease Cancels the run context; clears the konductor identity
OnNewLeader Any pod wins Logs the new leader identity

ReleaseOnCancel: true ensures the Lease is released when Shutdown() is called, allowing a new pod to immediately become konductor rather than waiting for the lease to expire.

Identity

Each pod identifies itself by hostname (os.Hostname()). If the hostname call fails, a UUID is generated as a fallback. The identity appears in Konductor() after election and in the emitted Kubernetes events.

Lean election timing

Configured via pkg/konfig ENV vars:

ENV Default Purpose
LEASE_DURATION 60s How long a lease is valid without renewal
RENEW_DEADLINE 40s How long the leader tries to renew before giving up
RETRY_PERIOD 10s How often non-leaders try to acquire the lease

For faster failover in testing, use the defaults in konfig.NewDefaultKonfig() (15s/10s/2s).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KonductorElection

type KonductorElection struct {
	// contains filtered or unexported fields
}

func NewKonductorElection

func NewKonductorElection(
	kube *kubeclient.Kubeclient,
	event *event.Event,
	run func(context.Context),
	onElected func(konductor string),
	opts Options,
) *KonductorElection

func (*KonductorElection) Konductor

func (ko *KonductorElection) Konductor() string

Konductor returns the instance that won the konductor election

func (*KonductorElection) Name

func (ko *KonductorElection) Name() string

func (*KonductorElection) Shutdown

func (ko *KonductorElection) Shutdown(ctx context.Context)

func (*KonductorElection) Start

func (ko *KonductorElection) Start(ctx context.Context) error

func (*KonductorElection) Started

func (ko *KonductorElection) Started() bool

type Options

type Options struct {
	LeaseDuration time.Duration
	RetryPeriod   time.Duration
	RenewDeadline time.Duration

	Namespace   string
	Labels      map[string]string
	Annotations map[string]string
}

Jump to

Keyboard shortcuts

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