@@ -29,7 +29,7 @@ err := c.Watch(
29
29
&source.Kind {Type: &v1.Pod {}},
30
30
&handler.EnqueueRequestForObject {})
31
31
if err != nil {
32
- return err
32
+ return err
33
33
}
34
34
```
35
35
{% endmethod %}
@@ -64,11 +64,11 @@ correct RBAC rules are in place and informers have been started.
64
64
// Watch for Pod events, and enqueue a reconcile.Request for the ReplicaSet in the OwnerReferences
65
65
err := c.Watch (
66
66
&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 {}})
70
70
if err != nil {
71
- return err
71
+ return err
72
72
}
73
73
```
74
74
{% endmethod %}
@@ -103,26 +103,49 @@ correct RBAC rules are in place and informers have been started.
103
103
// objects to Reconcile
104
104
mapFn := handler.ToRequestsFunc (
105
105
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
+
117
138
// Watch Deployments and trigger Reconciles for objects
118
139
// mapped from the Deployment in the event
119
140
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)
124
147
if err != nil {
125
- return err
148
+ return err
126
149
}
127
150
```
128
151
{% endmethod %}
@@ -140,11 +163,11 @@ object with the external state that would trigger the Reconcile.
140
163
``` go
141
164
events := make (chan event.GenericEvent )
142
165
err := ctrl.Watch (
143
- &source.Channel {Source: events},
144
- &handler.EnqueueRequestForObject {},
166
+ &source.Channel {Source: events},
167
+ &handler.EnqueueRequestForObject {},
145
168
)
146
169
if err != nil {
147
- return err
170
+ return err
148
171
}
149
172
```
150
173
{% endmethod %}
0 commit comments