controller-runtime: sigs.k8s.io/controller-runtime/pkg/handler Index | Examples | Files

package handler

import "sigs.k8s.io/controller-runtime/pkg/handler"

Package handler defines EventHandlers that enqueue reconcile.Requests in response to Create, Update, Deletion Events observed from Watching Kubernetes APIs. Users should provide a source.Source and handler.EventHandler to Controller.Watch in order to generate and enqueue reconcile.Request work items.

Generally, following premade event handlers should be sufficient for most use cases:

EventHandlers

EnqueueRequestForObject - Enqueues a reconcile.Request containing the Name and Namespace of the object in the Event. This will cause the object that was the source of the Event (e.g. the created / deleted / updated object) to be reconciled.

EnqueueRequestForOwner - Enqueues a reconcile.Request containing the Name and Namespace of the Owner of the object in the Event. This will cause owner of the object that was the source of the Event (e.g. the owner object that created the object) to be reconciled.

EnqueueRequestsFromMapFunc - Enqueues reconcile.Requests resulting from a user provided transformation function run against the object in the Event. This will cause an arbitrary collection of objects (defined from a transformation of the source object) to be reconciled.

Index

Examples

Package Files

doc.go enqueue.go enqueue_mapped.go enqueue_owner.go eventhandler.go

type EnqueueRequestForObject Uses

type EnqueueRequestForObject struct{}

EnqueueRequestForObject enqueues a Request containing the Name and Namespace of the object that is the source of the Event. (e.g. the created / deleted / updated objects Name and Namespace). handler.EnqueueRequestForObject is used by almost all Controllers that have associated Resources (e.g. CRDs) to reconcile the associated Resource.

This example watches Pods and enqueues Requests with the Name and Namespace of the Pod from the Event (i.e. change caused by a Create, Update, Delete).

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &corev1.Pod{}},
    &handler.EnqueueRequestForObject{},
)
if err != nil {
    // handle it
}

func (*EnqueueRequestForObject) Create Uses

func (e *EnqueueRequestForObject) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface)

Create implements EventHandler

func (*EnqueueRequestForObject) Delete Uses

func (e *EnqueueRequestForObject) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface)

Delete implements EventHandler

func (*EnqueueRequestForObject) Generic Uses

func (e *EnqueueRequestForObject) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface)

Generic implements EventHandler

func (*EnqueueRequestForObject) Update Uses

func (e *EnqueueRequestForObject) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface)

Update implements EventHandler

type EnqueueRequestForOwner Uses

type EnqueueRequestForOwner struct {
    // OwnerType is the type of the Owner object to look for in OwnerReferences.  Only Group and Kind are compared.
    OwnerType runtime.Object

    // IsController if set will only look at the first OwnerReference with Controller: true.
    IsController bool
    // contains filtered or unexported fields
}

EnqueueRequestForOwner enqueues Requests for the Owners of an object. E.g. the object that created the object that was the source of the Event.

If a ReplicaSet creates Pods, users may reconcile the ReplicaSet in response to Pod Events using:

- a source.Kind Source with Type of Pod.

- a handler.EnqueueRequestForOwner EventHandler with an OwnerType of ReplicaSet and IsController set to true.

This example watches ReplicaSets and enqueues a Request containing the Name and Namespace of the owning (direct) Deployment responsible for the creation of the ReplicaSet.

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &appsv1.ReplicaSet{}},
    &handler.EnqueueRequestForOwner{
        OwnerType:    &appsv1.Deployment{},
        IsController: true,
    },
)
if err != nil {
    // handle it
}

func (*EnqueueRequestForOwner) Create Uses

func (e *EnqueueRequestForOwner) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface)

Create implements EventHandler

func (*EnqueueRequestForOwner) Delete Uses

func (e *EnqueueRequestForOwner) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface)

Delete implements EventHandler

func (*EnqueueRequestForOwner) Generic Uses

func (e *EnqueueRequestForOwner) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface)

Generic implements EventHandler

func (*EnqueueRequestForOwner) InjectMapper Uses

func (e *EnqueueRequestForOwner) InjectMapper(m meta.RESTMapper) error

InjectMapper is called by the Controller to provide the rest mapper used by the manager.

func (*EnqueueRequestForOwner) InjectScheme Uses

func (e *EnqueueRequestForOwner) InjectScheme(s *runtime.Scheme) error

InjectScheme is called by the Controller to provide a singleton scheme to the EnqueueRequestForOwner.

func (*EnqueueRequestForOwner) Update Uses

func (e *EnqueueRequestForOwner) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface)

Update implements EventHandler

type EnqueueRequestsFromMapFunc Uses

type EnqueueRequestsFromMapFunc struct {
    // Mapper transforms the argument into a slice of keys to be reconciled
    ToRequests Mapper
}

EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects defined by some user specified transformation of the source Event. (e.g. trigger Reconciler for a set of objects in response to a cluster resize event caused by adding or deleting a Node)

EnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other objects of a differing type.

For UpdateEvents which contain both a new and old object, the transformation function is run on both objects and both sets of Requests are enqueue.

This example watches Deployments and enqueues a Request contain the Name and Namespace of different objects (of Type: MyKind) using a mapping function defined by the user.

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &appsv1.Deployment{}},
    &handler.EnqueueRequestsFromMapFunc{
        ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
            return []reconcile.Request{
                {NamespacedName: types.NamespacedName{
                    Name:      a.Meta.GetName() + "-1",
                    Namespace: a.Meta.GetNamespace(),
                }},
                {NamespacedName: types.NamespacedName{
                    Name:      a.Meta.GetName() + "-2",
                    Namespace: a.Meta.GetNamespace(),
                }},
            }
        }),
    })
if err != nil {
    // handle it
}

func (*EnqueueRequestsFromMapFunc) Create Uses

func (e *EnqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface)

Create implements EventHandler

func (*EnqueueRequestsFromMapFunc) Delete Uses

func (e *EnqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface)

Delete implements EventHandler

func (*EnqueueRequestsFromMapFunc) Generic Uses

func (e *EnqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface)

Generic implements EventHandler

func (*EnqueueRequestsFromMapFunc) InjectFunc Uses

func (e *EnqueueRequestsFromMapFunc) InjectFunc(f inject.Func) error

InjectFunc implements inject.Injector.

func (*EnqueueRequestsFromMapFunc) Update Uses

func (e *EnqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface)

Update implements EventHandler

type EventHandler Uses

type EventHandler interface {
    // Create is called in response to an create event - e.g. Pod Creation.
    Create(event.CreateEvent, workqueue.RateLimitingInterface)

    // Update is called in response to an update event -  e.g. Pod Updated.
    Update(event.UpdateEvent, workqueue.RateLimitingInterface)

    // Delete is called in response to a delete event - e.g. Pod Deleted.
    Delete(event.DeleteEvent, workqueue.RateLimitingInterface)

    // Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
    // external trigger request - e.g. reconcile Autoscaling, or a Webhook.
    Generic(event.GenericEvent, workqueue.RateLimitingInterface)
}

EventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create). EventHandlers map an Event for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an Event for object with type Foo (using source.KindSource) then reconcile one or more object(s) with type Bar.

Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.

* Use EnqueueRequestForObject to reconcile the object the event is for - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)

* Use EnqueueRequestForOwner to reconcile the owner of the object the event is for - do this for events for the types the Controller creates. (e.g. ReplicaSets created by a Deployment Controller)

* Use EnqueueRequestFromMapFunc to transform an event for an object to a reconcile of an object of a different type - do this for events for types the Controller may be interested in, but doesn't create. (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)

Unless you are implementing your own EventHandler, you can ignore the functions on the EventHandler interface. Most users shouldn't need to implement their own EventHandler.

type Funcs Uses

type Funcs struct {
    // Create is called in response to an add event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    CreateFunc func(event.CreateEvent, workqueue.RateLimitingInterface)

    // Update is called in response to an update event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    UpdateFunc func(event.UpdateEvent, workqueue.RateLimitingInterface)

    // Delete is called in response to a delete event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    DeleteFunc func(event.DeleteEvent, workqueue.RateLimitingInterface)

    // GenericFunc is called in response to a generic event.  Defaults to no-op.
    // RateLimitingInterface is used to enqueue reconcile.Requests.
    GenericFunc func(event.GenericEvent, workqueue.RateLimitingInterface)
}

Funcs implements EventHandler.

This example implements handler.EnqueueRequestForObject.

Code:

// controller is a controller.controller
err := c.Watch(
    &source.Kind{Type: &corev1.Pod{}},
    handler.Funcs{
        CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.Meta.GetName(),
                Namespace: e.Meta.GetNamespace(),
            }})
        },
        UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.MetaNew.GetName(),
                Namespace: e.MetaNew.GetNamespace(),
            }})
        },
        DeleteFunc: func(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.Meta.GetName(),
                Namespace: e.Meta.GetNamespace(),
            }})
        },
        GenericFunc: func(e event.GenericEvent, q workqueue.RateLimitingInterface) {
            q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
                Name:      e.Meta.GetName(),
                Namespace: e.Meta.GetNamespace(),
            }})
        },
    },
)
if err != nil {
    // handle it
}

func (Funcs) Create Uses

func (h Funcs) Create(e event.CreateEvent, q workqueue.RateLimitingInterface)

Create implements EventHandler

func (Funcs) Delete Uses

func (h Funcs) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface)

Delete implements EventHandler

func (Funcs) Generic Uses

func (h Funcs) Generic(e event.GenericEvent, q workqueue.RateLimitingInterface)

Generic implements EventHandler

func (Funcs) Update Uses

func (h Funcs) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface)

Update implements EventHandler

type MapObject Uses

type MapObject struct {
    // Meta is the meta data for an object from an event.
    Meta metav1.Object

    // Object is the object from an event.
    Object runtime.Object
}

MapObject contains information from an event to be transformed into a Request.

type Mapper Uses

type Mapper interface {
    // Map maps an object
    Map(MapObject) []reconcile.Request
}

Mapper maps an object to a collection of keys to be enqueued

type ToRequestsFunc Uses

type ToRequestsFunc func(MapObject) []reconcile.Request

ToRequestsFunc implements Mapper using a function.

func (ToRequestsFunc) Map Uses

func (m ToRequestsFunc) Map(i MapObject) []reconcile.Request

Map implements Mapper

Package handler imports 11 packages (graph) and is imported by 220 packages. Updated 2019-07-08. Refresh now. Tools for package owners.