k8s

package module
v0.0.0-...-706d37a Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

Kubernetes backend for leader election

Go Reference

Usage

package main

import (
	"context"
	"os"
	"path/filepath"
	"time"

	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/util/rand"
	"k8s.io/client-go/tools/clientcmd"

	le "go.linka.cloud/leaderelection"
	"go.linka.cloud/leaderelection/k8s"
)

const (
	lockName = "test"
)

func main() {
	ctx, cancel := context.WithCancel(le.SetupSignalHandler())
	defer cancel()

	name := rand.String(8)
	path := os.Getenv("KUBECONFIG")
	if path == "" {
		path = filepath.Join(os.Getenv("HOME"), ".kube", "config")
	}
	cfg, err := clientcmd.BuildConfigFromFlags("", path)
	if err != nil {
		logrus.Fatal(err)
	}
	rlc := k8s.Config{
		Identity: name,
	}
	l, err := k8s.NewFromKubeconfig("default", lockName, rlc, cfg, 10*time.Second)
	if err != nil {
		logrus.Fatal(err)
	}
	config := le.Config{
		Lock:            l,
		LeaseDuration:   15 * time.Second,
		RenewDeadline:   10 * time.Second,
		RetryPeriod:     2 * time.Second,
		ReleaseOnCancel: true,
		Name:            lockName,
		Callbacks: le.Callbacks{
			OnStartedLeading: func(ctx context.Context) {
				logrus.Info("started leading")
			},
			OnStoppedLeading: func() {
				logrus.Info("stopped leading")
			},
			OnNewLeader: func(identity string) {
				logrus.Infof("new leader: %s", identity)
			},
		},
	}
	e, err := le.New(config)
	if err != nil {
		logrus.Fatal(err)
	}
	e.Run(ctx)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LeaderElectionRecordToLeaseSpec

func LeaderElectionRecordToLeaseSpec(ler *le.Record) coordinationv1.LeaseSpec

func LeaseSpecToLeaderElectionRecord

func LeaseSpecToLeaderElectionRecord(spec *coordinationv1.LeaseSpec) *le.Record

func New

func New(ns string, name string, coordinationClient coordinationv1client.CoordinationV1Interface, rlc Config) (le.Lock, error)

New will create a lock of a given type according to the input parameters

func NewFromKubeconfig

func NewFromKubeconfig(ns string, name string, rlc Config, kubeconfig *restclient.Config, renewDeadline time.Duration) (le.Lock, error)

NewFromKubeconfig will create a lock of a given type according to the input parameters. Timeout set for a client used to contact to Kubernetes should be lower than RenewDeadline to keep a single hung request from forcing a leader loss. Setting it to max(time.Second, RenewDeadline/2) as a reasonable heuristic.

Types

type Config

type Config struct {
	// Identity is the unique string identifying a lease holder across
	// all participants in an election.
	Identity string
	// EventRecorder is optional.
	EventRecorder EventRecorder
}

Config common data that exists across different resource locks

type EventRecorder

type EventRecorder interface {
	Eventf(obj runtime.Object, eventType, reason, message string, args ...interface{})
}

EventRecorder records a change in the ResourceLock.

type LeaseLock

type LeaseLock struct {
	// LeaseMeta should contain a Name and a Namespace of a
	// LeaseMeta object that the LeaderElector will attempt to lead.
	LeaseMeta  metav1.ObjectMeta
	Client     coordinationv1client.LeasesGetter
	LockConfig Config
	// contains filtered or unexported fields
}

func (*LeaseLock) Create

func (ll *LeaseLock) Create(ctx context.Context, ler le.Record) error

Create attempts to create a Lease

func (*LeaseLock) Describe

func (ll *LeaseLock) Describe() string

Describe is used to convert details on current resource lock into a string

func (*LeaseLock) Get

func (ll *LeaseLock) Get(ctx context.Context) (*le.Record, []byte, error)

Get returns the election record from a Lease spec

func (*LeaseLock) Identity

func (ll *LeaseLock) Identity() string

Identity returns the Identity of the lock

func (*LeaseLock) RecordEvent

func (ll *LeaseLock) RecordEvent(s string)

RecordEvent in leader election while adding meta-data

func (*LeaseLock) Update

func (ll *LeaseLock) Update(ctx context.Context, ler le.Record) error

Update will update an existing Lease spec.

Jump to

Keyboard shortcuts

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