Add predicate func example in book
@@ -114,13 +114,36 @@ mapFn := handler.ToRequestsFunc(
}},
}
})
+
+
+// 'UpdateFunc' and 'CreateFunc' used to judge if a event about the object is
+// what we want. If that is true, the event will be processed by the reconciler.
+p := predicate.Funcs{
+ UpdateFunc: func(e event.UpdateEvent) bool {
+ // The object doesn't contain label "foo", so the event will be
+ // ignored.
+ if _, ok := e.MetaOld.GetLabels()["foo"]; !ok {
+ return false
+ }
+ return e.ObjectOld != e.ObjectNew
+ },
+ CreateFunc: func(e event.CreateEvent) bool {
+ if _, ok := e.Meta.GetLabels()["foo"]; !ok {
+ return false
+ }
+ return true
+ },
+}
+
// Watch Deployments and trigger Reconciles for objects
// mapped from the Deployment in the event
err := c.Watch(
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: mapFn,
- })
+ },
+ // Comment it if default predicate fun is used.
+ p)
if err != nil {
return err
}
