Skip to content

Commit

Permalink
skip subresource in resource discovery
Browse files Browse the repository at this point in the history
Signed-off-by: lou <alex1988@outlook.com>
  • Loading branch information
27149chen authored and Daniel Jiang committed Aug 22, 2023
1 parent 0c0ccf9 commit 1ae745c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelogs/unreleased/6688-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

Check warning on line 260 in pkg/discovery/helper.go

View check run for this annotation

Codecov / codecov/patch

pkg/discovery/helper.go#L260

Added line #L260 was not covered by tests
}

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, "/")

Check warning on line 269 in pkg/discovery/helper.go

View check run for this annotation

Codecov / codecov/patch

pkg/discovery/helper.go#L267-L269

Added lines #L267 - L269 were not covered by tests
}

// 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 1ae745c

Please sign in to comment.