Predicates seems to not work as expected #2950
-
I've tried this: func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool {
return false
}))).
Complete(r)
} This: func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool {
return false
})).
Complete(r)
} And this: func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
WithEventFilter(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
}).
Complete(r)
} But it always try to reconcile ALL of my pods. How to filter those events ? EDIT : It turns out that I got confused by logs produced by a function called in 2 different controllers, so there is the solution I went for: func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool {
_, ok := object.GetLabels()["some-label"]
return ok
}))).
Complete(r)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @paullaffitte, It looks like you've tried multiple approaches to filter out events using predicates. Predicates are a powerful way to ensure that your controller only reconciles objects that meet certain conditions. Your original attempts were trying to filter out all the events by returning However, it might be the correction: func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool {
_, ok := object.GetLabels()["some-label"]
return ok
}))).
Complete(r)
} In the above config, your reconciler will only react to Pod events that have a label with the key "some-label". For further information see the doc https://sdk.operatorframework.io/docs/building-operators/golang/references/event-filtering/ Also, would be great we create a similar doc here in kubebuilder. If you're still facing issues, consider the following:
I hope that helps you out. |
Beta Was this translation helpful? Give feedback.
Hello @paullaffitte,
It looks like you've tried multiple approaches to filter out events using predicates. Predicates are a powerful way to ensure that your controller only reconciles objects that meet certain conditions.
Your original attempts were trying to filter out all the events by returning
false
, which should effectively mean your reconciler doesn't get triggered at all. If it's still reconciling, it might be because of another controller or an error in your setup.However, it might be the correction: