mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Merge pull request #63254 from liggitt/api-resources
Automatic merge from submit-queue (batch tested with PRs 60890, 63244, 60741, 63254). 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>. Add name output and verb filtering to api-resources This allows `kubectl api-resources -o name` to be used as input to `kubectl get ...` to see all resources still existing in a given namespace: Example: ```sh kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -o name -n foo ``` Release note: ```release-note `kubectl api-resources` now supports filtering to resources supporting specific verbs, and can output fully qualified resource names suitable for combining with commands like `kubectl get` ```
This commit is contained in:
commit
d6967f358e
@ -4885,12 +4885,15 @@ runTests() {
|
|||||||
|
|
||||||
record_command run_RESTMapper_evaluation_tests
|
record_command run_RESTMapper_evaluation_tests
|
||||||
|
|
||||||
|
# find all resources
|
||||||
|
kubectl "${kube_flags[@]}" api-resources
|
||||||
|
# find all namespaced resources that support list by name and get them
|
||||||
|
kubectl "${kube_flags[@]}" api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl "${kube_flags[@]}" get -o name
|
||||||
|
|
||||||
################
|
################
|
||||||
# Cluster Role #
|
# Cluster Role #
|
||||||
################
|
################
|
||||||
|
|
||||||
kubectl "${kube_flags[@]}" api-resources
|
|
||||||
|
|
||||||
if kube::test::if_supports_resource "${clusterroles}" ; then
|
if kube::test::if_supports_resource "${clusterroles}" ; then
|
||||||
record_command run_clusterroles_tests
|
record_command run_clusterroles_tests
|
||||||
fi
|
fi
|
||||||
|
@ -57,6 +57,7 @@ type ApiResourcesOptions struct {
|
|||||||
Output string
|
Output string
|
||||||
APIGroup string
|
APIGroup string
|
||||||
Namespaced bool
|
Namespaced bool
|
||||||
|
Verbs []string
|
||||||
NoHeaders bool
|
NoHeaders bool
|
||||||
|
|
||||||
genericclioptions.IOStreams
|
genericclioptions.IOStreams
|
||||||
@ -90,8 +91,9 @@ func NewCmdApiResources(f cmdutil.Factory, ioStreams genericclioptions.IOStreams
|
|||||||
}
|
}
|
||||||
cmdutil.AddOutputFlags(cmd)
|
cmdutil.AddOutputFlags(cmd)
|
||||||
cmdutil.AddNoHeadersFlags(cmd)
|
cmdutil.AddNoHeadersFlags(cmd)
|
||||||
cmd.Flags().StringVar(&o.APIGroup, "api-group", "", "The API group to use when talking to the server.")
|
cmd.Flags().StringVar(&o.APIGroup, "api-group", "", "Limit to resources in the specified API group.")
|
||||||
cmd.Flags().BoolVar(&o.Namespaced, "namespaced", true, "Namespaced indicates if a resource is namespaced or not.")
|
cmd.Flags().BoolVar(&o.Namespaced, "namespaced", true, "Namespaced indicates if a resource is namespaced or not.")
|
||||||
|
cmd.Flags().StringSliceVar(&o.Verbs, "verbs", o.Verbs, "Limit to resources that support the specified verbs.")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +105,7 @@ func (o *ApiResourcesOptions) Complete(cmd *cobra.Command) error {
|
|||||||
|
|
||||||
func (o *ApiResourcesOptions) Validate(cmd *cobra.Command) error {
|
func (o *ApiResourcesOptions) Validate(cmd *cobra.Command) error {
|
||||||
validOutputTypes := sets.NewString("", "json", "yaml", "wide", "name", "custom-columns", "custom-columns-file", "go-template", "go-template-file", "jsonpath", "jsonpath-file")
|
validOutputTypes := sets.NewString("", "json", "yaml", "wide", "name", "custom-columns", "custom-columns-file", "go-template", "go-template-file", "jsonpath", "jsonpath-file")
|
||||||
supportedOutputTypes := sets.NewString("", "wide")
|
supportedOutputTypes := sets.NewString("", "wide", "name")
|
||||||
outputFormat := cmdutil.GetFlagString(cmd, "output")
|
outputFormat := cmdutil.GetFlagString(cmd, "output")
|
||||||
if !validOutputTypes.Has(outputFormat) {
|
if !validOutputTypes.Has(outputFormat) {
|
||||||
return fmt.Errorf("output must be one of '' or 'wide': %v", outputFormat)
|
return fmt.Errorf("output must be one of '' or 'wide': %v", outputFormat)
|
||||||
@ -156,6 +158,10 @@ func (o *ApiResourcesOptions) RunApiResources(cmd *cobra.Command, f cmdutil.Fact
|
|||||||
if nsChanged && o.Namespaced != resource.Namespaced {
|
if nsChanged && o.Namespaced != resource.Namespaced {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// filter to resources that support the specified verbs
|
||||||
|
if len(o.Verbs) > 0 && !sets.NewString(resource.Verbs...).HasAll(o.Verbs...) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
resources = append(resources, groupResource{
|
resources = append(resources, groupResource{
|
||||||
APIGroup: gv.Group,
|
APIGroup: gv.Group,
|
||||||
APIResource: resource,
|
APIResource: resource,
|
||||||
@ -163,7 +169,7 @@ func (o *ApiResourcesOptions) RunApiResources(cmd *cobra.Command, f cmdutil.Fact
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if o.NoHeaders == false {
|
if o.NoHeaders == false && o.Output != "name" {
|
||||||
if err = printContextHeaders(w, o.Output); err != nil {
|
if err = printContextHeaders(w, o.Output); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -171,7 +177,16 @@ func (o *ApiResourcesOptions) RunApiResources(cmd *cobra.Command, f cmdutil.Fact
|
|||||||
|
|
||||||
sort.Stable(sortableGroupResource(resources))
|
sort.Stable(sortableGroupResource(resources))
|
||||||
for _, r := range resources {
|
for _, r := range resources {
|
||||||
if o.Output == "wide" {
|
switch o.Output {
|
||||||
|
case "name":
|
||||||
|
name := r.APIResource.Name
|
||||||
|
if len(r.APIGroup) > 0 {
|
||||||
|
name += "." + r.APIGroup
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(w, "%s\n", name); err != nil {
|
||||||
|
return 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\n",
|
||||||
r.APIResource.Name,
|
r.APIResource.Name,
|
||||||
strings.Join(r.APIResource.ShortNames, ","),
|
strings.Join(r.APIResource.ShortNames, ","),
|
||||||
@ -181,7 +196,7 @@ func (o *ApiResourcesOptions) RunApiResources(cmd *cobra.Command, f cmdutil.Fact
|
|||||||
r.APIResource.Verbs); err != nil {
|
r.APIResource.Verbs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
case "":
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\n",
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\n",
|
||||||
r.APIResource.Name,
|
r.APIResource.Name,
|
||||||
strings.Join(r.APIResource.ShortNames, ","),
|
strings.Join(r.APIResource.ShortNames, ","),
|
||||||
|
Loading…
Reference in New Issue
Block a user