2 Copyright 2017 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
20 "k8s.io/apimachinery/pkg/runtime/schema"
21 "k8s.io/client-go/discovery"
24 // CategoryExpander maps category strings to GroupResouces.
25 // Categories are classification or 'tag' of a group of resources.
26 type CategoryExpander interface {
27 Expand(category string) ([]schema.GroupResource, bool)
30 // SimpleCategoryExpander implements CategoryExpander interface
31 // using a static mapping of categories to GroupResource mapping.
32 type SimpleCategoryExpander struct {
33 Expansions map[string][]schema.GroupResource
36 // Expand fulfills CategoryExpander
37 func (e SimpleCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
38 ret, ok := e.Expansions[category]
42 // discoveryCategoryExpander struct lets a REST Client wrapper (discoveryClient) to retrieve list of APIResourceList,
43 // and then convert to fallbackExpander
44 type discoveryCategoryExpander struct {
45 discoveryClient discovery.DiscoveryInterface
48 // NewDiscoveryCategoryExpander returns a category expander that makes use of the "categories" fields from
49 // the API, found through the discovery client. In case of any error or no category found (which likely
50 // means we're at a cluster prior to categories support, fallback to the expander provided.
51 func NewDiscoveryCategoryExpander(client discovery.DiscoveryInterface) CategoryExpander {
53 panic("Please provide discovery client to shortcut expander")
55 return discoveryCategoryExpander{discoveryClient: client}
58 // Expand fulfills CategoryExpander
59 func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
60 // Get all supported resources for groups and versions from server, if no resource found, fallback anyway.
61 apiResourceLists, _ := e.discoveryClient.ServerResources()
62 if len(apiResourceLists) == 0 {
66 discoveredExpansions := map[string][]schema.GroupResource{}
67 for _, apiResourceList := range apiResourceLists {
68 gv, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
72 // Collect GroupVersions by categories
73 for _, apiResource := range apiResourceList.APIResources {
74 if categories := apiResource.Categories; len(categories) > 0 {
75 for _, category := range categories {
76 groupResource := schema.GroupResource{
78 Resource: apiResource.Name,
80 discoveredExpansions[category] = append(discoveredExpansions[category], groupResource)
86 ret, ok := discoveredExpansions[category]
90 // UnionCategoryExpander implements CategoryExpander interface.
91 // It maps given category string to union of expansions returned by all the CategoryExpanders in the list.
92 type UnionCategoryExpander []CategoryExpander
94 // Expand fulfills CategoryExpander
95 func (u UnionCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
96 ret := []schema.GroupResource{}
99 // Expand the category for each CategoryExpander in the list and merge/combine the results.
100 for _, expansion := range u {
101 curr, currOk := expansion.Expand(category)
103 for _, currGR := range curr {
105 for _, existing := range ret {
106 if existing == currGR {
112 ret = append(ret, currGR)