Skip to content

Commit 3ebee37

Browse files
authored
Merge pull request kubernetes#81971 from laddng/api-resources-sort-by-flag
Added a new `--sort-by` flag to kubectl api-resources command
2 parents 0c91af2 + cf492e9 commit 3ebee37

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/apiresources/apiresources.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ var (
4242
# Print the supported API Resources with more information
4343
kubectl api-resources -o wide
4444
45+
# Print the supported API Resources sorted by a column
46+
kubectl api-resources --sort-by=name
47+
4548
# Print the supported namespaced resources
4649
kubectl api-resources --namespaced=true
4750
@@ -56,6 +59,7 @@ var (
5659
// As new fields are added, add them here instead of referencing the cmd.Flags()
5760
type APIResourceOptions struct {
5861
Output string
62+
SortBy string
5963
APIGroup string
6064
Namespaced bool
6165
Verbs []string
@@ -101,6 +105,7 @@ func NewCmdAPIResources(f cmdutil.Factory, ioStreams genericclioptions.IOStreams
101105
cmd.Flags().StringVar(&o.APIGroup, "api-group", o.APIGroup, "Limit to resources in the specified API group.")
102106
cmd.Flags().BoolVar(&o.Namespaced, "namespaced", o.Namespaced, "If false, non-namespaced resources will be returned, otherwise returning namespaced resources by default.")
103107
cmd.Flags().StringSliceVar(&o.Verbs, "verbs", o.Verbs, "Limit to resources that support the specified verbs.")
108+
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'.")
104109
cmd.Flags().BoolVar(&o.Cached, "cached", o.Cached, "Use the cached list of resources if available.")
105110
return cmd
106111
}
@@ -111,6 +116,12 @@ func (o *APIResourceOptions) Validate() error {
111116
if !supportedOutputTypes.Has(o.Output) {
112117
return fmt.Errorf("--output %v is not available", o.Output)
113118
}
119+
supportedSortTypes := sets.NewString("", "name", "kind")
120+
if len(o.SortBy) > 0 {
121+
if !supportedSortTypes.Has(o.SortBy) {
122+
return fmt.Errorf("--sort-by accepts only name or kind")
123+
}
124+
}
114125
return nil
115126
}
116127

@@ -185,7 +196,7 @@ func (o *APIResourceOptions) RunAPIResources(cmd *cobra.Command, f cmdutil.Facto
185196
}
186197
}
187198

188-
sort.Stable(sortableGroupResource(resources))
199+
sort.Stable(sortableResource{resources, o.SortBy})
189200
for _, r := range resources {
190201
switch o.Output {
191202
case "name":
@@ -233,16 +244,31 @@ func printContextHeaders(out io.Writer, output string) error {
233244
return err
234245
}
235246

236-
type sortableGroupResource []groupResource
247+
type sortableResource struct {
248+
resources []groupResource
249+
sortBy string
250+
}
237251

238-
func (s sortableGroupResource) Len() int { return len(s) }
239-
func (s sortableGroupResource) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
240-
func (s sortableGroupResource) Less(i, j int) bool {
241-
ret := strings.Compare(s[i].APIGroup, s[j].APIGroup)
252+
func (s sortableResource) Len() int { return len(s.resources) }
253+
func (s sortableResource) Swap(i, j int) {
254+
s.resources[i], s.resources[j] = s.resources[j], s.resources[i]
255+
}
256+
func (s sortableResource) Less(i, j int) bool {
257+
ret := strings.Compare(s.compareValues(i, j))
242258
if ret > 0 {
243259
return false
244260
} else if ret == 0 {
245-
return strings.Compare(s[i].APIResource.Name, s[j].APIResource.Name) < 0
261+
return strings.Compare(s.resources[i].APIResource.Name, s.resources[j].APIResource.Name) < 0
246262
}
247263
return true
248264
}
265+
266+
func (s sortableResource) compareValues(i, j int) (string, string) {
267+
switch s.sortBy {
268+
case "name":
269+
return s.resources[i].APIResource.Name, s.resources[j].APIResource.Name
270+
case "kind":
271+
return s.resources[i].APIResource.Kind, s.resources[j].APIResource.Kind
272+
}
273+
return s.resources[i].APIGroup, s.resources[j].APIGroup
274+
}

0 commit comments

Comments
 (0)