generic

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: Apache-2.0 Imports: 23 Imported by: 182

Documentation

Overview

Package generic provides generic types and implementations for Controllers, Clients, and Caches.

Index

Constants

This section is empty.

Variables

ErrSkip notifies the caller to skip this error.

Functions

func ConfigureApplyForObject

func ConfigureApplyForObject(apply apply.Apply, obj metav1.Object, opts *GeneratingHandlerOptions) apply.Apply

Types

type Cache

type Cache[T runtime.Object] struct {
	// contains filtered or unexported fields
}

Cache is a object cache stored in memory for objects of type T.

func (*Cache[T]) AddIndexer

func (c *Cache[T]) AddIndexer(indexName string, indexer Indexer[T])

AddIndexer adds a new Indexer to the cache with the provided name. If you call this after you already have data in the store, the results are undefined.

func (*Cache[T]) Get

func (c *Cache[T]) Get(namespace, name string) (T, error)

Get returns the resources with the specified name in the given namespace from the cache.

func (*Cache[T]) GetByIndex

func (c *Cache[T]) GetByIndex(indexName, key string) (result []T, err error)

GetByIndex returns the stored objects whose set of indexed values for the named index includes the given indexed value.

func (*Cache[T]) List

func (c *Cache[T]) List(namespace string, selector labels.Selector) (ret []T, err error)

List will attempt to find resources in the given namespace from the Cache.

type CacheInterface

type CacheInterface[T runtime.Object] interface {
	// Get returns the resources with the specified name in the given namespace from the cache.
	Get(namespace, name string) (T, error)

	// List will attempt to find resources in the given namespace from the Cache.
	List(namespace string, selector labels.Selector) ([]T, error)

	// AddIndexer adds  a new Indexer to the cache with the provided name.
	// If you call this after you already have data in the store, the results are undefined.
	AddIndexer(indexName string, indexer Indexer[T])

	// GetByIndex returns the stored objects whose set of indexed values
	// for the named index includes the given indexed value.
	GetByIndex(indexName, key string) ([]T, error)
}

CacheInterface is an interface for Object retrieval from memory.

type ClientInterface

type ClientInterface[T RuntimeMetaObject, TList runtime.Object] interface {
	// Create creates a new object and return the newly created Object or an error.
	Create(T) (T, error)

	// Update updates the object and return the newly updated Object or an error.
	Update(T) (T, error)

	// UpdateStatus updates the Status field of a the object and return the newly updated Object or an error.
	// Will always return an error if the object does not have a status field.
	UpdateStatus(T) (T, error)

	// Delete deletes the Object in the given name and namespace.
	Delete(namespace, name string, options *metav1.DeleteOptions) error

	// Get will attempt to retrieve the resource with the given name in the given namespace.
	Get(namespace, name string, options metav1.GetOptions) (T, error)

	// List will attempt to find resources in the given namespace.
	List(namespace string, opts metav1.ListOptions) (TList, error)

	// Watch will start watching resources in the given namespace.
	Watch(namespace string, opts metav1.ListOptions) (watch.Interface, error)

	// Patch will patch the resource with the matching name in the matching namespace.
	Patch(namespace, name string, pt types.PatchType, data []byte, subresources ...string) (result T, err error)

	// WithImpersonation returns a new copy of the client that uses impersonation.
	WithImpersonation(impersonate rest.ImpersonationConfig) (ClientInterface[T, TList], error)
}

ClientInterface is an interface to performs CRUD like operations on an Objects.

type Controller

type Controller[T RuntimeMetaObject, TList runtime.Object] struct {
	// contains filtered or unexported fields
}

Controller is used to manage objects of type T.

func NewController

func NewController[T RuntimeMetaObject, TList runtime.Object](gvk schema.GroupVersionKind, resource string, namespaced bool, controller controller.SharedControllerFactory) *Controller[T, TList]

NewController creates a new controller for the given Object type and ObjectList type.

func (*Controller[T, TList]) AddGenericHandler

func (c *Controller[T, TList]) AddGenericHandler(ctx context.Context, name string, handler Handler)

AddGenericHandler runs the given handler when the controller detects an object was changed.

func (*Controller[T, TList]) AddGenericRemoveHandler

func (c *Controller[T, TList]) AddGenericRemoveHandler(ctx context.Context, name string, handler Handler)

AddGenericRemoveHandler runs the given handler when the controller detects an object was removed.

func (*Controller[T, TList]) Cache

func (c *Controller[T, TList]) Cache() CacheInterface[T]

Cache returns a cache for the objects T.

func (*Controller[T, TList]) Create

func (c *Controller[T, TList]) Create(obj T) (T, error)

Create creates a new object and return the newly created Object or an error.

func (*Controller[T, TList]) Delete

func (c *Controller[T, TList]) Delete(namespace, name string, options *metav1.DeleteOptions) error

Delete deletes the Object in the given name and Namespace.

func (*Controller[T, TList]) Enqueue

func (c *Controller[T, TList]) Enqueue(namespace, name string)

Enqueue adds the resource with the given name in the provided namespace to the worker queue of the controller.

func (*Controller[T, TList]) EnqueueAfter

func (c *Controller[T, TList]) EnqueueAfter(namespace, name string, duration time.Duration)

EnqueueAfter runs Enqueue after the provided duration.

func (*Controller[T, TList]) Get

func (c *Controller[T, TList]) Get(namespace, name string, options metav1.GetOptions) (T, error)

Get gets returns the given resource with the given name in the provided namespace.

func (*Controller[T, TList]) GroupVersionKind

func (c *Controller[T, TList]) GroupVersionKind() schema.GroupVersionKind

GroupVersionKind returns the GVK used to create this Controller.

func (*Controller[T, TList]) Informer

func (c *Controller[T, TList]) Informer() cache.SharedIndexInformer

Informer returns the SharedIndexInformer used by this controller.

func (*Controller[T, TList]) List

func (c *Controller[T, TList]) List(namespace string, opts metav1.ListOptions) (TList, error)

List will attempt to find resources in the given namespace.

func (*Controller[T, TList]) OnChange

func (c *Controller[T, TList]) OnChange(ctx context.Context, name string, sync ObjectHandler[T])

OnChange runs the given object handler when the controller detects a resource was changed.

func (*Controller[T, TList]) OnRemove

func (c *Controller[T, TList]) OnRemove(ctx context.Context, name string, sync ObjectHandler[T])

OnRemove runs the given object handler when the controller detects a resource was changed.

func (*Controller[T, TList]) Patch

func (c *Controller[T, TList]) Patch(namespace, name string, pt types.PatchType, data []byte, subresources ...string) (T, error)

Patch will patch the resource with the matching name in the matching namespace.

func (*Controller[T, TList]) Update

func (c *Controller[T, TList]) Update(obj T) (T, error)

Update updates the object and return the newly updated Object or an error.

func (*Controller[T, TList]) UpdateStatus

func (c *Controller[T, TList]) UpdateStatus(obj T) (T, error)

UpdateStatus updates the Status field of a the object and return the newly updated Object or an error. Will always return an error if the object does not have a status field.

func (*Controller[T, TList]) Updater

func (c *Controller[T, TList]) Updater() Updater

Updater creates a new Updater for the Object type T.

func (*Controller[T, TList]) Watch

func (c *Controller[T, TList]) Watch(namespace string, opts metav1.ListOptions) (watch.Interface, error)

Watch will start watching resources in the given namespace.

func (*Controller[T, TList]) WithImpersonation

func (c *Controller[T, TList]) WithImpersonation(impersonate rest.ImpersonationConfig) (ClientInterface[T, TList], error)

WithImpersonation returns a new copy of the client that uses impersonation.

type ControllerInterface

type ControllerInterface[T RuntimeMetaObject, TList runtime.Object] interface {
	ControllerMeta
	ClientInterface[T, TList]

	// OnChange runs the given object handler when the controller detects a resource was changed.
	OnChange(ctx context.Context, name string, sync ObjectHandler[T])

	// OnRemove runs the given object handler when the controller detects a resource was changed.
	OnRemove(ctx context.Context, name string, sync ObjectHandler[T])

	// Enqueue adds the resource with the given name in the provided namespace to the worker queue of the controller.
	Enqueue(namespace, name string)

	// EnqueueAfter runs Enqueue after the provided duration.
	EnqueueAfter(namespace, name string, duration time.Duration)

	// Cache returns a cache for the resource type T.
	Cache() CacheInterface[T]
}

ControllerInterface interface for managing K8s Objects.

type ControllerMeta

type ControllerMeta interface {
	// Informer returns the SharedIndexInformer used by this controller.
	Informer() cache.SharedIndexInformer

	// GroupVersionKind returns the GVK used to create this Controller.
	GroupVersionKind() schema.GroupVersionKind

	// AddGenericHandler adds a generic handler that runs when a resource changes.
	AddGenericHandler(ctx context.Context, name string, handler Handler)

	// AddGenericHandler adds a generic handler that runs when a resource is removed.
	AddGenericRemoveHandler(ctx context.Context, name string, handler Handler)

	// Updater returns a update function that will attempt to perform an update for a specific resource type.
	Updater() Updater
}

ControllerMeta holds meta information shared by all controllers.

type Factory

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

func NewFactoryFromConfigWithOptions

func NewFactoryFromConfigWithOptions(config *rest.Config, opts *FactoryOptions) (*Factory, error)

func (*Factory) ControllerFactory

func (c *Factory) ControllerFactory() controller.SharedControllerFactory

func (*Factory) SetThreadiness

func (c *Factory) SetThreadiness(gvk schema.GroupVersionKind, threadiness int)

func (*Factory) Start

func (c *Factory) Start(ctx context.Context, defaultThreadiness int) error

func (*Factory) Sync

func (c *Factory) Sync(ctx context.Context) error

type FactoryOptions

type FactoryOptions struct {
	Namespace               string
	Resync                  time.Duration
	SharedCacheFactory      cache.SharedCacheFactory
	SharedControllerFactory controller.SharedControllerFactory
	HealthCallback          func(bool)
}

type GeneratingHandlerOptions

type GeneratingHandlerOptions struct {
	AllowCrossNamespace bool
	AllowClusterScoped  bool
	NoOwnerReference    bool
	DynamicLookup       bool
	// UniqueApplyForResourceVersion will skip calling apply if the resource version didn't change from the previous execution
	UniqueApplyForResourceVersion bool
}

type Handler

type Handler func(key string, obj runtime.Object) (runtime.Object, error)

ObjectHandler performs operations on the given runtime.Object and returns the new runtime.Object or an error

func FromObjectHandlerToHandler

func FromObjectHandlerToHandler[T RuntimeMetaObject](sync ObjectHandler[T]) Handler

FromObjectHandlerToHandler converts an ObjecHandler to a Handler.

func NewRemoveHandler

func NewRemoveHandler(name string, updater Updater, handler Handler) Handler

type Indexer

type Indexer[T runtime.Object] func(obj T) ([]string, error)

Indexer computes a set of indexed values for the provided object.

type NonNamespacedCache

type NonNamespacedCache[T runtime.Object] struct {
	CacheInterface[T]
}

NonNamespacedCache is a Cache for objects of type T that are not namespaced.

func (*NonNamespacedCache[T]) Get

func (c *NonNamespacedCache[T]) Get(name string) (T, error)

Get calls Cache.Get(...) with an empty namespace parameter.

func (*NonNamespacedCache[T]) List

func (c *NonNamespacedCache[T]) List(selector labels.Selector) (ret []T, err error)

Get calls Cache.List(...) with an empty namespace parameter.

type NonNamespacedCacheInterface

type NonNamespacedCacheInterface[T runtime.Object] interface {
	// Get returns the resources with the specified name from the cache.
	Get(name string) (T, error)

	// List will attempt to find resources from the Cache.
	List(selector labels.Selector) ([]T, error)

	// AddIndexer adds  a new Indexer to the cache with the provided name.
	// If you call this after you already have data in the store, the results are undefined.
	AddIndexer(indexName string, indexer Indexer[T])

	// GetByIndex returns the stored objects whose set of indexed values
	// for the named index includes the given indexed value.
	GetByIndex(indexName, key string) ([]T, error)
}

NonNamespacedCacheInterface is an interface for non namespaced Object retrieval from memory.

type NonNamespacedClientInterface

type NonNamespacedClientInterface[T RuntimeMetaObject, TList runtime.Object] interface {
	// Create creates a new object and return the newly created Object or an error.
	Create(T) (T, error)

	// Update updates the object and return the newly updated Object or an error.
	Update(T) (T, error)

	// UpdateStatus updates the Status field of a the object and return the newly updated Object or an error.
	// Will always return an error if the object does not have a status field.
	UpdateStatus(T) (T, error)

	// Delete deletes the Object in the given name.
	Delete(name string, options *metav1.DeleteOptions) error

	// Get will attempt to retrieve the resource with the specified name.
	Get(name string, options metav1.GetOptions) (T, error)

	// List will attempt to find multiple resources.
	List(opts metav1.ListOptions) (TList, error)

	// Watch will start watching resources.
	Watch(opts metav1.ListOptions) (watch.Interface, error)

	// Patch will patch the resource with the matching name.
	Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result T, err error)

	// WithImpersonation returns a new copy of the client that uses impersonation.
	WithImpersonation(impersonate rest.ImpersonationConfig) (NonNamespacedClientInterface[T, TList], error)
}

NonNamespacedClientInterface is an interface to performs CRUD like operations on nonNamespaced Objects.

type NonNamespacedController

type NonNamespacedController[T RuntimeMetaObject, TList runtime.Object] struct {
	*Controller[T, TList]
}

NonNamespacedController is a Controller for non namespaced resources. This controller provides similar function definitions as Controller except the namespace parameter is omitted.

func NewNonNamespacedController

func NewNonNamespacedController[T RuntimeMetaObject, TList runtime.Object](gvk schema.GroupVersionKind, resource string,
	controller controller.SharedControllerFactory,
) *NonNamespacedController[T, TList]

NewNonNamespacedController returns a Controller controller that is not namespaced. NonNamespacedController redefines specific functions to no longer accept the namespace parameter.

func (*NonNamespacedController[T, TList]) Cache

func (c *NonNamespacedController[T, TList]) Cache() NonNamespacedCacheInterface[T]

Cache calls ControllerInterface.Cache(...) and wraps the result in a new NonNamespacedCache.

func (*NonNamespacedController[T, TList]) Delete

func (c *NonNamespacedController[T, TList]) Delete(name string, options *metav1.DeleteOptions) error

Delete calls Controller.Delete(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) Enqueue

func (c *NonNamespacedController[T, TList]) Enqueue(name string)

Enqueue calls Controller.Enqueue(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) EnqueueAfter

func (c *NonNamespacedController[T, TList]) EnqueueAfter(name string, duration time.Duration)

EnqueueAfter calls Controller.EnqueueAfter(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) Get

func (c *NonNamespacedController[T, TList]) Get(name string, options metav1.GetOptions) (T, error)

Get calls Controller.Get(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) List

func (c *NonNamespacedController[T, TList]) List(opts metav1.ListOptions) (TList, error)

List calls Controller.List(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) Patch

func (c *NonNamespacedController[T, TList]) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (T, error)

Patch calls the Controller.Patch(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) Watch

func (c *NonNamespacedController[T, TList]) Watch(opts metav1.ListOptions) (watch.Interface, error)

Watch calls Controller.Watch(...) with an empty namespace parameter.

func (*NonNamespacedController[T, TList]) WithImpersonation

func (c *NonNamespacedController[T, TList]) WithImpersonation(impersonate rest.ImpersonationConfig) (NonNamespacedClientInterface[T, TList], error)

WithImpersonation returns a new copy of the client that uses impersonation.

type NonNamespacedControllerInterface

type NonNamespacedControllerInterface[T RuntimeMetaObject, TList runtime.Object] interface {
	ControllerMeta
	NonNamespacedClientInterface[T, TList]

	// OnChange runs the given object handler when the controller detects a resource was changed.
	OnChange(ctx context.Context, name string, sync ObjectHandler[T])

	// OnRemove runs the given object handler when the controller detects a resource was changed.
	OnRemove(ctx context.Context, name string, sync ObjectHandler[T])

	// Enqueue adds the resource with the given name to the worker queue of the controller.
	Enqueue(name string)

	// EnqueueAfter runs Enqueue after the provided duration.
	EnqueueAfter(name string, duration time.Duration)

	// Cache returns a cache for the resource type T.
	Cache() NonNamespacedCacheInterface[T]
}

NonNamespacedControllerInterface interface for managing non namespaced K8s Objects.

type ObjectHandler

type ObjectHandler[T runtime.Object] func(string, T) (T, error)

ObjectHandler performs operations on the given object and returns the new object or an error

type RuntimeMetaObject

type RuntimeMetaObject interface {
	comparable
	runtime.Object
	metav1.Object
}

RuntimeMetaObject is an interface for a K8s Object to be used with a specific controller.

type Updater

type Updater func(runtime.Object) (runtime.Object, error)

Directories

Path Synopsis
Package fake is a generated GoMock package.
Package fake is a generated GoMock package.

Jump to

Keyboard shortcuts

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