Using Predicates for Event Filtering with Operator SDK

Events are produced by Sources assigned to resources a controller is watching. These events are transformed into Requests by EventHandlers and passed to Reconcile(). Predicates allow controllers to filter events before they are provided to EventHandlers. Filtering is useful because your controller may only want to handle specific types of events. Filtering also helps reduce chattiness with the API server, as Reconcile() is only called for events transformed by EventHandlers.

Predicate types

A Predicate implements the following methods that take an event of a particular type and return true if the event should be processed by Reconcile():

  1. // Predicate filters events before enqueuing the keys.
  2. type Predicate interface {
  3. Create(event.CreateEvent) bool
  4. Delete(event.DeleteEvent) bool
  5. Update(event.UpdateEvent) bool
  6. Generic(event.GenericEvent) bool
  7. }
  8. // Funcs implements Predicate.
  9. type Funcs struct {
  10. CreateFunc func(event.CreateEvent) bool
  11. DeleteFunc func(event.DeleteEvent) bool
  12. UpdateFunc func(event.UpdateEvent) bool
  13. GenericFunc func(event.GenericEvent) bool
  14. }

For example, all Create events for any watched resource will be passed to Funcs.Create() and filtered out if the method evaluates to false. If you do not register a Predicate method for a particular type, events of that type will not be filtered.

All event types contain Kubernetes metadata about the object that triggered the event, and the object itself. Predicate logic uses these data to make decisions about what should be filtered. Some event types include other fields pertaining to the semantics of that event. For example, event.UpdateEvent includes both old and new metadata and objects:

  1. type UpdateEvent struct {
  2. // MetaOld is the ObjectMeta of the Kubernetes Type that was updated (before the update).
  3. MetaOld v1.Object
  4. // ObjectOld is the object from the event.
  5. ObjectOld runtime.Object
  6. // MetaNew is the ObjectMeta of the Kubernetes Type that was updated (after the update).
  7. MetaNew v1.Object
  8. // ObjectNew is the object from the event.
  9. ObjectNew runtime.Object
  10. }

You can find all type definitions in the event package documentation.

Using Predicates

Any number of Predicates can be set for a controller via the builder method WithEventFilter(), which will filter an event if any of those Predicates evaluates to false. This first example is an implementation of a memcached-operator controller that simply filters Delete events on Pods that have been confirmed deleted; the controller receives all Delete events that occur, and we may only care about resources that have not been completely deleted:

  1. import (
  2. "context"
  3. "github.com/go-logr/logr"
  4. corev1 "k8s.io/api/core/v1"
  5. "k8s.io/apimachinery/pkg/runtime"
  6. ctrl "sigs.k8s.io/controller-runtime"
  7. "sigs.k8s.io/controller-runtime/pkg/client"
  8. "sigs.k8s.io/controller-runtime/pkg/event"
  9. "sigs.k8s.io/controller-runtime/pkg/predicate"
  10. cachev1alpha1 "github.com/example-inc/app-operator/apis/cache/v1alpha1"
  11. )
  12. ...
  13. func ignoreDeletionPredicate() predicate.Predicate {
  14. return predicate.Funcs{
  15. UpdateFunc: func(e event.UpdateEvent) bool {
  16. // Ignore updates to CR status in which case metadata.Generation does not change
  17. return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration()
  18. },
  19. DeleteFunc: func(e event.DeleteEvent) bool {
  20. // Evaluates to false if the object has been confirmed deleted.
  21. return !e.DeleteStateUnknown
  22. },
  23. }
  24. }
  25. func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error {
  26. return ctrl.NewControllerManagedBy(mgr).
  27. For(&cachev1alpha1.Memcached{}).
  28. Owns(&corev1.Pod{}).
  29. WithEventFilter(ignoreDeletionPredicate()).
  30. Complete(r)
  31. }
  32. ...
  33. }

Use cases

Predicates are not necessary for many operators, although filtering reduces the amount of chatter to the API server from Reconcile(). They are particularly useful for controllers that watch resources cluster-wide, i.e. without a namespace.

Last modified June 17, 2020: Nest legacy Go docs under new ones (#3248) (87a7a6f3)