mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
enable openapiv3 by default
This commit is contained in:
parent
de9ce03f19
commit
ce3385ab1f
@ -68,10 +68,6 @@ type ExplainOptions struct {
|
|||||||
Mapper meta.RESTMapper
|
Mapper meta.RESTMapper
|
||||||
Schema openapi.Resources
|
Schema openapi.Resources
|
||||||
|
|
||||||
// Toggles whether the OpenAPI v3 template-based renderer should be used to show
|
|
||||||
// output.
|
|
||||||
EnableOpenAPIV3 bool
|
|
||||||
|
|
||||||
// Name of the template to use with the openapiv3 template renderer. If
|
// Name of the template to use with the openapiv3 template renderer. If
|
||||||
// `EnableOpenAPIV3` is disabled, this does nothing
|
// `EnableOpenAPIV3` is disabled, this does nothing
|
||||||
OutputFormat string
|
OutputFormat string
|
||||||
@ -82,10 +78,9 @@ type ExplainOptions struct {
|
|||||||
|
|
||||||
func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *ExplainOptions {
|
func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *ExplainOptions {
|
||||||
return &ExplainOptions{
|
return &ExplainOptions{
|
||||||
IOStreams: streams,
|
IOStreams: streams,
|
||||||
CmdParent: parent,
|
CmdParent: parent,
|
||||||
EnableOpenAPIV3: cmdutil.ExplainOpenapiV3.IsEnabled(),
|
OutputFormat: plaintextTemplateName,
|
||||||
OutputFormat: plaintextTemplateName,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,9 +104,7 @@ func NewCmdExplain(parent string, f cmdutil.Factory, streams genericclioptions.I
|
|||||||
cmd.Flags().StringVar(&o.APIVersion, "api-version", o.APIVersion, "Get different explanations for particular API version (API group/version)")
|
cmd.Flags().StringVar(&o.APIVersion, "api-version", o.APIVersion, "Get different explanations for particular API version (API group/version)")
|
||||||
|
|
||||||
// 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 {
|
cmd.Flags().StringVar(&o.OutputFormat, "output", plaintextTemplateName, "Format in which to render the schema (plaintext, plaintext-openapiv2)")
|
||||||
cmd.Flags().StringVar(&o.OutputFormat, "output", plaintextTemplateName, "Format in which to render the schema (plaintext, plaintext-openapiv2)")
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@ -128,12 +121,10 @@ func (o *ExplainOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only openapi v3 needs the openapiv3client
|
// Only openapi v3 needs the discovery client.
|
||||||
if o.EnableOpenAPIV3 {
|
o.OpenAPIV3Client, err = f.OpenAPIV3Client()
|
||||||
o.OpenAPIV3Client, err = f.OpenAPIV3Client()
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
o.args = args
|
o.args = args
|
||||||
@ -172,39 +163,36 @@ func (o *ExplainOptions) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to openapiv2 implementation using special template name
|
// Fallback to openapiv2 implementation using special template name
|
||||||
if o.EnableOpenAPIV3 {
|
switch o.OutputFormat {
|
||||||
switch o.OutputFormat {
|
case plaintextOpenAPIV2TemplateName:
|
||||||
case plaintextOpenAPIV2TemplateName:
|
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
|
||||||
|
case plaintextTemplateName:
|
||||||
|
// Check whether the server reponds to OpenAPIV3.
|
||||||
|
if _, err := o.OpenAPIV3Client.Paths(); err != nil {
|
||||||
|
// Use v2 renderer if server does not support v3
|
||||||
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
|
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
|
||||||
case plaintextTemplateName:
|
|
||||||
// Check whether the server reponds to OpenAPIV3.
|
|
||||||
if _, err := o.OpenAPIV3Client.Paths(); err != nil {
|
|
||||||
// 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,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(
|
func (o *ExplainOptions) renderOpenAPIV2(
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
sptest "k8s.io/apimachinery/pkg/util/strategicpatch/testing"
|
sptest "k8s.io/apimachinery/pkg/util/strategicpatch/testing"
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
@ -278,21 +277,3 @@ func runExplainTestCases(t *testing.T, cases []explainTestCase) {
|
|||||||
buf.Reset()
|
buf.Reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlphaEnablement(t *testing.T) {
|
|
||||||
alphas := map[cmdutil.FeatureGate]string{
|
|
||||||
cmdutil.ExplainOpenapiV3: "output",
|
|
||||||
}
|
|
||||||
for feature, flag := range alphas {
|
|
||||||
f := cmdtesting.NewTestFactory()
|
|
||||||
defer f.Cleanup()
|
|
||||||
|
|
||||||
cmd := explain.NewCmdExplain("kubectl", f, genericclioptions.NewTestIOStreamsDiscard())
|
|
||||||
require.Nil(t, cmd.Flags().Lookup(flag), "flag %q should not be registered without the %q feature enabled", flag, feature)
|
|
||||||
|
|
||||||
cmdtesting.WithAlphaEnvs([]cmdutil.FeatureGate{feature}, t, func(t *testing.T) {
|
|
||||||
cmd := explain.NewCmdExplain("kubectl", f, genericclioptions.NewTestIOStreamsDiscard())
|
|
||||||
require.NotNil(t, cmd.Flags().Lookup(flag), "flag %q should be registered with the %q feature enabled", flag, feature)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user