enable openapiv3 by default

This commit is contained in:
Alexander Zielenski 2023-03-08 13:15:56 -08:00
parent de9ce03f19
commit ce3385ab1f
2 changed files with 35 additions and 66 deletions

View File

@ -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
@ -84,7 +80,6 @@ func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *Expl
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,13 +121,11 @@ 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
return nil return nil
@ -172,7 +163,6 @@ 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) return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
@ -204,8 +194,6 @@ func (o *ExplainOptions) Run() error {
) )
} }
} }
return o.renderOpenAPIV2(fullySpecifiedGVR, fieldsPath)
}
func (o *ExplainOptions) renderOpenAPIV2( func (o *ExplainOptions) renderOpenAPIV2(
fullySpecifiedGVR schema.GroupVersionResource, fullySpecifiedGVR schema.GroupVersionResource,

View File

@ -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)
})
}
}