-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a new --sort-by
flag to kubectl api-resources command
#81971
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
laddng:api-resources-sort-by-flag
Oct 19, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ var ( | |
# Print the supported API Resources with more information | ||
kubectl api-resources -o wide | ||
|
||
# Print the supported API Resources sorted by a column | ||
kubectl api-resources --sort-by=name | ||
|
||
# Print the supported namespaced resources | ||
kubectl api-resources --namespaced=true | ||
|
||
|
@@ -56,6 +59,7 @@ var ( | |
// As new fields are added, add them here instead of referencing the cmd.Flags() | ||
type APIResourceOptions struct { | ||
Output string | ||
SortBy string | ||
APIGroup string | ||
Namespaced bool | ||
Verbs []string | ||
|
@@ -101,6 +105,7 @@ func NewCmdAPIResources(f cmdutil.Factory, ioStreams genericclioptions.IOStreams | |
cmd.Flags().StringVar(&o.APIGroup, "api-group", o.APIGroup, "Limit to resources in the specified API group.") | ||
cmd.Flags().BoolVar(&o.Namespaced, "namespaced", o.Namespaced, "If false, non-namespaced resources will be returned, otherwise returning namespaced resources by default.") | ||
cmd.Flags().StringSliceVar(&o.Verbs, "verbs", o.Verbs, "Limit to resources that support the specified verbs.") | ||
cmd.Flags().StringVar(&o.SortBy, "sort-by", o.SortBy, "If non-empty, sort nodes list using specified field. The field can be either 'name' or 'kind'.") | ||
cmd.Flags().BoolVar(&o.Cached, "cached", o.Cached, "Use the cached list of resources if available.") | ||
return cmd | ||
} | ||
|
@@ -111,6 +116,12 @@ func (o *APIResourceOptions) Validate() error { | |
if !supportedOutputTypes.Has(o.Output) { | ||
return fmt.Errorf("--output %v is not available", o.Output) | ||
} | ||
supportedSortTypes := sets.NewString("", "name", "kind") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be instantiated with |
||
if len(o.SortBy) > 0 { | ||
if !supportedSortTypes.Has(o.SortBy) { | ||
return fmt.Errorf("--sort-by accepts only name or kind") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
|
@@ -185,7 +196,7 @@ func (o *APIResourceOptions) RunAPIResources(cmd *cobra.Command, f cmdutil.Facto | |
} | ||
} | ||
|
||
sort.Stable(sortableGroupResource(resources)) | ||
sort.Stable(sortableResource{resources, o.SortBy}) | ||
for _, r := range resources { | ||
switch o.Output { | ||
case "name": | ||
|
@@ -233,16 +244,31 @@ func printContextHeaders(out io.Writer, output string) error { | |
return err | ||
} | ||
|
||
type sortableGroupResource []groupResource | ||
type sortableResource struct { | ||
resources []groupResource | ||
sortBy string | ||
} | ||
|
||
func (s sortableGroupResource) Len() int { return len(s) } | ||
func (s sortableGroupResource) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s sortableGroupResource) Less(i, j int) bool { | ||
ret := strings.Compare(s[i].APIGroup, s[j].APIGroup) | ||
func (s sortableResource) Len() int { return len(s.resources) } | ||
func (s sortableResource) Swap(i, j int) { | ||
s.resources[i], s.resources[j] = s.resources[j], s.resources[i] | ||
} | ||
func (s sortableResource) Less(i, j int) bool { | ||
ret := strings.Compare(s.compareValues(i, j)) | ||
if ret > 0 { | ||
return false | ||
} else if ret == 0 { | ||
return strings.Compare(s[i].APIResource.Name, s[j].APIResource.Name) < 0 | ||
return strings.Compare(s.resources[i].APIResource.Name, s.resources[j].APIResource.Name) < 0 | ||
} | ||
return true | ||
} | ||
|
||
func (s sortableResource) compareValues(i, j int) (string, string) { | ||
switch s.sortBy { | ||
case "name": | ||
return s.resources[i].APIResource.Name, s.resources[j].APIResource.Name | ||
case "kind": | ||
return s.resources[i].APIResource.Kind, s.resources[j].APIResource.Kind | ||
} | ||
return s.resources[i].APIGroup, s.resources[j].APIGroup | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorted by name