handler

package
Version: v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: Apache-2.0 Imports: 12 Imported by: 16

Documentation

Index

Constants

View Source
const (
	// NamespacedNameAnnotation is an annotation whose value encodes the name and namespace of a resource to
	// reconcile when a resource containing this annotation changes. Valid values are of the form
	// `<namespace>/<name>` for namespace-scoped owners and `<name>` for cluster-scoped owners.
	NamespacedNameAnnotation = "operator-sdk/primary-resource"
	// TypeAnnotation is an annotation whose value encodes the group and kind of a resource to reconcil when a
	// resource containing this annotation changes. Valid values are of the form `<Kind>` for resource in the
	// core group, and `<Kind>.<group>` for all other resources.
	TypeAnnotation = "operator-sdk/primary-resource-type"
)

Variables

This section is empty.

Functions

func SetOwnerAnnotations

func SetOwnerAnnotations(owner, object client.Object) error

SetOwnerAnnotations helps in adding 'NamespacedNameAnnotation' and 'TypeAnnotation' to object based on the values obtained from owner. The object gets the annotations from owner's namespace, name, group and kind. In other terms, object can be said to be the dependent having annotations from the owner. When a watch is set on the object, the annotations help to identify the owner and trigger reconciliation. Annotations are ALWAYS overwritten.

Types

type EnqueueRequestForAnnotation

type EnqueueRequestForAnnotation struct {
	Type schema.GroupKind
}

EnqueueRequestForAnnotation enqueues Request containing the Name and Namespace specified in the annotations of the object that is the source of the Event. The source of the event triggers reconciliation of the parent resource which is identified by annotations. `NamespacedNameAnnotation` and `TypeAnnotation` together uniquely identify an owner resource to reconcile.

handler.EnqueueRequestForAnnotation can be used to trigger reconciliation of resources which are cross-referenced. This allows a namespace-scoped dependent to trigger reconciliation of an owner which is in a different namespace, and a cluster-scoped dependent can trigger the reconciliation of a namespace(scoped)-owner.

As an example, consider the case where we would like to watch clusterroles based on which we reconcile namespace-scoped replicasets. With native owner references, this would not be possible since the cluster-scoped dependent (clusterroles) is trying to specify a namespace-scoped owner (replicasets). Whereas in case of annotations-based handlers, we could implement the following:

if err := c.Watch(&source.Kind{
	// Watch clusterroles
	Type: &rbacv1.ClusterRole{}},

	// Enqueue ReplicaSet reconcile requests using the namespacedName annotation value in the request.
	&handler.EnqueueRequestForAnnotation{schema.GroupKind{Group:"apps", Kind:"ReplicaSet"}}); err != nil {
		entryLog.Error(err, "unable to watch ClusterRole")
		os.Exit(1)
	}
}

With this watch, the ReplicaSet reconciler would receive a request to reconcile "my-namespace/my-replicaset" based on a change to a ClusterRole that has the following annotations:

annotations:
	operator-sdk/primary-resource:"my-namespace/my-replicaset"
	operator-sdk/primary-resource-type:"ReplicaSet.apps"

Though an annotation-based watch handler removes the boundaries set by native owner reference implementation, the garbage collector still respects the scope restrictions. For example, if a parent creates a child resource across scopes not supported by owner references, it becomes the responsibility of the reconciler to clean up the child resource. Hence, the resource utilizing this handler SHOULD ALWAYS BE IMPLEMENTED WITH A FINALIZER.

func (*EnqueueRequestForAnnotation) Create

Create implements EventHandler

func (*EnqueueRequestForAnnotation) Delete

Delete implements EventHandler

func (*EnqueueRequestForAnnotation) Generic

Generic implements EventHandler

func (*EnqueueRequestForAnnotation) Update

Update implements EventHandler

type InstrumentedEnqueueRequestForObject

type InstrumentedEnqueueRequestForObject struct {
	handler.EnqueueRequestForObject
}

InstrumentedEnqueueRequestForObject wraps controller-runtime handler for "EnqueueRequestForObject", and sets up primary resource metrics on event handlers. The main objective of this handler is to set prometheus metrics when create/update/delete events occur. These metrics contain the following information on resource.

resource_created_at_seconds{"name", "namespace", "group", "version", "kind"}

To call the handler use:

&handler.InstrumentedEnqueueRequestForObject{}

func (InstrumentedEnqueueRequestForObject) Create

Create implements EventHandler, and creates the metrics.

func (InstrumentedEnqueueRequestForObject) Delete

Delete implements EventHandler, and deletes metrics.

func (InstrumentedEnqueueRequestForObject) Update

Update implements EventHandler, and updates the metrics.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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