123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /*
- Copyright 2014 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package cmd
- import (
- "fmt"
- "io"
- "strings"
- "github.com/renstrom/dedent"
- "github.com/spf13/cobra"
- apierrors "k8s.io/kubernetes/pkg/api/errors"
- "k8s.io/kubernetes/pkg/api/meta"
- "k8s.io/kubernetes/pkg/kubectl"
- cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
- "k8s.io/kubernetes/pkg/kubectl/resource"
- "k8s.io/kubernetes/pkg/runtime"
- utilerrors "k8s.io/kubernetes/pkg/util/errors"
- )
- // DescribeOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
- // referencing the cmd.Flags()
- type DescribeOptions struct {
- Filenames []string
- Recursive bool
- }
- var (
- describe_long = dedent.Dedent(`
- Show details of a specific resource or group of resources.
- This command joins many API calls together to form a detailed description of a
- given resource or group of resources.
- $ kubectl describe TYPE NAME_PREFIX
- will first check for an exact match on TYPE and NAME_PREFIX. If no such resource
- exists, it will output details for every resource that has a name prefixed with NAME_PREFIX.
- `) + valid_resources
- describe_example = dedent.Dedent(`
- # Describe a node
- kubectl describe nodes kubernetes-minion-emt8.c.myproject.internal
- # Describe a pod
- kubectl describe pods/nginx
- # Describe a pod identified by type and name in "pod.json"
- kubectl describe -f pod.json
- # Describe all pods
- kubectl describe pods
- # Describe pods by label name=myLabel
- kubectl describe po -l name=myLabel
- # Describe all pods managed by the 'frontend' replication controller (rc-created pods
- # get the name of the rc as a prefix in the pod the name).
- kubectl describe pods frontend`)
- )
- func NewCmdDescribe(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command {
- options := &DescribeOptions{}
- describerSettings := &kubectl.DescriberSettings{}
- validArgs := kubectl.DescribableResources()
- argAliases := kubectl.ResourceAliases(validArgs)
- cmd := &cobra.Command{
- Use: "describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME)",
- Short: "Show details of a specific resource or group of resources",
- Long: describe_long,
- Example: describe_example,
- Run: func(cmd *cobra.Command, args []string) {
- err := RunDescribe(f, out, cmdErr, cmd, args, options, describerSettings)
- cmdutil.CheckErr(err)
- },
- ValidArgs: validArgs,
- ArgAliases: argAliases,
- }
- usage := "Filename, directory, or URL to a file containing the resource to describe"
- kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
- cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
- cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
- cmd.Flags().Bool("all-namespaces", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
- cmd.Flags().BoolVar(&describerSettings.ShowEvents, "show-events", true, "If true, display events related to the described object.")
- cmdutil.AddInclude3rdPartyFlags(cmd)
- return cmd
- }
- func RunDescribe(f *cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string, options *DescribeOptions, describerSettings *kubectl.DescriberSettings) error {
- selector := cmdutil.GetFlagString(cmd, "selector")
- allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces")
- cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
- if err != nil {
- return err
- }
- if allNamespaces {
- enforceNamespace = false
- }
- if len(args) == 0 && len(options.Filenames) == 0 {
- fmt.Fprint(cmdErr, "You must specify the type of resource to describe. ", valid_resources)
- return cmdutil.UsageError(cmd, "Required resource not specified.")
- }
- mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
- r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
- ContinueOnError().
- NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
- FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
- SelectorParam(selector).
- ResourceTypeOrNameArgs(true, args...).
- Flatten().
- Do()
- err = r.Err()
- if err != nil {
- return err
- }
- allErrs := []error{}
- infos, err := r.Infos()
- if err != nil {
- if apierrors.IsNotFound(err) && len(args) == 2 {
- return DescribeMatchingResources(mapper, typer, f, cmdNamespace, args[0], args[1], describerSettings, out, err)
- }
- allErrs = append(allErrs, err)
- }
- first := true
- for _, info := range infos {
- mapping := info.ResourceMapping()
- describer, err := f.Describer(mapping)
- if err != nil {
- allErrs = append(allErrs, err)
- continue
- }
- s, err := describer.Describe(info.Namespace, info.Name, *describerSettings)
- if err != nil {
- allErrs = append(allErrs, err)
- continue
- }
- if first {
- first = false
- fmt.Fprint(out, s)
- } else {
- fmt.Fprintf(out, "\n\n%s", s)
- }
- }
- return utilerrors.NewAggregate(allErrs)
- }
- func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper, f *cmdutil.Factory, namespace, rsrc, prefix string, describerSettings *kubectl.DescriberSettings, out io.Writer, originalError error) error {
- r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
- NamespaceParam(namespace).DefaultNamespace().
- ResourceTypeOrNameArgs(true, rsrc).
- SingleResourceType().
- Flatten().
- Do()
- mapping, err := r.ResourceMapping()
- if err != nil {
- return err
- }
- describer, err := f.Describer(mapping)
- if err != nil {
- return err
- }
- infos, err := r.Infos()
- if err != nil {
- return err
- }
- isFound := false
- for ix := range infos {
- info := infos[ix]
- if strings.HasPrefix(info.Name, prefix) {
- isFound = true
- s, err := describer.Describe(info.Namespace, info.Name, *describerSettings)
- if err != nil {
- return err
- }
- fmt.Fprintf(out, "%s\n", s)
- }
- }
- if !isFound {
- return originalError
- }
- return nil
- }
|