Add FilterByName for cluster-scoped resources. (#816)

This is a precursor to reconciling named webhook configurations, and largely a copy of `FilterByNameAndNamespace`.
This commit is contained in:
Matt Moore 2019-10-28 10:41:42 -07:00 committed by Knative Prow Robot
parent ba704ad664
commit 809ce573e4
2 changed files with 74 additions and 0 deletions

View File

@ -98,6 +98,17 @@ func Filter(gvk schema.GroupVersionKind) func(obj interface{}) bool {
}
}
// FilterWithName makes it simple to create FilterFunc's for use with
// cache.FilteringResourceEventHandler that filter based on a name.
func FilterWithName(name string) func(obj interface{}) bool {
return func(obj interface{}) bool {
if object, ok := obj.(metav1.Object); ok {
return name == object.GetName()
}
return false
}
}
// 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 {

View File

@ -135,6 +135,69 @@ func TestFilterWithNameAndNamespace(t *testing.T) {
}
}
func TestFilterWithName(t *testing.T) {
filter := FilterWithName("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: true, // Unlike FilterWithNameAndNamespace this passes
}, {
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)