k8s

package
v1.9.4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package k8s provides a Kubernetes-native cluster.Store implementation.

Worker discovery uses Pod annotations with a configurable label selector. Leader election uses the coordination/v1 Lease API (standard K8s pattern).

Example:

client := kubernetes.NewForConfigOrDie(rest.InClusterConfig())
provider := k8s.New(client, "my-namespace")
// Use provider as a cluster.Store

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Provider)

Option configures a Provider.

func WithAnnotationPrefix

func WithAnnotationPrefix(prefix string) Option

WithAnnotationPrefix sets the prefix for worker-data annotations on Pods. Default: "dispatch.xraph.com/".

func WithLabelSelector

func WithLabelSelector(sel string) Option

WithLabelSelector overrides the label selector used to discover worker Pods. Default: "app.kubernetes.io/component=dispatch-worker".

func WithLeaseName

func WithLeaseName(name string) Option

WithLeaseName sets the Lease object name used for leader election. Default: "dispatch-leader".

func WithLogger

func WithLogger(l log.Logger) Option

WithLogger sets a custom logger.

type Provider

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

Provider implements cluster.Store using Kubernetes primitives:

  • Worker discovery via Pod annotations and label selectors
  • Leader election via the coordination/v1 Lease API

func New

func New(client kubernetes.Interface, namespace string, opts ...Option) *Provider

New creates a Kubernetes cluster provider. The clientset and namespace are required. Use functional options to customise the lease name, label selector, annotation prefix, or logger.

func (*Provider) AcquireLeadership

func (p *Provider) AcquireLeadership(ctx context.Context, workerID id.WorkerID, ttl time.Duration) (bool, error)

AcquireLeadership attempts to become the cluster leader using the coordination/v1 Lease API.

func (*Provider) DeleteStaleWorkers

func (p *Provider) DeleteStaleWorkers(ctx context.Context, threshold time.Duration) (int64, error)

DeleteStaleWorkers removes dispatch annotations from pods whose last-seen is older than threshold. The pods themselves are owned by Kubernetes and are not deleted here — only the dispatch metadata is cleared, so a fresh instance can re-register without colliding with stale leadership state.

func (*Provider) DeregisterWorker

func (p *Provider) DeregisterWorker(ctx context.Context, workerID id.WorkerID) error

DeregisterWorker removes dispatch annotations from the worker's Pod.

func (*Provider) GetLeader

func (p *Provider) GetLeader(ctx context.Context) (*cluster.Worker, error)

GetLeader returns the current cluster leader from the Lease, or nil if there is no active leader.

func (*Provider) GetWorker

func (p *Provider) GetWorker(ctx context.Context, workerID id.WorkerID) (*cluster.Worker, error)

GetWorker returns the worker whose Pod carries the given worker-id annotation.

func (*Provider) HeartbeatWorker

func (p *Provider) HeartbeatWorker(ctx context.Context, workerID id.WorkerID) error

HeartbeatWorker updates the last-seen annotation on the worker's Pod.

func (*Provider) ListWorkers

func (p *Provider) ListWorkers(ctx context.Context) ([]*cluster.Worker, error)

ListWorkers returns all registered dispatch workers by scanning Pod annotations.

func (*Provider) ReapDeadWorkers

func (p *Provider) ReapDeadWorkers(ctx context.Context, threshold time.Duration) ([]*cluster.Worker, error)

ReapDeadWorkers returns workers whose last-seen annotation is older than the given threshold.

func (*Provider) RegisterWorker

func (p *Provider) RegisterWorker(ctx context.Context, w *cluster.Worker) error

RegisterWorker stores worker metadata as annotations on the worker's Pod. The Pod is located by matching the worker's Hostname to the Pod name.

func (*Provider) RenewLeadership

func (p *Provider) RenewLeadership(ctx context.Context, workerID id.WorkerID, ttl time.Duration) (bool, error)

RenewLeadership extends the leader's hold by updating the Lease.

Jump to

Keyboard shortcuts

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