...

Source file src/k8s.io/client-go/restmapper/category_expansion.go

Documentation: k8s.io/client-go/restmapper

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     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
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    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.
    15  */
    16  
    17  package restmapper
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/runtime/schema"
    21  	"k8s.io/client-go/discovery"
    22  )
    23  
    24  // CategoryExpander maps category strings to GroupResources.
    25  // Categories are classification or 'tag' of a group of resources.
    26  type CategoryExpander interface {
    27  	Expand(category string) ([]schema.GroupResource, bool)
    28  }
    29  
    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
    34  }
    35  
    36  // Expand fulfills CategoryExpander
    37  func (e SimpleCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
    38  	ret, ok := e.Expansions[category]
    39  	return ret, ok
    40  }
    41  
    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
    46  }
    47  
    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 {
    52  	if client == nil {
    53  		panic("Please provide discovery client to shortcut expander")
    54  	}
    55  	return discoveryCategoryExpander{discoveryClient: client}
    56  }
    57  
    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.ServerGroupsAndResources()
    62  	if len(apiResourceLists) == 0 {
    63  		return nil, false
    64  	}
    65  
    66  	discoveredExpansions := map[string][]schema.GroupResource{}
    67  	for _, apiResourceList := range apiResourceLists {
    68  		gv, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
    69  		if err != nil {
    70  			continue
    71  		}
    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{
    77  						Group:    gv.Group,
    78  						Resource: apiResource.Name,
    79  					}
    80  					discoveredExpansions[category] = append(discoveredExpansions[category], groupResource)
    81  				}
    82  			}
    83  		}
    84  	}
    85  
    86  	ret, ok := discoveredExpansions[category]
    87  	return ret, ok
    88  }
    89  
    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
    93  
    94  // Expand fulfills CategoryExpander
    95  func (u UnionCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
    96  	ret := []schema.GroupResource{}
    97  	ok := false
    98  
    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)
   102  
   103  		for _, currGR := range curr {
   104  			found := false
   105  			for _, existing := range ret {
   106  				if existing == currGR {
   107  					found = true
   108  					break
   109  				}
   110  			}
   111  			if !found {
   112  				ret = append(ret, currGR)
   113  			}
   114  		}
   115  		ok = ok || currOk
   116  	}
   117  
   118  	return ret, ok
   119  }
   120  

View as plain text