factory

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const DefaultQueueKey = "key"

DefaultQueueKey is the queue key used for string trigger based controllers.

Variables

This section is empty.

Functions

func DefaultQueueKeysFunc

func DefaultQueueKeysFunc(_ runtime.Object) []string

DefaultQueueKeysFunc returns a slice with a single element - the DefaultQueueKey

Types

type Controller

type Controller interface {
	// Run runs the controller and blocks until the controller is finished.
	// Number of workers can be specified via workers parameter.
	// This function will return when all internal loops are finished.
	// Note that having more than one worker usually means handing parallelization of Sync().
	Run(ctx context.Context, workers int)

	// Sync contain the main controller logic.
	// This should not be called directly, but can be used in unit tests to exercise the sync.
	Sync(ctx context.Context, controllerContext SyncContext, key string) error

	// Name returns the controller name string.
	Name() string

	// SyncContext returns the SyncContext of this controller
	SyncContext() SyncContext
}

Controller interface represents a runnable Kubernetes controller. Cancelling the syncContext passed will cause the controller to shutdown. Number of workers determine how much parallel the job processing should be.

type EventFilterFunc

type EventFilterFunc func(obj interface{}) bool

EventFilterFunc is used to filter informer events to prevent Sync() from being called

type Factory

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

Factory is generator that generate standard Kubernetes controllers. Factory is really generic and should be only used for simple controllers that does not require special stuff..

func New

func New() *Factory

New return new factory instance.

func (*Factory) ResyncEvery

func (f *Factory) ResyncEvery(interval time.Duration) *Factory

ResyncEvery will cause the Sync() function to be called periodically, regardless of informers. This is useful when you want to refresh every N minutes or you fear that your informers can be stucked. If this is not called, no periodical resync will happen. Note: The controller context passed to Sync() function in this case does not contain the object metadata or object itself.

This can be used to detect periodical resyncs, but normal Sync() have to be cautious about `nil` objects.

func (*Factory) ToController

func (f *Factory) ToController(name string) Controller

ToController produce a runnable controller.

func (*Factory) WithBareInformers

func (f *Factory) WithBareInformers(informers ...Informer) *Factory

WithBareInformers allow to register informer that already has custom event handlers registered and no additional event handlers will be added to this informer. The controller will wait for the cache of this informer to be synced. The existing event handlers will have to respect the queue key function or the sync() implementation will have to count with custom queue keys.

func (*Factory) WithFilteredEventsInformers

func (f *Factory) WithFilteredEventsInformers(filter EventFilterFunc, informers ...Informer) *Factory

WithFilteredEventsInformers is used to register event handlers and get the caches synchronized functions. Pass the informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function is called. Pass filter to filter out events that should not trigger Sync() call.

func (*Factory) WithFilteredEventsInformersQueueKeysFunc

func (f *Factory) WithFilteredEventsInformersQueueKeysFunc(queueKeyFn ObjectQueueKeysFunc, filter EventFilterFunc, informers ...Informer) *Factory

WithFilteredEventsInformersQueueKeysFunc is used to register event handlers and get the caches synchronized functions. Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function is called. Pass the queueKeyFn you want to use to transform the informer runtime.Object into string key used by work queue. Pass filter to filter out events that should not trigger Sync() call.

func (*Factory) WithInformers

func (f *Factory) WithInformers(informers ...Informer) *Factory

WithInformers is used to register event handlers and get the caches synchronized functions. Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function is called.

func (*Factory) WithInformersQueueKeysFunc

func (f *Factory) WithInformersQueueKeysFunc(queueKeyFn ObjectQueueKeysFunc, informers ...Informer) *Factory

WithInformersQueueKeysFunc is used to register event handlers and get the caches synchronized functions. Pass informers you want to use to react to changes on resources. If informer event is observed, then the Sync() function is called. Pass the queueKeyFn you want to use to transform the informer runtime.Object into string key used by work queue.

func (*Factory) WithSync

func (f *Factory) WithSync(syncFn SyncFunc) *Factory

WithSync is used to set the controller synchronization function. This function is the core of the controller and is usually hold the main controller logic.

func (*Factory) WithSyncContext

func (f *Factory) WithSyncContext(ctx SyncContext) *Factory

WithSyncContext allows to specify custom, existing sync context for this factory. This is useful during unit testing where you can override the default event recorder or mock the runtime objects. If this function not called, a SyncContext is created by the factory automatically.

type Informer

type Informer interface {
	AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error)
	HasSynced() bool
}

Informer represents any structure that allow to register event handlers and informs if caches are synced. Any SharedInformer will comply.

type ObjectQueueKeysFunc

type ObjectQueueKeysFunc func(runtime.Object) []string

ObjectQueueKeysFunc is used to make a string work queue keys out of the runtime object that is passed to it. This can extract the "namespace/name" if you need to or just return "key" if you building controller that only use string triggers.

type SyncContext

type SyncContext interface {
	// Queue gives access to controller queue. This can be used for manual requeue, although if a Sync() function return
	// an error, the object is automatically re-queued. Use with caution.
	Queue() workqueue.RateLimitingInterface
}

SyncContext interface represents a context given to the Sync() function where the main controller logic happen. SyncContext exposes controller name and give user access to the queue (for manual requeue). SyncContext also provides metadata about object that informers observed as changed.

func NewSyncContext

func NewSyncContext(name string) SyncContext

NewSyncContext gives new sync context.

type SyncFunc

type SyncFunc func(ctx context.Context, controllerContext SyncContext, key string) error

SyncFunc is a function that contain main controller logic. The syncContext.syncContext passed is the main controller syncContext, when cancelled it means the controller is being shut down. The syncContext provides access to controller name, queue and event recorder.

Jump to

Keyboard shortcuts

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