explain.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "github.com/renstrom/dedent"
  18. "github.com/spf13/cobra"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/apimachinery/registered"
  21. "k8s.io/kubernetes/pkg/kubectl"
  22. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  23. )
  24. var (
  25. explainExamples = dedent.Dedent(`
  26. # Get the documentation of the resource and its fields
  27. kubectl explain pods
  28. # Get the documentation of a specific field of a resource
  29. kubectl explain pods.spec.containers`)
  30. explainLong = dedent.Dedent(`
  31. Documentation of resources.
  32. `) + valid_resources
  33. )
  34. // NewCmdExplain returns a cobra command for swagger docs
  35. func NewCmdExplain(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command {
  36. cmd := &cobra.Command{
  37. Use: "explain RESOURCE",
  38. Short: "Documentation of resources",
  39. Long: explainLong,
  40. Example: explainExamples,
  41. Run: func(cmd *cobra.Command, args []string) {
  42. err := RunExplain(f, out, cmdErr, cmd, args)
  43. cmdutil.CheckErr(err)
  44. },
  45. }
  46. cmd.Flags().Bool("recursive", false, "Print the fields of fields (Currently only 1 level deep)")
  47. cmdutil.AddInclude3rdPartyFlags(cmd)
  48. return cmd
  49. }
  50. // RunExplain executes the appropriate steps to print a model's documentation
  51. func RunExplain(f *cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string) error {
  52. if len(args) == 0 {
  53. fmt.Fprint(cmdErr, "You must specify the type of resource to explain. ", valid_resources)
  54. return cmdutil.UsageError(cmd, "Required resource not specified.")
  55. }
  56. if len(args) > 1 {
  57. return cmdutil.UsageError(cmd, "We accept only this format: explain RESOURCE")
  58. }
  59. recursive := cmdutil.GetFlagBool(cmd, "recursive")
  60. apiVersionString := cmdutil.GetFlagString(cmd, "api-version")
  61. apiVersion := unversioned.GroupVersion{}
  62. mapper, _ := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  63. // TODO: After we figured out the new syntax to separate group and resource, allow
  64. // the users to use it in explain (kubectl explain <group><syntax><resource>).
  65. // Refer to issue #16039 for why we do this. Refer to PR #15808 that used "/" syntax.
  66. inModel, fieldsPath, err := kubectl.SplitAndParseResourceRequest(args[0], mapper)
  67. if err != nil {
  68. return err
  69. }
  70. // TODO: We should deduce the group for a resource by discovering the supported resources at server.
  71. fullySpecifiedGVR, groupResource := unversioned.ParseResourceArg(inModel)
  72. gvk := unversioned.GroupVersionKind{}
  73. if fullySpecifiedGVR != nil {
  74. gvk, _ = mapper.KindFor(*fullySpecifiedGVR)
  75. }
  76. if gvk.Empty() {
  77. gvk, err = mapper.KindFor(groupResource.WithVersion(""))
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. if len(apiVersionString) == 0 {
  83. groupMeta, err := registered.Group(gvk.Group)
  84. if err != nil {
  85. return err
  86. }
  87. apiVersion = groupMeta.GroupVersion
  88. } else {
  89. apiVersion, err = unversioned.ParseGroupVersion(apiVersionString)
  90. if err != nil {
  91. return nil
  92. }
  93. }
  94. schema, err := f.SwaggerSchema(apiVersion.WithKind(gvk.Kind))
  95. if err != nil {
  96. return err
  97. }
  98. return kubectl.PrintModelDescription(inModel, fieldsPath, out, schema, recursive)
  99. }