explain.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 explain
  14. import (
  15. "fmt"
  16. "github.com/spf13/cobra"
  17. "k8s.io/apimachinery/pkg/api/meta"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/cli-runtime/pkg/genericclioptions"
  20. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
  22. "k8s.io/kubernetes/pkg/kubectl/explain"
  23. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  24. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  25. )
  26. var (
  27. explainLong = templates.LongDesc(`
  28. List the fields for supported resources
  29. This command describes the fields associated with each supported API resource.
  30. Fields are identified via a simple JSONPath identifier:
  31. <type>.<fieldName>[.<fieldName>]
  32. Add the --recursive flag to display all of the fields at once without descriptions.
  33. Information about each field is retrieved from the server in OpenAPI format.`)
  34. explainExamples = templates.Examples(i18n.T(`
  35. # Get the documentation of the resource and its fields
  36. kubectl explain pods
  37. # Get the documentation of a specific field of a resource
  38. kubectl explain pods.spec.containers`))
  39. )
  40. type ExplainOptions struct {
  41. genericclioptions.IOStreams
  42. CmdParent string
  43. APIVersion string
  44. Recursive bool
  45. Mapper meta.RESTMapper
  46. Schema openapi.Resources
  47. }
  48. func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *ExplainOptions {
  49. return &ExplainOptions{
  50. IOStreams: streams,
  51. CmdParent: parent,
  52. }
  53. }
  54. // NewCmdExplain returns a cobra command for swagger docs
  55. func NewCmdExplain(parent string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
  56. o := NewExplainOptions(parent, streams)
  57. cmd := &cobra.Command{
  58. Use: "explain RESOURCE",
  59. DisableFlagsInUseLine: true,
  60. Short: i18n.T("Documentation of resources"),
  61. Long: explainLong + "\n\n" + cmdutil.SuggestAPIResources(parent),
  62. Example: explainExamples,
  63. Run: func(cmd *cobra.Command, args []string) {
  64. cmdutil.CheckErr(o.Complete(f, cmd))
  65. cmdutil.CheckErr(o.Validate(args))
  66. cmdutil.CheckErr(o.Run(args))
  67. },
  68. }
  69. cmd.Flags().BoolVar(&o.Recursive, "recursive", o.Recursive, "Print the fields of fields (Currently only 1 level deep)")
  70. cmd.Flags().StringVar(&o.APIVersion, "api-version", o.APIVersion, "Get different explanations for particular API version")
  71. return cmd
  72. }
  73. func (o *ExplainOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
  74. var err error
  75. o.Mapper, err = f.ToRESTMapper()
  76. if err != nil {
  77. return err
  78. }
  79. o.Schema, err = f.OpenAPISchema()
  80. if err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. func (o *ExplainOptions) Validate(args []string) error {
  86. if len(args) == 0 {
  87. return fmt.Errorf("You must specify the type of resource to explain. %s\n", cmdutil.SuggestAPIResources(o.CmdParent))
  88. }
  89. if len(args) > 1 {
  90. return fmt.Errorf("We accept only this format: explain RESOURCE\n")
  91. }
  92. return nil
  93. }
  94. // Run executes the appropriate steps to print a model's documentation
  95. func (o *ExplainOptions) Run(args []string) error {
  96. recursive := o.Recursive
  97. apiVersionString := o.APIVersion
  98. // TODO: After we figured out the new syntax to separate group and resource, allow
  99. // the users to use it in explain (kubectl explain <group><syntax><resource>).
  100. // Refer to issue #16039 for why we do this. Refer to PR #15808 that used "/" syntax.
  101. inModel, fieldsPath, err := explain.SplitAndParseResourceRequest(args[0], o.Mapper)
  102. if err != nil {
  103. return err
  104. }
  105. // TODO: We should deduce the group for a resource by discovering the supported resources at server.
  106. fullySpecifiedGVR, groupResource := schema.ParseResourceArg(inModel)
  107. gvk := schema.GroupVersionKind{}
  108. if fullySpecifiedGVR != nil {
  109. gvk, _ = o.Mapper.KindFor(*fullySpecifiedGVR)
  110. }
  111. if gvk.Empty() {
  112. gvk, err = o.Mapper.KindFor(groupResource.WithVersion(""))
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. if len(apiVersionString) != 0 {
  118. apiVersion, err := schema.ParseGroupVersion(apiVersionString)
  119. if err != nil {
  120. return err
  121. }
  122. gvk = apiVersion.WithKind(gvk.Kind)
  123. }
  124. schema := o.Schema.LookupResource(gvk)
  125. if schema == nil {
  126. return fmt.Errorf("Couldn't find resource for %q", gvk)
  127. }
  128. return explain.PrintModelDescription(fieldsPath, o.Out, schema, gvk, recursive)
  129. }