From 1ae745cf8b38ea401b7cb3819bd4292a94db6c19 Mon Sep 17 00:00:00 2001 From: lou Date: Thu, 10 Aug 2023 19:12:58 +0800 Subject: [PATCH] skip subresource in resource discovery Signed-off-by: lou --- changelogs/unreleased/6688-27149chen | 1 + pkg/discovery/helper.go | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/6688-27149chen diff --git a/changelogs/unreleased/6688-27149chen b/changelogs/unreleased/6688-27149chen new file mode 100644 index 0000000000..229d6e075a --- /dev/null +++ b/changelogs/unreleased/6688-27149chen @@ -0,0 +1 @@ +Fixes #6636, skip subresource in resource discovery \ No newline at end of file diff --git a/pkg/discovery/helper.go b/pkg/discovery/helper.go index e925d27d48..fa84de1f96 100644 --- a/pkg/discovery/helper.go +++ b/pkg/discovery/helper.go @@ -18,6 +18,7 @@ package discovery import ( "sort" + "strings" "sync" "github.com/pkg/errors" @@ -170,7 +171,7 @@ func (h *helper) Refresh() error { } h.resources = discovery.FilteredBy( - discovery.ResourcePredicateFunc(filterByVerbs), + And(filterByVerbs, skipSubresource), serverResources, ) @@ -240,10 +241,34 @@ func refreshServerGroupsAndResources(discoveryClient serverResourcesInterface, l return serverGroups, serverResources, err } +// And returns a composite predicate that implements a logical AND of the predicates passed to it. +func And(predicates ...discovery.ResourcePredicateFunc) discovery.ResourcePredicate { + return and{predicates} +} + +type and struct { + predicates []discovery.ResourcePredicateFunc +} + +func (a and) Match(groupVersion string, r *metav1.APIResource) bool { + for _, p := range a.predicates { + if !p(groupVersion, r) { + return false + } + } + + return true +} + func filterByVerbs(groupVersion string, r *metav1.APIResource) bool { return discovery.SupportsAllVerbs{Verbs: []string{"list", "create", "get", "delete"}}.Match(groupVersion, r) } +func skipSubresource(_ string, r *metav1.APIResource) bool { + // if we have a slash, then this is a subresource and we shouldn't include it. + return !strings.Contains(r.Name, "/") +} + // 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) {