mirror of https://github.com/knative/pkg.git
Add Filter function by name/namespace (#414)
This commit is contained in:
parent
9e0db8f0a7
commit
5e4512dcb2
|
|
@ -78,6 +78,18 @@ func Filter(gvk schema.GroupVersionKind) func(obj interface{}) bool {
|
|||
}
|
||||
}
|
||||
|
||||
// FilterWithNameAndNamespace makes it simple to create FilterFunc's for use with
|
||||
// cache.FilteringResourceEventHandler that filter based on a namespace and a name.
|
||||
func FilterWithNameAndNamespace(namespace, name string) func(obj interface{}) bool {
|
||||
return func(obj interface{}) bool {
|
||||
if object, ok := obj.(metav1.Object); ok {
|
||||
return name == object.GetName() &&
|
||||
namespace == object.GetNamespace()
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Impl is our core controller implementation. It handles queuing and feeding work
|
||||
// from the queue to an implementation of Reconciler.
|
||||
type Impl struct {
|
||||
|
|
|
|||
|
|
@ -55,6 +55,69 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
func TestFilterWithNameAndNamespace(t *testing.T) {
|
||||
filter := FilterWithNameAndNamespace("test-namespace", "test-name")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
want bool
|
||||
}{{
|
||||
name: "not a metav1.Object",
|
||||
input: "foo",
|
||||
want: false,
|
||||
}, {
|
||||
name: "nil",
|
||||
input: nil,
|
||||
want: false,
|
||||
}, {
|
||||
name: "name matches, namespace does not",
|
||||
input: &Resource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
Namespace: "wrong-namespace",
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
}, {
|
||||
name: "namespace matches, name does not",
|
||||
input: &Resource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "wrong-name",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
}, {
|
||||
name: "neither matches",
|
||||
input: &Resource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "wrong-name",
|
||||
Namespace: "wrong-namespace",
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
}, {
|
||||
name: "matches",
|
||||
input: &Resource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
Namespace: "test-namespace",
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
}}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got := filter(test.input)
|
||||
if test.want != got {
|
||||
t.Errorf("FilterWithNameAndNamespace() = %v, wanted %v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
filter := Filter(gvk)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue