mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
kubectl: alias plaintext-openapiv2 to old explain
This commit is contained in:
parent
81dd9e3d25
commit
8249a827bd
@ -27,7 +27,7 @@ import (
|
|||||||
openapiclient "k8s.io/client-go/openapi"
|
openapiclient "k8s.io/client-go/openapi"
|
||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
"k8s.io/kubectl/pkg/explain"
|
"k8s.io/kubectl/pkg/explain"
|
||||||
explainv2 "k8s.io/kubectl/pkg/explain/v2"
|
openapiv3explain "k8s.io/kubectl/pkg/explain/v2"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"k8s.io/kubectl/pkg/util/openapi"
|
"k8s.io/kubectl/pkg/util/openapi"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
@ -51,6 +51,9 @@ var (
|
|||||||
|
|
||||||
# Get the documentation of a specific field of a resource
|
# Get the documentation of a specific field of a resource
|
||||||
kubectl explain pods.spec.containers`))
|
kubectl explain pods.spec.containers`))
|
||||||
|
|
||||||
|
plaintextTemplateName = "plaintext"
|
||||||
|
plaintextOpenAPIV2TemplateName = "plaintext-openapiv2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExplainOptions struct {
|
type ExplainOptions struct {
|
||||||
@ -82,7 +85,7 @@ func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *Expl
|
|||||||
IOStreams: streams,
|
IOStreams: streams,
|
||||||
CmdParent: parent,
|
CmdParent: parent,
|
||||||
EnableOpenAPIV3: cmdutil.ExplainOpenapiV3.IsEnabled(),
|
EnableOpenAPIV3: cmdutil.ExplainOpenapiV3.IsEnabled(),
|
||||||
OutputFormat: "plaintext",
|
OutputFormat: plaintextTemplateName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +110,7 @@ func NewCmdExplain(parent string, f cmdutil.Factory, streams genericclioptions.I
|
|||||||
|
|
||||||
// Only enable --output as a valid flag if the feature is enabled
|
// Only enable --output as a valid flag if the feature is enabled
|
||||||
if o.EnableOpenAPIV3 {
|
if o.EnableOpenAPIV3 {
|
||||||
cmd.Flags().StringVar(&o.OutputFormat, "output", o.OutputFormat, "Format in which to render the schema")
|
cmd.Flags().StringVar(&o.OutputFormat, "output", plaintextTemplateName, "Format in which to render the schema (plaintext, plaintext-openapiv2)")
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
@ -150,13 +153,10 @@ func (o *ExplainOptions) Validate() error {
|
|||||||
|
|
||||||
// Run executes the appropriate steps to print a model's documentation
|
// Run executes the appropriate steps to print a model's documentation
|
||||||
func (o *ExplainOptions) Run() error {
|
func (o *ExplainOptions) Run() error {
|
||||||
recursive := o.Recursive
|
|
||||||
apiVersionString := o.APIVersion
|
|
||||||
|
|
||||||
var fullySpecifiedGVR schema.GroupVersionResource
|
var fullySpecifiedGVR schema.GroupVersionResource
|
||||||
var fieldsPath []string
|
var fieldsPath []string
|
||||||
var err error
|
var err error
|
||||||
if len(apiVersionString) == 0 {
|
if len(o.APIVersion) == 0 {
|
||||||
fullySpecifiedGVR, fieldsPath, err = explain.SplitAndParseResourceRequestWithMatchingPrefix(o.args[0], o.Mapper)
|
fullySpecifiedGVR, fieldsPath, err = explain.SplitAndParseResourceRequestWithMatchingPrefix(o.args[0], o.Mapper)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -171,16 +171,47 @@ func (o *ExplainOptions) Run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback to openapiv2 implementation using special template name
|
||||||
if o.EnableOpenAPIV3 {
|
if o.EnableOpenAPIV3 {
|
||||||
return explainv2.PrintModelDescription(
|
switch o.OutputFormat {
|
||||||
fieldsPath,
|
case plaintextOpenAPIV2TemplateName:
|
||||||
o.Out,
|
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
|
||||||
o.OpenAPIV3Client,
|
case plaintextTemplateName:
|
||||||
fullySpecifiedGVR,
|
// Check whether the server reponds to OpenAPIV3.
|
||||||
recursive,
|
if _, err := o.OpenAPIV3Client.Paths(); err != nil {
|
||||||
o.OutputFormat,
|
// Use v2 renderer if server does not support v3
|
||||||
)
|
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
if len(o.APIVersion) > 0 {
|
||||||
|
apiVersion, err := schema.ParseGroupVersion(o.APIVersion)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fullySpecifiedGVR.Group = apiVersion.Group
|
||||||
|
fullySpecifiedGVR.Version = apiVersion.Version
|
||||||
|
}
|
||||||
|
|
||||||
|
return openapiv3explain.PrintModelDescription(
|
||||||
|
fieldsPath,
|
||||||
|
o.Out,
|
||||||
|
o.OpenAPIV3Client,
|
||||||
|
fullySpecifiedGVR,
|
||||||
|
o.Recursive,
|
||||||
|
o.OutputFormat,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ExplainOptions) renderOpenAPIV2(
|
||||||
|
fullySpecifiedGVR schema.GroupVersionResource,
|
||||||
|
fieldsPath []string,
|
||||||
|
) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
gvk, _ := o.Mapper.KindFor(fullySpecifiedGVR)
|
gvk, _ := o.Mapper.KindFor(fullySpecifiedGVR)
|
||||||
if gvk.Empty() {
|
if gvk.Empty() {
|
||||||
@ -190,8 +221,8 @@ func (o *ExplainOptions) Run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(apiVersionString) != 0 {
|
if len(o.APIVersion) != 0 {
|
||||||
apiVersion, err := schema.ParseGroupVersion(apiVersionString)
|
apiVersion, err := schema.ParseGroupVersion(o.APIVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -203,5 +234,5 @@ func (o *ExplainOptions) Run() error {
|
|||||||
return fmt.Errorf("couldn't find resource for %q", gvk)
|
return fmt.Errorf("couldn't find resource for %q", gvk)
|
||||||
}
|
}
|
||||||
|
|
||||||
return explain.PrintModelDescription(fieldsPath, o.Out, schema, gvk, recursive)
|
return explain.PrintModelDescription(fieldsPath, o.Out, schema, gvk, o.Recursive)
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ func printModelDescriptionWithGenerator(
|
|||||||
gv, exists := paths[resourcePath]
|
gv, exists := paths[resourcePath]
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("could not locate schema for %s", resourcePath)
|
return fmt.Errorf("couldn't find resource for \"%v\"", gvr)
|
||||||
}
|
}
|
||||||
|
|
||||||
openAPISchemaBytes, err := gv.Schema(runtime.ContentTypeJSON)
|
openAPISchemaBytes, err := gv.Schema(runtime.ContentTypeJSON)
|
||||||
|
@ -45,7 +45,7 @@ func TestExplainErrors(t *testing.T) {
|
|||||||
Version: "v1",
|
Version: "v1",
|
||||||
Resource: "doesntmatter",
|
Resource: "doesntmatter",
|
||||||
}, false, "unknown-format")
|
}, false, "unknown-format")
|
||||||
require.ErrorContains(t, err, "could not locate schema")
|
require.ErrorContains(t, err, "couldn't find resource for \"test0.example.com/v1, Resource=doesntmatter\"")
|
||||||
|
|
||||||
// Validate error when openapi client returns error.
|
// Validate error when openapi client returns error.
|
||||||
fakeClient.ForcedErr = fmt.Errorf("Always fails")
|
fakeClient.ForcedErr = fmt.Errorf("Always fails")
|
||||||
|
Loading…
Reference in New Issue
Block a user