leaderelection

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 21 Imported by: 0

README

Build

Kubernetes Leader Election Library for Go

This library provides a thin wrapper for kubernetes leader election. It can be used to elect a leader between multiple replica deployments/daemonsets. Behind the scenes kubernetes lease objects are used to persist leader information.

go get github.com/edgedelta/leader-election

Example usage


import (
    ...

    "github.com/edgedelta/leader-election"
)

func main() {
  le, err := leaderelection.New(
    leaderelection.WithleaseDuration(15*time.Second),
    leaderelection.WithRenewTime(10*time.Second),
    leaderelection.WithRetryPeriod(2*time.Second),
    leaderelection.WithLeaseNamespace("custom-ns"))

  if err != nil {
    // Handle error
  }

  if err := le.Start(); err != nil {
    // Handle error
  }

  // run the leader election based logic
  ctx, cancel := context.WithCancel(context.Background())
  go run(le, ctx)

  // wait for termination signal
  termSignal := make(chan os.Signal, 1)
  signal.Notify(termSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  <-termSignal

  // stop leader election engine
  cancel()
  if err := le.Stop(); err != nil {
    // Handle error
  }
}

func run(le *leaderelection.K8sLeaderEngine, ctx context.Context) {
  // do leader stuff as long as le.IsLeader() and ctx is not Done
}

Check the lease holder

kubectl get lease -n custom-ns

Documentation

Index

Constants

View Source
const (
	ServiceAccountMountPath    = "/var/run/secrets/kubernetes.io/serviceaccount"
	DefaultKubernetesAPIServer = "https://kubernetes.default.svc"
)

Variables

View Source
var (
	GetAPIClient = func() (k8s.Interface, error) {
		config, err := rest.InClusterConfig()
		if err != nil {
			return nil, fmt.Errorf("failed to get in-cluster config, err: %v", err)
		}

		apiClient, err := k8s.NewForConfig(config)
		if err != nil {
			return nil, fmt.Errorf("failed to obtain K8s API client, err: %v", err)
		}
		return apiClient, nil
	}
	ErrNotRunning = fmt.Errorf("leader engine is not running")
)

Functions

This section is empty.

Types

type K8sLeaderEngine

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

func New

func (*K8sLeaderEngine) GetLeader

func (le *K8sLeaderEngine) GetLeader() string

func (*K8sLeaderEngine) IsLeader

func (le *K8sLeaderEngine) IsLeader() bool

func (*K8sLeaderEngine) Start

func (le *K8sLeaderEngine) Start() error

Start the leader engine and block until a leader is elected.

func (*K8sLeaderEngine) Stop

func (le *K8sLeaderEngine) Stop() error

Stop the engine and wait until the goroutines have stopped

type K8sLeaderEngineOption

type K8sLeaderEngineOption func(*K8sLeaderEngine)

func WithContext

func WithContext(ctx context.Context) K8sLeaderEngineOption

func WithErrorLogger

func WithErrorLogger(logger Logger) K8sLeaderEngineOption

func WithHolderIdentity

func WithHolderIdentity(identity string) K8sLeaderEngineOption

func WithLeaseDuration

func WithLeaseDuration(dur time.Duration) K8sLeaderEngineOption

func WithLeaseName

func WithLeaseName(name string) K8sLeaderEngineOption

func WithLeaseNamespace

func WithLeaseNamespace(namespace string) K8sLeaderEngineOption

func WithLogger

func WithLogger(logger Logger) K8sLeaderEngineOption

func WithRenewTime

func WithRenewTime(dur time.Duration) K8sLeaderEngineOption

func WithRetryPeriod

func WithRetryPeriod(dur time.Duration) K8sLeaderEngineOption

type Logger

type Logger interface {
	Log(string, ...interface{})
}

Jump to

Keyboard shortcuts

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