controller

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package controller is responsible for creating and registering controllers for sigs.k8s.io/controller-runtime/pkg/manager.Manager.

A controller is responsible for watching for updates to the resource of a desired type and propagating those updates as events through the event channel.

The reconciliation part of a controller -- reacting on a resource change -- is implemented by the Reconciler type, which in turn implements sigs.k8s.io/controller-runtime/pkg/reconcile.Reconciler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(
	ctx context.Context,
	objectType client.Object,
	mgr manager.Manager,
	eventCh chan<- interface{},
	options ...Option,
) error

Register registers a new controller for the object type in the manager and configure it with the provided options. If the options include WithFieldIndices, it will add the specified indices to FieldIndexer of the manager. The registered controller will send events to the provided channel.

Types

type Getter

type Getter interface {
	// Get is from client.Reader.
	Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
}

Getter gets a resource from the k8s API. It allows us to mock the client.Reader.Get method.

type NamespacedNameFilterFunc

type NamespacedNameFilterFunc func(nsname types.NamespacedName) (shouldProcess bool, msg string)

NamespacedNameFilterFunc is a function that returns true if the resource should be processed by the reconciler. If the function returns false, the reconciler will log the returned string.

type NewReconcilerFunc

type NewReconcilerFunc func(cfg ReconcilerConfig) *Reconciler

NewReconcilerFunc defines a function that creates a new Reconciler. Used for unit-testing.

type Option

type Option func(*config)

Option defines configuration options for registering a controller.

func WithFieldIndices

func WithFieldIndices(fieldIndices index.FieldIndices) Option

WithFieldIndices adds indices to the FieldIndexer of the manager.

func WithK8sPredicate

func WithK8sPredicate(p predicate.Predicate) Option

WithK8sPredicate enables filtering of events before they are sent to the controller.

func WithNamespacedNameFilter

func WithNamespacedNameFilter(filter NamespacedNameFilterFunc) Option

WithNamespacedNameFilter enables filtering of objects by NamespacedName by the controller.

func WithNewReconciler

func WithNewReconciler(newReconciler NewReconcilerFunc) Option

WithNewReconciler allows us to mock reconciler creation in the unit tests.

func WithOnlyMetadata added in v1.1.0

func WithOnlyMetadata() Option

WithOnlyMetadata tells the controller to only cache metadata, and to watch the API server in metadata-only form. If using this option, you must set the GroupVersionKind on the ObjectType you pass into the Register function. If watching a resource with OnlyMetadata, for example the v1.Pod, you must not Get and List using the v1.Pod type. Instead, you must use the special metav1.PartialObjectMetadata type.

type Reconciler

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

Reconciler reconciles Kubernetes resources of a specific type. It implements the reconcile.Reconciler interface. A successful reconciliation of a resource has the two possible outcomes: (1) If the resource is deleted, the Implementation will send a DeleteEvent to the event channel. (2) If the resource is upserted (created or updated), the Implementation will send an UpsertEvent to the event channel.

func NewReconciler

func NewReconciler(cfg ReconcilerConfig) *Reconciler

NewReconciler creates a new reconciler.

func (*Reconciler) Reconcile

func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error)

Reconcile implements the reconcile.Reconciler Reconcile method.

type ReconcilerConfig

type ReconcilerConfig struct {
	// Getter gets a resource from the k8s API.
	Getter Getter
	// ObjectType is the type of the resource that the reconciler will reconcile.
	ObjectType client.Object
	// EventCh is the channel where the reconciler will send events.
	EventCh chan<- interface{}
	// NamespacedNameFilter filters resources the controller will process. Can be nil.
	NamespacedNameFilter NamespacedNameFilterFunc
	// OnlyMetadata indicates that this controller for this resource is only caching metadata for the resource.
	OnlyMetadata bool
}

ReconcilerConfig is the configuration for the reconciler.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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