Merge pull request #111096 from brianpursley/api-resources-categories
Add categories to kubectl api-resources wide output and add --categories flag Kubernetes-commit: afbce897a9c7956fd3219b64abacf46fa5d3e6aa
This commit is contained in:
commit
5592f5001b
|
@ -66,6 +66,7 @@ type APIResourceOptions struct {
|
|||
Verbs []string
|
||||
NoHeaders bool
|
||||
Cached bool
|
||||
Categories []string
|
||||
|
||||
genericclioptions.IOStreams
|
||||
}
|
||||
|
@ -109,6 +110,7 @@ func NewCmdAPIResources(f cmdutil.Factory, ioStreams genericclioptions.IOStreams
|
|||
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 list of resources 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.")
|
||||
cmd.Flags().StringSliceVar(&o.Categories, "categories", o.Categories, "Limit to resources that belong the the specified categories.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -185,6 +187,10 @@ func (o *APIResourceOptions) RunAPIResources(cmd *cobra.Command, f cmdutil.Facto
|
|||
if len(o.Verbs) > 0 && !sets.NewString(resource.Verbs...).HasAll(o.Verbs...) {
|
||||
continue
|
||||
}
|
||||
// filter to resources that belong to the specified categories
|
||||
if len(o.Categories) > 0 && !sets.NewString(resource.Categories...).HasAll(o.Categories...) {
|
||||
continue
|
||||
}
|
||||
resources = append(resources, groupResource{
|
||||
APIGroup: gv.Group,
|
||||
APIGroupVersion: gv.String(),
|
||||
|
@ -211,13 +217,14 @@ func (o *APIResourceOptions) RunAPIResources(cmd *cobra.Command, f cmdutil.Facto
|
|||
errs = append(errs, err)
|
||||
}
|
||||
case "wide":
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\t%v\n",
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\t%v\t%v\n",
|
||||
r.APIResource.Name,
|
||||
strings.Join(r.APIResource.ShortNames, ","),
|
||||
r.APIGroupVersion,
|
||||
r.APIResource.Namespaced,
|
||||
r.APIResource.Kind,
|
||||
r.APIResource.Verbs); err != nil {
|
||||
strings.Join(r.APIResource.Verbs, ","),
|
||||
strings.Join(r.APIResource.Categories, ",")); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
case "":
|
||||
|
@ -241,7 +248,7 @@ func (o *APIResourceOptions) RunAPIResources(cmd *cobra.Command, f cmdutil.Facto
|
|||
func printContextHeaders(out io.Writer, output string) error {
|
||||
columnNames := []string{"NAME", "SHORTNAMES", "APIVERSION", "NAMESPACED", "KIND"}
|
||||
if output == "wide" {
|
||||
columnNames = append(columnNames, "VERBS")
|
||||
columnNames = append(columnNames, "VERBS", "CATEGORIES")
|
||||
}
|
||||
_, err := fmt.Fprintf(out, "%s\n", strings.Join(columnNames, "\t"))
|
||||
return err
|
||||
|
|
|
@ -110,6 +110,7 @@ func TestAPIResourcesRun(t *testing.T) {
|
|||
Kind: "Foo",
|
||||
Verbs: []string{"get", "list"},
|
||||
ShortNames: []string{"f", "fo"},
|
||||
Categories: []string{"some-category"},
|
||||
},
|
||||
{
|
||||
Name: "bars",
|
||||
|
@ -117,6 +118,7 @@ func TestAPIResourcesRun(t *testing.T) {
|
|||
Kind: "Bar",
|
||||
Verbs: []string{"get", "list", "create"},
|
||||
ShortNames: []string{},
|
||||
Categories: []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -129,6 +131,7 @@ func TestAPIResourcesRun(t *testing.T) {
|
|||
Kind: "Baz",
|
||||
Verbs: []string{"get", "list", "create", "delete"},
|
||||
ShortNames: []string{"b"},
|
||||
Categories: []string{"some-category", "another-category"},
|
||||
},
|
||||
{
|
||||
Name: "NoVerbs",
|
||||
|
@ -136,6 +139,7 @@ func TestAPIResourcesRun(t *testing.T) {
|
|||
Kind: "NoVerbs",
|
||||
Verbs: []string{},
|
||||
ShortNames: []string{"b"},
|
||||
Categories: []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -189,10 +193,10 @@ bazzes b somegroup/v1 true Baz
|
|||
commandSetupFn: func(cmd *cobra.Command) {
|
||||
cmd.Flags().Set("output", "wide")
|
||||
},
|
||||
expectedOutput: `NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS
|
||||
bars v1 true Bar [get list create]
|
||||
foos f,fo v1 false Foo [get list]
|
||||
bazzes b somegroup/v1 true Baz [get list create delete]
|
||||
expectedOutput: `NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS CATEGORIES
|
||||
bars v1 true Bar get,list,create
|
||||
foos f,fo v1 false Foo get,list some-category
|
||||
bazzes b somegroup/v1 true Baz get,list,create,delete some-category,another-category
|
||||
`,
|
||||
expectedInvalidations: 1,
|
||||
},
|
||||
|
@ -236,6 +240,27 @@ bazzes b somegroup/v1 true Baz
|
|||
},
|
||||
expectedOutput: `NAME SHORTNAMES APIVERSION NAMESPACED KIND
|
||||
bazzes b somegroup/v1 true Baz
|
||||
`,
|
||||
expectedInvalidations: 1,
|
||||
},
|
||||
{
|
||||
name: "single category",
|
||||
commandSetupFn: func(cmd *cobra.Command) {
|
||||
cmd.Flags().Set("categories", "some-category")
|
||||
},
|
||||
expectedOutput: `NAME SHORTNAMES APIVERSION NAMESPACED KIND
|
||||
foos f,fo v1 false Foo
|
||||
bazzes b somegroup/v1 true Baz
|
||||
`,
|
||||
expectedInvalidations: 1,
|
||||
},
|
||||
{
|
||||
name: "multiple categories",
|
||||
commandSetupFn: func(cmd *cobra.Command) {
|
||||
cmd.Flags().Set("categories", "some-category,another-category")
|
||||
},
|
||||
expectedOutput: `NAME SHORTNAMES APIVERSION NAMESPACED KIND
|
||||
bazzes b somegroup/v1 true Baz
|
||||
`,
|
||||
expectedInvalidations: 1,
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue