controller

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: Apache-2.0 Imports: 14 Imported by: 78

Documentation

Overview

Package controller provide a simple pattern for async operations that require retries and/or regular intervals.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGlobalStatus

func GetGlobalStatus() models.ControllerStatuses

GetGlobalStatus returns the status of all controllers

func NoopFunc

func NoopFunc(ctx context.Context) error

NoopFunc is a no-op placeholder for DoFunc & StopFunc. It is automatically used when StopFunc is undefined, and can be used as a DoFunc stub when the controller should only run StopFunc.

Types

type Controller added in v1.5.0

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

Controller is a simple pattern that allows to perform the following tasks:

  • Run an operation in the background and retry until it succeeds
  • Perform a regular sync operation in the background

A controller has configurable retry intervals and will collect statistics on number of successful runs, number of failures, last error message, and last error timestamp.

Controllers have a name and are tied to a Manager. The manager is typically bound to higher level objects such as endpoint. These higher level objects can then run multiple controllers to perform async tasks such as:

  • Annotating k8s resources with values
  • Synchronizing an object with the kvstore
  • Any other async operation to may fail and require retries

Embedding the Manager into higher level resources allows to bind controllers to the lifetime of that object. Controllers also have a UUID to allow correlating all log messages of a controller instance.

Guidelines to writing controllers:

  • Make sure that the task the controller performs is done in an atomic fashion, e.g. if a controller modifies a resource in multiple steps, an intermediate manipulation operation failing should not leave behind an inconsistent state. This can typically be achieved by locking the resource and rolling back or by using transactions.
  • Controllers typically act on behalf of a higher level object such as an endpoint. The controller must ensure that the higher level object is properly locked when accessing any fields.
  • Controllers run asynchronously in the background, it is the responsibility of the controller to be aware of the lifecycle of the owning higher level object. This is typically achieved by removing all controllers when the owner dies. It is the responsibility of the owner to either lock the owner in a way that will delay destruction throughout the controller run or to check for the destruction throughout the run.

func (*Controller) GetFailureCount added in v1.5.0

func (c *Controller) GetFailureCount() int

GetFailureCount returns the number of failed controller runs

func (*Controller) GetLastError added in v1.5.0

func (c *Controller) GetLastError() error

GetLastError returns the last error returned

func (*Controller) GetLastErrorTimestamp added in v1.5.0

func (c *Controller) GetLastErrorTimestamp() time.Time

GetLastErrorTimestamp returns the last error returned

func (*Controller) GetStatusModel added in v1.5.0

func (c *Controller) GetStatusModel() *models.ControllerStatus

GetStatusModel returns a models.ControllerStatus representing the controller's configuration & status

func (*Controller) GetSuccessCount added in v1.5.0

func (c *Controller) GetSuccessCount() int

GetSuccessCount returns the number of successful controller runs

func (*Controller) Trigger added in v1.9.9

func (c *Controller) Trigger()

Trigger triggers the controller

type ControllerFunc

type ControllerFunc func(ctx context.Context) error

ControllerFunc is a function that the controller runs. This type is used for DoFunc and StopFunc.

type ControllerParams

type ControllerParams struct {
	// DoFunc is the function that will be run until it succeeds and/or
	// using the interval RunInterval if not 0.
	// An unset DoFunc is an error and will be logged as one.
	DoFunc ControllerFunc

	// CancelDoFuncOnUpdate when set to true cancels the controller context
	// (the DoFunc) to allow quick termination of controller
	CancelDoFuncOnUpdate bool

	// StopFunc is called when the controller stops. It is intended to run any
	// clean-up tasks for the controller (e.g. deallocate/release resources)
	// It is guaranteed that DoFunc is called at least once before StopFunc is
	// called.
	// An unset StopFunc is not an error (and will be a no-op)
	// Note: Since this occurs on controller exit, error counts and tracking may
	// not be checked after StopFunc is run.
	StopFunc ControllerFunc

	// If set to any other value than 0, will cause DoFunc to be run in the
	// specified interval. The interval starts from when the DoFunc has
	// returned last
	RunInterval time.