2015-08-13 01:33:25 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-08-13 01:33:25 +00:00
|
|
|
|
|
|
|
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 (
|
2016-08-23 01:40:57 +00:00
|
|
|
"fmt"
|
2015-08-13 01:33:25 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2015-12-01 16:52:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-01-13 22:40:56 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
2015-08-13 01:33:25 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2016-10-07 22:24:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
2015-08-13 01:33:25 +00:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
)
|
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
var (
|
2016-10-07 22:24:42 +00:00
|
|
|
explainLong = templates.LongDesc(`
|
|
|
|
Documentation of resources.
|
|
|
|
|
|
|
|
` + valid_resources)
|
|
|
|
|
|
|
|
explainExamples = templates.Examples(`
|
2016-05-20 17:49:56 +00:00
|
|
|
# Get the documentation of the resource and its fields
|
|
|
|
kubectl explain pods
|
2015-08-13 01:33:25 +00:00
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
# Get the documentation of a specific field of a resource
|
|
|
|
kubectl explain pods.spec.containers`)
|
2015-08-13 01:33:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewCmdExplain returns a cobra command for swagger docs
|
2016-10-13 00:18:39 +00:00
|
|
|
func NewCmdExplain(f cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command {
|
2015-08-13 01:33:25 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "explain RESOURCE",
|
2016-06-07 17:38:04 +00:00
|
|
|
Short: "Documentation of resources",
|
2015-08-13 01:33:25 +00:00
|
|
|
Long: explainLong,
|
|
|
|
Example: explainExamples,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-08-23 01:40:57 +00:00
|
|
|
err := RunExplain(f, out, cmdErr, cmd, args)
|
2015-08-13 01:33:25 +00:00
|
|
|
cmdutil.CheckErr(err)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().Bool("recursive", false, "Print the fields of fields (Currently only 1 level deep)")
|
2016-03-10 01:27:19 +00:00
|
|
|
cmdutil.AddInclude3rdPartyFlags(cmd)
|
2015-08-13 01:33:25 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunExplain executes the appropriate steps to print a model's documentation
|
2016-10-13 00:18:39 +00:00
|
|
|
func RunExplain(f cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string) error {
|
2016-08-23 01:40:57 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Fprint(cmdErr, "You must specify the type of resource to explain. ", valid_resources)
|
|
|
|
return cmdutil.UsageError(cmd, "Required resource not specified.")
|
|
|
|
}
|
|
|
|
if len(args) > 1 {
|
2015-08-13 01:33:25 +00:00
|
|
|
return cmdutil.UsageError(cmd, "We accept only this format: explain RESOURCE")
|
|
|
|
}
|
|
|
|
|
|
|
|
recursive := cmdutil.GetFlagBool(cmd, "recursive")
|
2015-12-01 16:52:11 +00:00
|
|
|
apiVersionString := cmdutil.GetFlagString(cmd, "api-version")
|
|
|
|
apiVersion := unversioned.GroupVersion{}
|
2015-08-13 01:33:25 +00:00
|
|
|
|
2016-09-16 19:50:34 +00:00
|
|
|
mapper, _ := f.Object()
|
2015-10-21 20:49:22 +00:00
|
|
|
// TODO: After we figured out the new syntax to separate group and resource, allow
|
|
|
|
// the users to use it in explain (kubectl explain <group><syntax><resource>).
|
|
|
|
// Refer to issue #16039 for why we do this. Refer to PR #15808 that used "/" syntax.
|
|
|
|
inModel, fieldsPath, err := kubectl.SplitAndParseResourceRequest(args[0], mapper)
|
2015-08-13 01:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-21 20:49:22 +00:00
|
|
|
// TODO: We should deduce the group for a resource by discovering the supported resources at server.
|
2016-03-11 15:21:50 +00:00
|
|
|
fullySpecifiedGVR, groupResource := unversioned.ParseResourceArg(inModel)
|
|
|
|
gvk := unversioned.GroupVersionKind{}
|
|
|
|
if fullySpecifiedGVR != nil {
|
|
|
|
gvk, _ = mapper.KindFor(*fullySpecifiedGVR)
|
|
|
|
}
|
2016-08-17 21:09:51 +00:00
|
|
|
if gvk.Empty() {
|
2016-03-11 15:21:50 +00:00
|
|
|
gvk, err = mapper.KindFor(groupResource.WithVersion(""))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-17 00:18:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 16:52:11 +00:00
|
|
|
if len(apiVersionString) == 0 {
|
2016-01-13 22:40:56 +00:00
|
|
|
groupMeta, err := registered.Group(gvk.Group)
|
2015-10-17 00:18:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-01 16:52:11 +00:00
|
|
|
apiVersion = groupMeta.GroupVersion
|
|
|
|
|
|
|
|
} else {
|
|
|
|
apiVersion, err = unversioned.ParseGroupVersion(apiVersionString)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-17 00:18:54 +00:00
|
|
|
}
|
2015-12-01 16:52:11 +00:00
|
|
|
|
2016-02-07 02:19:42 +00:00
|
|
|
schema, err := f.SwaggerSchema(apiVersion.WithKind(gvk.Kind))
|
2015-08-13 01:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-11 22:15:41 +00:00
|
|
|
return kubectl.PrintModelDescription(inModel, fieldsPath, out, schema, recursive)
|
2015-08-13 01:33:25 +00:00
|
|
|
}
|