Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

Commit

Permalink
Merge pull request #60 from enj/enj/r/name_filter
Browse files Browse the repository at this point in the history
Add filter by name utility functions
  • Loading branch information
openshift-merge-robot committed Nov 26, 2018
2 parents 9700c97 + 157b0ed commit bdbce1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/boilerplate/controller/filter.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}
7 changes: 7 additions & 0 deletions pkg/boilerplate/operator/filter.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit bdbce1a

Please sign in to comment.