-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using predicate in the controller watchers to filter out irrelevant e…
…vents Signed-off-by: Moath Qasim <moad.qassem@gmail.com>
- Loading branch information
1 parent
094aa53
commit d727107
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
) | ||
|
||
// Factory returns a predicate func that applies the given filter function | ||
// on CREATE, UPDATE and DELETE events. For UPDATE events, the filter is applied | ||
// to both the old and new object and OR's the result. | ||
func Factory(filter func(o ctrlruntimeclient.Object) bool) predicate.Funcs { | ||
if filter == nil { | ||
return predicate.Funcs{} | ||
} | ||
|
||
return predicate.Funcs{ | ||
CreateFunc: func(e event.CreateEvent) bool { | ||
return filter(e.Object) | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent) bool { | ||
return filter(e.ObjectOld) || filter(e.ObjectNew) | ||
}, | ||
DeleteFunc: func(e event.DeleteEvent) bool { | ||
return filter(e.Object) | ||
}, | ||
} | ||
} | ||
|
||
// ByNamespace returns a predicate func that only includes objects in the given namespace. | ||
func ByNamespace(namespace string) predicate.Funcs { | ||
return Factory(func(o ctrlruntimeclient.Object) bool { | ||
return o.GetNamespace() == namespace | ||
}) | ||
} |