Skip to content

Commit

Permalink
Merge pull request #6635 from 27149chen/skip-subresource
Browse files Browse the repository at this point in the history
skip subresource in resource discovery
  • Loading branch information
reasonerjt authored Aug 22, 2023
2 parents a62f2fa + 0f9e582 commit 3e61386
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit 3e61386

Please sign in to comment.