Skip to content

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.

handler.EventHandler is an argument to Controller.Watch that enqueues reconcile.Requests in response to events.

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

EventHandler interface

// * Use EnqueueRequestsFromMapFunc 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 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)
}

EnqueueRequestForObject

This is used by default in builder.doWatch. If you create an operator with kubebuilder, you're using this eventhandler. This function converts events received from the Source into reconcile.Requests object and enqueue them to the given queue.

  1. Create, Delete, Generic:
    q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
        Name:      evt.Object.GetName(),
        Namespace: evt.Object.GetNamespace(),
    }})
    
  2. Update: Enqueue ObjectNew (ObjectOld if ObjectNew doesn't exist)
    q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
        Name:      evt.ObjectNew.GetName(),
        Namespace: evt.ObjectNew.GetNamespace(),
    }})