Skip to content

Commit fa3a947

Browse files
tyler-lloydrbtr
authored andcommitted
refactor: cleanup PredicateFunc usage
Group the predicate funcs into either specific event filters or the "universal" filters which get applied to every event type. Then And() them which is what controller-runtime event_handler is functionally doing anyway. link: https://github.com/kubernetes-sigs/controller-runtime/blob/aea2e32a936584b06ae6f7992f856fe7292b0297/pkg/internal/source/event_handler.go#L116
1 parent 7917b46 commit fa3a947

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

cns/kubecontroller/nodenetworkconfig/reconciler.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -160,35 +160,47 @@ func (r *Reconciler) Started(ctx context.Context) (bool, error) {
160160
// SetupWithManager Sets up the reconciler with a new manager, filtering using NodeNetworkConfigFilter on nodeName.
161161
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, node *v1.Node, cnsconfig *configuration.CNSConfig) error {
162162
r.nnccli = nodenetworkconfig.NewClient(mgr.GetClient())
163+
164+
// specific event funcs
165+
eventFuncs := predicate.Funcs{
166+
// ignore delete events.
167+
DeleteFunc: func(event.DeleteEvent) bool {
168+
return false
169+
},
170+
UpdateFunc: func(ue event.UpdateEvent) bool {
171+
if ue.ObjectOld == nil || ue.ObjectNew == nil {
172+
return false
173+
}
174+
// check that the generation is the same - status changes don't update generation. But in IPAMv2
175+
// it is safe to reconcile objects with a changed generation (typically means it was patched by
176+
// CNS itself). This saves us from filtering out updates that were made while this controller's
177+
// watch was down.
178+
if cnsconfig != nil && cnsconfig.EnableIPAMv2 {
179+
return true
180+
}
181+
return ue.ObjectOld.GetGeneration() == ue.ObjectNew.GetGeneration()
182+
},
183+
}
184+
185+
// these are applied to every event type
186+
universalFuncs := predicate.NewPredicateFuncs(func(object client.Object) bool {
187+
// match on node controller ref for all other events.
188+
if !metav1.IsControlledBy(object, node) {
189+
return false
190+
}
191+
192+
// only process events on objects that are not being deleted.
193+
if !object.GetDeletionTimestamp().IsZero() {
194+
return false
195+
}
196+
return true
197+
})
198+
199+
filters := predicate.And(eventFuncs, universalFuncs)
200+
163201
err := ctrl.NewControllerManagedBy(mgr).
164202
For(&v1alpha.NodeNetworkConfig{}).
165-
WithEventFilter(predicate.Funcs{
166-
// ignore delete events.
167-
DeleteFunc: func(event.DeleteEvent) bool {
168-
return false
169-
},
170-
}).
171-
WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool {
172-
// match on node controller ref for all other events.
173-
return metav1.IsControlledBy(object, node)
174-
})).
175-
WithEventFilter(predicate.Funcs{
176-
// check that the generation is the same iff IPAMv1 - status changes don't update generation.
177-
UpdateFunc: func(ue event.UpdateEvent) bool {
178-
if ue.ObjectOld == nil || ue.ObjectNew == nil {
179-
return false
180-
}
181-
// IPAMv2 is idempotent and can process every update event.
182-
if cnsconfig != nil && cnsconfig.EnableIPAMv2 {
183-
return true
184-
}
185-
return ue.ObjectOld.GetGeneration() == ue.ObjectNew.GetGeneration()
186-
},
187-
}).
188-
WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool {
189-
// only process events on objects that are not being deleted.
190-
return object.GetDeletionTimestamp().IsZero()
191-
})).
203+
WithEventFilter(filters).
192204
Complete(r)
193205
if err != nil {
194206
return errors.Wrap(err, "failed to set up reconciler with manager")

0 commit comments

Comments
 (0)