Skip to content

Commit d82a96b

Browse files
author
willise
committed
Add predicate func example in book
1 parent d904596 commit d82a96b

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

docs/book/beyond_basics/controller_watches.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ err := c.Watch(
2929
&source.Kind{Type: &v1.Pod{}},
3030
&handler.EnqueueRequestForObject{})
3131
if err != nil {
32-
return err
32+
return err
3333
}
3434
```
3535
{% endmethod %}
@@ -64,11 +64,11 @@ correct RBAC rules are in place and informers have been started.
6464
// Watch for Pod events, and enqueue a reconcile.Request for the ReplicaSet in the OwnerReferences
6565
err := c.Watch(
6666
&source.Kind{Type: &corev1.Pod{}},
67-
&handler.EnqueueRequestForOwner{
68-
IsController: true,
69-
OwnerType: &appsv1.ReplicaSet{}})
67+
&handler.EnqueueRequestForOwner{
68+
IsController: true,
69+
OwnerType: &appsv1.ReplicaSet{}})
7070
if err != nil {
71-
return err
71+
return err
7272
}
7373
```
7474
{% endmethod %}
@@ -103,26 +103,49 @@ correct RBAC rules are in place and informers have been started.
103103
// objects to Reconcile
104104
mapFn := handler.ToRequestsFunc(
105105
func(a handler.MapObject) []reconcile.Request {
106-
return []reconcile.Request{
107-
{NamespacedName: types.NamespacedName{
108-
Name: a.Meta.GetName() + "-1",
109-
Namespace: a.Meta.GetNamespace(),
110-
}},
111-
{NamespacedName: types.NamespacedName{
112-
Name: a.Meta.GetName() + "-2",
113-
Namespace: a.Meta.GetNamespace(),
114-
}},
115-
}
116-
})
106+
return []reconcile.Request{
107+
{NamespacedName: types.NamespacedName{
108+
Name: a.Meta.GetName() + "-1",
109+
Namespace: a.Meta.GetNamespace(),
110+
}},
111+
{NamespacedName: types.NamespacedName{
112+
Name: a.Meta.GetName() + "-2",
113+
Namespace: a.Meta.GetNamespace(),
114+
}},
115+
}
116+
})
117+
118+
119+
// 'UpdateFunc' and 'CreateFunc' used to judge if a event about the object is
120+
// what we want. If that is true, the event will be processed by the reconciler.
121+
p := predicate.Funcs{
122+
UpdateFunc: func(e event.UpdateEvent) bool {
123+
// The object doesn't contain label "foo", so the event will be
124+
// ignored.
125+
if _, ok := e.MetaOld.GetLabels()["foo"]; !ok {
126+
return false
127+
}
128+
return e.ObjectOld != e.ObjectNew
129+
},
130+
CreateFunc: func(e event.CreateEvent) bool {
131+
if _, ok := e.Meta.GetLabels()["foo"]; !ok {
132+
return false
133+
}
134+
return true
135+
},
136+
}
137+
117138
// Watch Deployments and trigger Reconciles for objects
118139
// mapped from the Deployment in the event
119140
err := c.Watch(
120-
&source.Kind{Type: &appsv1.Deployment{}},
121-
&handler.EnqueueRequestsFromMapFunc{
122-
ToRequests: mapFn,
123-
})
141+
&source.Kind{Type: &appsv1.Deployment{}},
142+
&handler.EnqueueRequestsFromMapFunc{
143+
ToRequests: mapFn,
144+
},
145+
// Comment it if default predicate fun is used.
146+
p)
124147
if err != nil {
125-
return err
148+
return err
126149
}
127150
```
128151
{% endmethod %}
@@ -140,11 +163,11 @@ object with the external state that would trigger the Reconcile.
140163
```go
141164
events := make(chan event.GenericEvent)
142165
err := ctrl.Watch(
143-
&source.Channel{Source: events},
144-
&handler.EnqueueRequestForObject{},
166+
&source.Channel{Source: events},
167+
&handler.EnqueueRequestForObject{},
145168
)
146169
if err != nil {
147-
return err
170+
return err
148171
}
149172
```
150173
{% endmethod %}

0 commit comments

Comments
 (0)