Merge pull request #59127 from fanzhangio/category

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Cleanup and add category doc

**What this PR does / why we need it**:
CategoryExpender interface and some implements structs in package pkg/kubectl/categories are quite confusing to contributors due to the insufficiency documents and illustration.

This PR does:
1. Clean up some useless or vague comments
2. Add illustration for CategoryExpender and implements structs

@pwittrock @droot
pull/6/head
Kubernetes Submit Queue 2018-02-08 19:03:50 -08:00 committed by GitHub
commit 23e4133cda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 3 deletions

View File

@ -21,10 +21,14 @@ import (
"k8s.io/client-go/discovery"
)
// CategoryExpander maps category strings to GroupResouces.
// Categories are classification or 'tag' of a group of resources.
type CategoryExpander interface {
Expand(category string) ([]schema.GroupResource, bool)
}
// SimpleCategoryExpander implements CategoryExpander interface
// using a static mapping of categories to GroupResource mapping.
type SimpleCategoryExpander struct {
Expansions map[string][]schema.GroupResource
}
@ -34,6 +38,8 @@ func (e SimpleCategoryExpander) Expand(category string) ([]schema.GroupResource,
return ret, ok
}
// discoveryCategoryExpander struct lets a REST Client wrapper (discoveryClient) to retrieve list of APIResourceList,
// and then convert to fallbackExpander
type discoveryCategoryExpander struct {
fallbackExpander CategoryExpander
discoveryClient discovery.DiscoveryInterface
@ -50,6 +56,7 @@ func NewDiscoveryCategoryExpander(fallbackExpander CategoryExpander, client disc
}
func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
// Get all supported resources for groups and versions from server, if no resource found, fallback anyway.
apiResourceLists, _ := e.discoveryClient.ServerResources()
if len(apiResourceLists) == 0 {
return e.fallbackExpander.Expand(category)
@ -62,7 +69,7 @@ func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResour
if err != nil {
return e.fallbackExpander.Expand(category)
}
// Collect GroupVersions by categories
for _, apiResource := range apiResourceList.APIResources {
if categories := apiResource.Categories; len(categories) > 0 {
for _, category := range categories {
@ -86,14 +93,15 @@ func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResour
return ret, ok
}
// discoveryFilteredExpander expands the given CategoryExpander (delegate) to filter group and resource returned from server
type discoveryFilteredExpander struct {
delegate CategoryExpander
discoveryClient discovery.DiscoveryInterface
}
// NewDiscoveryFilteredExpander returns a category expander that filters the returned groupresources by
// what the server has available
// NewDiscoveryFilteredExpander returns a category expander that filters the returned groupresources
// by what the server has available
func NewDiscoveryFilteredExpander(delegate CategoryExpander, client discovery.DiscoveryInterface) (discoveryFilteredExpander, error) {
if client == nil {
panic("Please provide discovery client to shortcut expander")
@ -129,12 +137,15 @@ func (e discoveryFilteredExpander) Expand(category string) ([]schema.GroupResour
return available, ok
}
// UnionCategoryExpander implements CategoryExpander interface.
// It maps given category string to union of expansions returned by all the CategoryExpanders in the list.
type UnionCategoryExpander []CategoryExpander
func (u UnionCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
ret := []schema.GroupResource{}
ok := false
// Expand the category for each CategoryExpander in the list and merge/combine the results.
for _, expansion := range u {
curr, currOk := expansion.Expand(category)