describe.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "strings"
  18. "github.com/renstrom/dedent"
  19. "github.com/spf13/cobra"
  20. apierrors "k8s.io/kubernetes/pkg/api/errors"
  21. "k8s.io/kubernetes/pkg/api/meta"
  22. "k8s.io/kubernetes/pkg/kubectl"
  23. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  24. "k8s.io/kubernetes/pkg/kubectl/resource"
  25. "k8s.io/kubernetes/pkg/runtime"
  26. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  27. )
  28. // DescribeOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  29. // referencing the cmd.Flags()
  30. type DescribeOptions struct {
  31. Filenames []string
  32. Recursive bool
  33. }
  34. var (
  35. describe_long = dedent.Dedent(`
  36. Show details of a specific resource or group of resources.
  37. This command joins many API calls together to form a detailed description of a
  38. given resource or group of resources.
  39. $ kubectl describe TYPE NAME_PREFIX
  40. will first check for an exact match on TYPE and NAME_PREFIX. If no such resource
  41. exists, it will output details for every resource that has a name prefixed with NAME_PREFIX.
  42. `) + valid_resources
  43. describe_example = dedent.Dedent(`
  44. # Describe a node
  45. kubectl describe nodes kubernetes-minion-emt8.c.myproject.internal
  46. # Describe a pod
  47. kubectl describe pods/nginx
  48. # Describe a pod identified by type and name in "pod.json"
  49. kubectl describe -f pod.json
  50. # Describe all pods
  51. kubectl describe pods
  52. # Describe pods by label name=myLabel
  53. kubectl describe po -l name=myLabel
  54. # Describe all pods managed by the 'frontend' replication controller (rc-created pods
  55. # get the name of the rc as a prefix in the pod the name).
  56. kubectl describe pods frontend`)
  57. )
  58. func NewCmdDescribe(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command {
  59. options := &DescribeOptions{}
  60. describerSettings := &kubectl.DescriberSettings{}
  61. validArgs := kubectl.DescribableResources()
  62. argAliases := kubectl.ResourceAliases(validArgs)
  63. cmd := &cobra.Command{
  64. Use: "describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME)",
  65. Short: "Show details of a specific resource or group of resources",
  66. Long: describe_long,
  67. Example: describe_example,
  68. Run: func(cmd *cobra.Command, args []string) {
  69. err := RunDescribe(f, out, cmdErr, cmd, args, options, describerSettings)
  70. cmdutil.CheckErr(err)
  71. },
  72. ValidArgs: validArgs,
  73. ArgAliases: argAliases,
  74. }
  75. usage := "Filename, directory, or URL to a file containing the resource to describe"
  76. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  77. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  78. cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
  79. 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.")
  80. cmd.Flags().BoolVar(&describerSettings.ShowEvents, "show-events", true, "If true, display events related to the described object.")
  81. cmdutil.AddInclude3rdPartyFlags(cmd)
  82. return cmd
  83. }
  84. func RunDescribe(f *cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string, options *DescribeOptions, describerSettings *kubectl.DescriberSettings) error {
  85. selector := cmdutil.GetFlagString(cmd, "selector")
  86. allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces")
  87. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  88. if err != nil {
  89. return err
  90. }
  91. if allNamespaces {
  92. enforceNamespace = false
  93. }
  94. if len(args) == 0 && len(options.Filenames) == 0 {
  95. fmt.Fprint(cmdErr, "You must specify the type of resource to describe. ", valid_resources)
  96. return cmdutil.UsageError(cmd, "Required resource not specified.")
  97. }
  98. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  99. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  100. ContinueOnError().
  101. NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
  102. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  103. SelectorParam(selector).
  104. ResourceTypeOrNameArgs(true, args...).
  105. Flatten().
  106. Do()
  107. err = r.Err()
  108. if err != nil {
  109. return err
  110. }
  111. allErrs := []error{}
  112. infos, err := r.Infos()
  113. if err != nil {
  114. if apierrors.IsNotFound(err) && len(args) == 2 {
  115. return DescribeMatchingResources(mapper, typer, f, cmdNamespace, args[0], args[1], describerSettings, out, err)
  116. }
  117. allErrs = append(allErrs, err)
  118. }
  119. first := true
  120. for _, info := range infos {
  121. mapping := info.ResourceMapping()
  122. describer, err := f.Describer(mapping)
  123. if err != nil {
  124. allErrs = append(allErrs, err)
  125. continue
  126. }
  127. s, err := describer.Describe(info.Namespace, info.Name, *describerSettings)
  128. if err != nil {
  129. allErrs = append(allErrs, err)
  130. continue
  131. }
  132. if first {
  133. first = false
  134. fmt.Fprint(out, s)
  135. } else {
  136. fmt.Fprintf(out, "\n\n%s", s)
  137. }
  138. }
  139. return utilerrors.NewAggregate(allErrs)
  140. }
  141. func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper, f *cmdutil.Factory, namespace, rsrc, prefix string, describerSettings *kubectl.DescriberSettings, out io.Writer, originalError error) error {
  142. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  143. NamespaceParam(namespace).DefaultNamespace().
  144. ResourceTypeOrNameArgs(true, rsrc).
  145. SingleResourceType().
  146. Flatten().
  147. Do()
  148. mapping, err := r.ResourceMapping()
  149. if err != nil {
  150. return err
  151. }
  152. describer, err := f.Describer(mapping)
  153. if err != nil {
  154. return err
  155. }
  156. infos, err := r.Infos()
  157. if err != nil {
  158. return err
  159. }
  160. isFound := false
  161. for ix := range infos {
  162. info := infos[ix]
  163. if strings.HasPrefix(info.Name, prefix) {
  164. isFound = true
  165. s, err := describer.Describe(info.Namespace, info.Name, *describerSettings)
  166. if err != nil {
  167. return err
  168. }
  169. fmt.Fprintf(out, "%s\n", s)
  170. }
  171. }
  172. if !isFound {
  173. return originalError
  174. }
  175. return nil
  176. }