diff --git a/pkg/boilerplate/controller/filter.go b/pkg/boilerplate/controller/filter.go index 90eccd1f..7a2c52fa 100644 --- a/pkg/boilerplate/controller/filter.go +++ b/pkg/boilerplate/controller/filter.go @@ -1,6 +1,9 @@ package controller -import "k8s.io/apimachinery/pkg/apis/meta/v1" +import ( + "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/sets" +) type ParentFilter interface { Parent(obj v1.Object) (namespace, name string) @@ -13,8 +16,10 @@ type Filter interface { Delete(obj v1.Object) bool } +type ParentFunc func(obj v1.Object) (namespace, name string) + type FilterFuncs struct { - ParentFunc func(obj v1.Object) (namespace, name string) + ParentFunc ParentFunc AddFunc func(obj v1.Object) bool UpdateFunc func(oldObj, newObj v1.Object) bool DeleteFunc func(obj v1.Object) bool @@ -47,3 +52,18 @@ func (f FilterFuncs) Delete(obj v1.Object) bool { } return f.DeleteFunc(obj) } + +func FilterByNames(parentFunc ParentFunc, names ...string) ParentFilter { + set := sets.NewString(names...) + has := func(obj v1.Object) bool { + return set.Has(obj.GetName()) + } + return FilterFuncs{ + ParentFunc: parentFunc, + AddFunc: has, + UpdateFunc: func(oldObj, newObj v1.Object) bool { + return has(newObj) + }, + DeleteFunc: has, + } +} diff --git a/pkg/boilerplate/operator/filter.go b/pkg/boilerplate/operator/filter.go new file mode 100644 index 00000000..172427d6 --- /dev/null +++ b/pkg/boilerplate/operator/filter.go @@ -0,0 +1,7 @@ +package operator + +import "github.com/openshift/service-serving-cert-signer/pkg/boilerplate/controller" + +func FilterByNames(names ...string) controller.Filter { + return controller.FilterByNames(nil, names...) +}