From 4fcd222777c99963aca5a3b5bb463b2b1e0e0296 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Mon, 14 May 2018 12:12:43 -0400 Subject: [PATCH] Expand required resource verbs with get and delete Some resources use GET for listing, which resulted in errors. Signed-off-by: Nolan Brubaker --- pkg/discovery/helper.go | 8 +++-- pkg/discovery/helper_test.go | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/pkg/discovery/helper.go b/pkg/discovery/helper.go index c62fca09b..fd23e8dce 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 bfa1e21fb..55d76d6d3 100644 --- a/pkg/discovery/helper_test.go +++ b/pkg/discovery/helper_test.go @@ -84,3 +84,60 @@ 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, create, get, delete in a different order", + groupVersion: "v1", + res: &metav1.APIResource{ + Verbs: metav1.Verbs{"delete", "get", "create", "list"}, + }, + expected: true, + }, + { + name: "resource that supports list, create, get, delete, and more", + groupVersion: "v1", + res: &metav1.APIResource{ + Verbs: metav1.Verbs{"list", "create", "get", "delete", "update", "patch", "deletecollection"}, + }, + expected: true, + }, + { + name: "resource that supports only list and create", + groupVersion: "v1", + res: &metav1.APIResource{ + Verbs: metav1.Verbs{"list", "create"}, + }, + expected: false, + }, + { + name: "resource that supports only 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) + }) + } +}