Unverified Commit 23e4133c authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

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
parents d6625f85 bfa3029c
...@@ -21,10 +21,14 @@ import ( ...@@ -21,10 +21,14 @@ import (
"k8s.io/client-go/discovery" "k8s.io/client-go/discovery"
) )
// CategoryExpander maps category strings to GroupResouces.
// Categories are classification or 'tag' of a group of resources.
type CategoryExpander interface { type CategoryExpander interface {
Expand(category string) ([]schema.GroupResource, bool) Expand(category string) ([]schema.GroupResource, bool)
} }
// SimpleCategoryExpander implements CategoryExpander interface
// using a static mapping of categories to GroupResource mapping.
type SimpleCategoryExpander struct { type SimpleCategoryExpander struct {
Expansions map[string][]schema.GroupResource Expansions map[string][]schema.GroupResource
} }
...@@ -34,6 +38,8 @@ func (e SimpleCategoryExpander) Expand(category string) ([]schema.GroupResource, ...@@ -34,6 +38,8 @@ func (e SimpleCategoryExpander) Expand(category string) ([]schema.GroupResource,
return ret, ok return ret, ok
} }
// discoveryCategoryExpander struct lets a REST Client wrapper (discoveryClient) to retrieve list of APIResourceList,
// and then convert to fallbackExpander
type discoveryCategoryExpander struct { type discoveryCategoryExpander struct {
fallbackExpander CategoryExpander fallbackExpander CategoryExpander
discoveryClient discovery.DiscoveryInterface discoveryClient discovery.DiscoveryInterface
...@@ -50,6 +56,7 @@ func NewDiscoveryCategoryExpander(fallbackExpander CategoryExpander, client disc ...@@ -50,6 +56,7 @@ func NewDiscoveryCategoryExpander(fallbackExpander CategoryExpander, client disc
} }
func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) { 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() apiResourceLists, _ := e.discoveryClient.ServerResources()
if len(apiResourceLists) == 0 { if len(apiResourceLists) == 0 {
return e.fallbackExpander.Expand(category) return e.fallbackExpander.Expand(category)
...@@ -62,7 +69,7 @@ func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResour ...@@ -62,7 +69,7 @@ func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResour
if err != nil { if err != nil {
return e.fallbackExpander.Expand(category) return e.fallbackExpander.Expand(category)
} }
// Collect GroupVersions by categories
for _, apiResource := range apiResourceList.APIResources { for _, apiResource := range apiResourceList.APIResources {
if categories := apiResource.Categories; len(categories) > 0 { if categories := apiResource.Categories; len(categories) > 0 {
for _, category := range categories { for _, category := range categories {
...@@ -86,14 +93,15 @@ func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResour ...@@ -86,14 +93,15 @@ func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResour
return ret, ok return ret, ok
} }
// discoveryFilteredExpander expands the given CategoryExpander (delegate) to filter group and resource returned from server
type discoveryFilteredExpander struct { type discoveryFilteredExpander struct {
delegate CategoryExpander delegate CategoryExpander
discoveryClient discovery.DiscoveryInterface discoveryClient discovery.DiscoveryInterface
} }
// NewDiscoveryFilteredExpander returns a category expander that filters the returned groupresources by // NewDiscoveryFilteredExpander returns a category expander that filters the returned groupresources
// what the server has available // by what the server has available
func NewDiscoveryFilteredExpander(delegate CategoryExpander, client discovery.DiscoveryInterface) (discoveryFilteredExpander, error) { func NewDiscoveryFilteredExpander(delegate CategoryExpander, client discovery.DiscoveryInterface) (discoveryFilteredExpander, error) {
if client == nil { if client == nil {
panic("Please provide discovery client to shortcut expander") panic("Please provide discovery client to shortcut expander")
...@@ -129,12 +137,15 @@ func (e discoveryFilteredExpander) Expand(category string) ([]schema.GroupResour ...@@ -129,12 +137,15 @@ func (e discoveryFilteredExpander) Expand(category string) ([]schema.GroupResour
return available, ok 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 type UnionCategoryExpander []CategoryExpander
func (u UnionCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) { func (u UnionCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) {
ret := []schema.GroupResource{} ret := []schema.GroupResource{}
ok := false ok := false
// Expand the category for each CategoryExpander in the list and merge/combine the results.
for _, expansion := range u { for _, expansion := range u {
curr, currOk := expansion.Expand(category) curr, currOk := expansion.Expand(category)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment