From 92fabf43b330626ebe561a6f77cc583e0e3bf0ca Mon Sep 17 00:00:00 2001 From: Matt Moyer Date: Wed, 9 Sep 2020 14:55:59 -0500 Subject: [PATCH] Add new controller.SimpleFilter and controller.NoOpFilter utilities. Signed-off-by: Matt Moyer --- internal/controller/utils.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/internal/controller/utils.go b/internal/controller/utils.go index 8f0866cb..e84591cc 100644 --- a/internal/controller/utils.go +++ b/internal/controller/utils.go @@ -11,16 +11,23 @@ import ( "github.com/suzerain-io/pinniped/internal/controllerlib" ) -func NameAndNamespaceExactMatchFilterFactory(name, namespace string) controllerlib.FilterFuncs { - objMatchesFunc := func(obj metav1.Object) bool { +func NameAndNamespaceExactMatchFilterFactory(name, namespace string) controllerlib.Filter { + return SimpleFilter(func(obj metav1.Object) bool { return obj.GetName() == name && obj.GetNamespace() == namespace - } + }) +} + +// NoOpFilter returns a controllerlib.Filter that allows all objects. +func NoOpFilter() controllerlib.Filter { + return SimpleFilter(func(object metav1.Object) bool { return true }) +} + +// SimpleFilter takes a single boolean match function on a metav1.Object and wraps it into a proper controllerlib.Filter. +func SimpleFilter(match func(metav1.Object) bool) controllerlib.Filter { return controllerlib.FilterFuncs{ - AddFunc: objMatchesFunc, - UpdateFunc: func(oldObj, newObj metav1.Object) bool { - return objMatchesFunc(oldObj) || objMatchesFunc(newObj) - }, - DeleteFunc: objMatchesFunc, + AddFunc: match, + UpdateFunc: func(oldObj, newObj metav1.Object) bool { return match(oldObj) || match(newObj) }, + DeleteFunc: match, } }