k8sresourcelock

package
v0.0.0-...-741fd3f Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader"
	EndpointsResourceLock             = "endpoints"
	ConfigMapsResourceLock            = "configmaps"
	LeasesResourceLock                = "leases"
	EndpointsLeasesResourceLock       = "endpointsleases"
	ConfigMapsLeasesResourceLock      = "configmapsleases"
)
View Source
const (
	UnknownLeader = "leaderelection.k8s.io/unknown"
)

Variables

This section is empty.

Functions

func ConcatRawRecord

func ConcatRawRecord(primaryRaw, secondaryRaw []byte) []byte

func LeaderElectionRecordToLeaseSpec

func LeaderElectionRecordToLeaseSpec(ler *LeaderElectionRecord) coordinationv1.LeaseSpec

Types

type ConfigMapLock

type ConfigMapLock struct {
	// ConfigMapMeta should contain a Name and a Namespace of a
	// ConfigMapMeta object that the LeaderElector will attempt to lead.
	ConfigMapMeta metav1.ObjectMeta
	Client        corev1client.ConfigMapsGetter
	LockConfig    ResourceLockConfig
	// contains filtered or unexported fields
}

func (*ConfigMapLock) Create

func (cml *ConfigMapLock) Create(ctx context.Context, ler LeaderElectionRecord) error

Create attempts to create a LeaderElectionRecord annotation

func (*ConfigMapLock) Describe

func (cml *ConfigMapLock) Describe() string

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

func (*ConfigMapLock) Get

Get returns the election record from a ConfigMap Annotation

func (*ConfigMapLock) Identity

func (cml *ConfigMapLock) Identity() string

Identity returns the Identity of the lock

func (*ConfigMapLock) Key

func (cml *ConfigMapLock) Key() string

Identity returns the Identity of the lock

func (*ConfigMapLock) RecordEvent

func (cml *ConfigMapLock) RecordEvent(s string)

RecordEvent in leader election while adding meta-data

func (*ConfigMapLock) Update

func (cml *ConfigMapLock) Update(ctx context.Context, ler LeaderElectionRecord) error

Update will update an existing annotation on a given resource.

type EndpointsLock

type EndpointsLock struct {
	// EndpointsMeta should contain a Name and a Namespace of an
	// Endpoints object that the LeaderElector will attempt to lead.
	EndpointsMeta metav1.ObjectMeta
	Client        corev1client.EndpointsGetter
	LockConfig    ResourceLockConfig
	// contains filtered or unexported fields
}

func (*EndpointsLock) Create

Create attempts to create a LeaderElectionRecord annotation

func (*EndpointsLock) Describe

func (el *EndpointsLock) Describe() string

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

func (*EndpointsLock) Get

Get returns the election record from a Endpoints Annotation

func (*EndpointsLock) Identity

func (el *EndpointsLock) Identity() string

Identity returns the Identity of the lock

func (*EndpointsLock) Key

func (el *EndpointsLock) Key() string

Key returns the Key of the lock

func (*EndpointsLock) RecordEvent

func (el *EndpointsLock) RecordEvent(s string)

RecordEvent in leader election while adding meta-data

func (*EndpointsLock) Update

Update will update and existing annotation on a given resource.

type EventRecorder

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

EventRecorder records a change in the ResourceLock.

type Interface

type Interface interface {
	// Get returns the LeaderElectionRecord
	Get(ctx context.Context) (*LeaderElectionRecord, []byte, error)

	// Create attempts to create a LeaderElectionRecord
	Create(ctx context.Context, ler LeaderElectionRecord) error

	// Update will update and existing LeaderElectionRecord
	Update(ctx context.Context, ler LeaderElectionRecord) error

	// RecordEvent is used to record events
	RecordEvent(string)

	// Identity will return the locks Identity
	Identity() string
	Key() string

	// Describe is used to convert details on current resource lock
	// into a string
	Describe() string
}

Interface offers a common interface for locking on arbitrary resources used in leader election. The Interface is used to hide the details on specific implementations in order to allow them to change over time. This interface is strictly for use by the leaderelection code.

func New

func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig) (Interface, error)

Manufacture will create a lock of a given type according to the input parameters nolint: lll

func NewFromKubeconfig

func NewFromKubeconfig(lockType string, ns string, name string, rlc ResourceLockConfig, kubeconfig *restclient.Config, renewDeadline time.Duration) (Interface, 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. nolint: lll

type LeaderElectionRecord

type LeaderElectionRecord struct {
	// HolderIdentity is the ID that owns the lease. If empty, no one owns this lease and
	// all callers may acquire. Versions of this library prior to Kubernetes 1.14 will not
	// attempt to acquire leases with empty identities and will wait for the full lease
	// interval to expire before attempting to reacquire. This value is set to empty when
	// a client voluntarily steps down.
	HolderIdentity string `json:"holderIdentity"`
	// HolderKey is the Key of the lease owner. This may be empty if a key is not set.
	HolderKey            string      `json:"holderKey"`
	LeaseDurationSeconds int         `json:"leaseDurationSeconds"`
	AcquireTime          metav1.Time `json:"acquireTime"`
	RenewTime            metav1.Time `json:"renewTime"`
	LeaderTransitions    int         `json:"leaderTransitions"`
}

LeaderElectionRecord is the record that is stored in the leader election annotation. This information should be used for observational purposes only and could be replaced with a random string (e.g. UUID) with only slight modification of this code. TODO(mikedanese): this should potentially be versioned

func LeaseSpecToLeaderElectionRecord

func LeaseSpecToLeaderElectionRecord(spec *coordinationv1.LeaseSpec) *LeaderElectionRecord

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 ResourceLockConfig
	// contains filtered or unexported fields
}

func (*LeaseLock) Create

func (ll *LeaseLock) Create(ctx context.Context, ler LeaderElectionRecord) 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

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) Key

func (ll *LeaseLock) Key() string

Key returns the Key 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 LeaderElectionRecord) error

Update will update an existing Lease spec.

type MultiLock

type MultiLock struct {
	Primary   Interface
	Secondary Interface
}

MultiLock is used for lock's migration

func (*MultiLock) Create

func (ml *MultiLock) Create(ctx context.Context, ler LeaderElectionRecord) error

Create attempts to create both primary lock and secondary lock

func (*MultiLock) Describe

func (ml *MultiLock) Describe() string

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

func (*MultiLock) Get

Get returns the older election record of the lock

func (*MultiLock) Identity

func (ml *MultiLock) Identity() string

Identity returns the Identity of the lock

func (*MultiLock) Key

func (ml *MultiLock) Key() string

Key returns the Key of the lock

func (*MultiLock) RecordEvent

func (ml *MultiLock) RecordEvent(s string)

RecordEvent in leader election while adding meta-data

func (*MultiLock) Update

func (ml *MultiLock) Update(ctx context.Context, ler LeaderElectionRecord) error

Update will update and existing annotation on both two resources.

type ResourceLockConfig

type ResourceLockConfig struct {
	// Identity is the unique string identifying a lease holder across
	// all participants in an election.
	Identity string
	// Key is a user-defined value used to indicate how high priority this lock
	// have. Other locks may steal the lock from us if they believe their key
	// has a higher priority.
	Key string
	// EventRecorder is optional.
	EventRecorder EventRecorder
}

ResourceLockConfig common data that exists across different resource locks

Jump to

Keyboard shortcuts

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