Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip subresource in resource discovery #6635

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/unreleased/6635-27149chen
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes #6636, skip subresource in resource discovery
27 changes: 26 additions & 1 deletion pkg/discovery/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package discovery

import (
"sort"
"strings"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -170,7 +171,7 @@ func (h *helper) Refresh() error {
}

h.resources = discovery.FilteredBy(
discovery.ResourcePredicateFunc(filterByVerbs),
And(filterByVerbs, skipSubresource),
serverResources,
)

Expand Down Expand Up @@ -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) {
Expand Down