diff --git a/pkg/discovery/helper.go b/pkg/discovery/helper.go index c62fca09b3d..fd23e8dcea8 100644 --- a/pkg/discovery/helper.go +++ b/pkg/discovery/helper.go @@ -108,9 +108,7 @@ func (h *helper) Refresh() error { } h.resources = discovery.FilteredBy( - discovery.ResourcePredicateFunc(func(groupVersion string, r *metav1.APIResource) bool { - return discovery.SupportsAllVerbs{Verbs: []string{"list", "create"}}.Match(groupVersion, r) - }), + discovery.ResourcePredicateFunc(filterByVerbs), preferredResources, ) @@ -132,6 +130,10 @@ func (h *helper) Refresh() error { return nil } +func filterByVerbs(groupVersion string, r *metav1.APIResource) bool { + return discovery.SupportsAllVerbs{Verbs: []string{"list", "create", "get", "delete"}}.Match(groupVersion, r) +} + // sortResources sources resources by moving extensions to the end of the slice. The order of all // the other resources is preserved. func sortResources(resources []*metav1.APIResourceList) { diff --git a/pkg/discovery/helper_test.go b/pkg/discovery/helper_test.go index bfa1e21fb26..8c20cc5a128 100644 --- a/pkg/discovery/helper_test.go +++ b/pkg/discovery/helper_test.go @@ -84,3 +84,44 @@ func TestSortResources(t *testing.T) { }) } } + +func TestFilteringByVerbs(t *testing.T) { + tests := []struct { + name string + groupVersion string + res *metav1.APIResource + expected bool + }{ + { + name: "resource that supports list, create, get, delete", + groupVersion: "v1", + res: &metav1.APIResource{ + Verbs: metav1.Verbs{"list", "create", "get", "delete"}, + }, + expected: true, + }, + { + name: "resource that supports list and create", + groupVersion: "v1", + res: &metav1.APIResource{ + Verbs: metav1.Verbs{"list", "create"}, + }, + expected: false, + }, + { + name: "resource that supports get and delete", + groupVersion: "v1", + res: &metav1.APIResource{ + Verbs: metav1.Verbs{"get", "delete"}, + }, + expected: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + out := filterByVerbs(test.groupVersion, test.res) + assert.Equal(t, test.expected, out) + }) + } +}