lockheed

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 16 Imported by: 0

README

Lockheed

Go locking library for kubernetes based locking

Overview

In one of the projects required locking of some actions in scope of a namespace, hence this library sprung to life.

Features

  • Keeps the state of the lock within a per lock kubernetes ConfigMap object
  • Implements interface based abstraction of Locker to enable future implementation of other locking backends then kubernetes
  • Acquiring and releasing of a mutex lock
  • Maintaining a lock with moving time window based on duration and refresh interval
  • Dynamic tagging locks
  • Listing all locks with filtering based on Conditions
  • Forcefull takeover of locks based on Conditions
  • (planned) Shared locks where some lock instances can exist in parallel while others might wait for the lock to be freed or lockable in mutex mode

Configuration directives

Any lock object is instantiated in an inactive state. Untill activated by calling lock.Acquire() it can be further configured with following methods, all of which return back (the same) lock object pointer, to allow chaining their invocation like lock.DirectiveA().DirectiveB().

  • .WithDuration(time.Duration) A duration for whitch to establish or renew the lock every time, 0 = infinite
  • .WithRenewInterval(time.Duration) How often to renew the lock, no renewal if not specified
  • .WithContext(context.Context) Use this custom context within the lock
  • .WithTags([]string) Add these tags to the lock state if not already there
  • .WithResetTags() Remove any tags that were not specified withing .WithTags() directive
  • .WithForce(Condition) Allow forcefull takeover of a lock if it matches specified condition

Examples

lock := lockheed.NewLock("lockname", lockheed.NewKubeLocker()).
    WithDuration(30 * time.Second).
    WithRenewInterval(9 * time.Second)
lock.Acquire()
defer lock.Release()
lock := lockheed.NewLock("lockname", lockheed.NewKubeLocker()).
    WithDuration(30 * time.Second).
    WithRenewInterval(9 * time.Second).
    WithTags([]string{"tag1", "tag2"})
lock.Acquire()
defer lock.Release()
lock := lockheed.NewLock("lockname", lockheed.NewKubeLocker()).
    WithDuration(30 * time.Second).
    WithRenewInterval(9 * time.Second).
    WithResetTags().
    WithForce(lockheed.Condition{
        Operation: lockheed.OperationContains,
        Field:     lockheed.FieldTags,
        Value:     "tag2",
    })
lock.Acquire()
defer lock.Release()

Kubelocker

Kubelocker stores lock state in ConfigMap objects of it's designated namespace. ConfigMaps are named as lockheed-<lockname>. The program implementing this library needs respective RBAC rules allowing ConfigMap manipulation.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultEventHandler

func DefaultEventHandler(ctx context.Context, echan chan Event)

func GetKubeConfig

func GetKubeConfig() *rest.Config

Types

type AcquireOption added in v0.5.3

type AcquireOption func(opts *AcquireOptions) error

func AcquireOptionWithRetry added in v0.5.3

func AcquireOptionWithRetry(rl *retry.RetryLogic) AcquireOption

type AcquireOptions added in v0.5.3

type AcquireOptions struct {
	RetryLogic *retry.RetryLogic
}

type Condition

type Condition struct {
	Operation  Operation
	Conditions *[]Condition
	Field      Field
	Value      interface{}
}

type Event

type Event struct {
	Code    int
	Message string
	Err     error
}

type Field

type Field string
const (
	FieldAcquired Field = "acquired"
	FieldTags     Field = "tags"
)

type KubeLocker

type KubeLocker struct {
	Clientset *kubernetes.Clientset
	Namespace string
	Prefix    string
}

func NewKubeLocker

func NewKubeLocker(cset *kubernetes.Clientset, namespace string) *KubeLocker

func (*KubeLocker) Acquire

func (locker *KubeLocker) Acquire(l *Lock) error

func (*KubeLocker) ConfigMapExists

func (locker *KubeLocker) ConfigMapExists(l *Lock) (bool, error)

func (*KubeLocker) CreateNewConfigMap

func (locker *KubeLocker) CreateNewConfigMap(l *Lock) error

func (*KubeLocker) GetAllLocks

func (locker *KubeLocker) GetAllLocks() ([]*Lock, error)

func (*KubeLocker) GetConfigMap

func (locker *KubeLocker) GetConfigMap(l *Lock) (*corev1.ConfigMap, error)

func (*KubeLocker) GetConfigMapName

func (locker *KubeLocker) GetConfigMapName(l *Lock) string

func (*KubeLocker) GetReservedConfigMap

func (locker *KubeLocker) GetReservedConfigMap(l *Lock) (*corev1.ConfigMap, error)

func (*KubeLocker) GetReservedConfigMapAttempt added in v0.5.0

func (locker *KubeLocker) GetReservedConfigMapAttempt(l *Lock) (*corev1.ConfigMap, error)

func (*KubeLocker) Init

func (locker *KubeLocker) Init(l *Lock) error

func (*KubeLocker) Release

func (locker *KubeLocker) Release(l *Lock) error

func (*KubeLocker) ReleaseConfigMap

func (locker *KubeLocker) ReleaseConfigMap(l *Lock) error

TODO: look at potential corner-cases

func (*KubeLocker) Renew

func (locker *KubeLocker) Renew(l *Lock) error

func (*KubeLocker) UpdateAndReleaseConfigMap

func (locker *KubeLocker) UpdateAndReleaseConfigMap(ctx context.Context, cmap *corev1.ConfigMap) error

type Lock

type Lock struct {
	Name       string               `json:"name"`
	LockType   LockType             `json:"lockType"`
	Leases     map[string]LockLease `json:"leases"`
	InstanceID string               `json:"-"`
	Context    context.Context      `json:"-"`
	Cancel     func()               `json:"-"`
	Locker     LockerInterface      `json:"-"`
	Options
	// contains filtered or unexported fields
}

func GetLocks

func GetLocks(locker LockerInterface, c *Condition) ([]*Lock, error)

func NewLock

func NewLock(name string, locker LockerInterface) *Lock

func (*Lock) Acquire

func (l *Lock) Acquire(opts ...AcquireOption) error

func (*Lock) AcquireRetry

func (l *Lock) AcquireRetry(retries int, delay time.Duration) error

func (*Lock) Emit

func (l *Lock) Emit(e Event) error

func (*Lock) EmitAcquireFailed

func (l *Lock) EmitAcquireFailed(err error)

func (*Lock) EmitAcquireSuccessful

func (l *Lock) EmitAcquireSuccessful()

func (*Lock) EmitDebug

func (l *Lock) EmitDebug(msg string)

func (*Lock) EmitMaintainStarted

func (l *Lock) EmitMaintainStarted()

func (*Lock) EmitMaintainStopped

func (l *Lock) EmitMaintainStopped()

func (*Lock) EmitReleaseFailed

func (l *Lock) EmitReleaseFailed(err error)

func (*Lock) EmitReleaseSuccessful

func (l *Lock) EmitReleaseSuccessful()

func (*Lock) EmitRenewFailed

func (l *Lock) EmitRenewFailed(err error)

func (*Lock) EmitRenewSuccessful

func (l *Lock) EmitRenewSuccessful()

func (*Lock) Evaluate

func (l *Lock) Evaluate(c *Condition) (bool, error)

func (*Lock) EvaluateSubconditions

func (l *Lock) EvaluateSubconditions(c *Condition) (bool, error)

func (*Lock) Init

func (l *Lock) Init()

func (*Lock) Maintain

func (l *Lock) Maintain()

func (*Lock) NewExpiryTime

func (l *Lock) NewExpiryTime() time.Time

func (*Lock) Release

func (l *Lock) Release() error

func (*Lock) Renew

func (l *Lock) Renew() error

func (*Lock) WithContext

func (l *Lock) WithContext(ctx context.Context) *Lock

func (*Lock) WithDuration

func (l *Lock) WithDuration(duration time.Duration) *Lock

func (*Lock) WithForce

func (l *Lock) WithForce(c Condition) *Lock

func (*Lock) WithRenewInterval

func (l *Lock) WithRenewInterval(interval time.Duration) *Lock

func (*Lock) WithResetTags

func (l *Lock) WithResetTags() *Lock

func (*Lock) WithTags

func (l *Lock) WithTags(tags []string) *Lock

type LockLease

type LockLease struct {
	InstanceID string    `json:"instanceID"`
	Expires    time.Time `json:"expires"`
}

func (*LockLease) Expired

func (lease *LockLease) Expired() bool

type LockType

type LockType string
const (
	LockTypeMutex LockType = "mutex"
)

type LockerInterface

type LockerInterface interface {
	Acquire(*Lock) error
	Renew(*Lock) error
	Release(*Lock) error
	// List all locks, this locker has access to
	GetAllLocks() ([]*Lock, error)
}

type Operation

type Operation string
const (
	OperationContains Operation = "contains"
	OperationEquals   Operation = "equals"
	OperationAnd      Operation = "and"
	OperationOr       Operation = "or"
)

type Options

type Options struct {
	Tags          []string      `json:"tags,omitempty"`
	Duration      time.Duration `json:"-"`
	RenewInterval time.Duration `json:"-"`
	MaxLeases     *int          `json:"-"`
	Takeover      *bool         `json:"-"`
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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