v0.12.0 Latest Latest

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

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





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"


This section is empty.


func NewPause added in v0.6.0

func NewPause(key string) (handler.EventHandler, error)

NewPause returns an event handler that filters out objects with a truthy "paused" annotation. When an annotation with key string key is present on an object and has a truthy value, ex. "true", the watch constructed with this event handler will not add events for that object to the queue. Key string key must be a valid annotation key.

A note on security: since users that can CRUD a particular API can apply or remove annotations with default cluster admission controllers, this same set of users can therefore start or stop reconciliation of objects via this pause mechanism. If this is a concern, configure an admission webhook to enforce a stricter annotation modification policy. See AdmissionReview configuration for user info available to a webhook: https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#request


This example applies the Pause handler to all incoming Pod events on a Pod controller.

package main

import (

	v1 "k8s.io/api/core/v1"

func main() {
	cfg, err := config.GetConfig()
	if err != nil {

	mgr, err := manager.New(cfg, manager.Options{})
	if err != nil {

	c, err := controller.NewUnmanaged("pod", mgr, controller.Options{
		Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
			return reconcile.Result{}, nil
	if err != nil {

	// Filter out Pods with the "my.app/paused: true" annotation.
	pause, err := handler.NewPause("my.app/paused")
	if err != nil {
	if err := c.Watch(source.Kind(mgr.GetCache(), &v1.Pod{}), pause); err != nil {


	if err := c.Start(signals.SetupSignalHandler()); err != nil {

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.


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")

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:


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 {

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:


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.


Path Synopsis

Jump to

Keyboard shortcuts

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