Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
bfa3029c
Commit
bfa3029c
authored
Jan 31, 2018
by
Fan Zhang
Committed by
fanzhangio
Feb 08, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup and add category doc
- Clean up some useless or vague comments. Fixed typos - Add illustration for CategoryExpender and implements structs
parent
c3a92d0b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
3 deletions
+14
-3
categories.go
pkg/kubectl/categories/categories.go
+14
-3
No files found.
pkg/kubectl/categories/categories.go
View file @
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment