diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain.go b/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain.go index 827d0b74b7e..172a883b0ee 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain.go @@ -68,10 +68,6 @@ type ExplainOptions struct { Mapper meta.RESTMapper 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 // `EnableOpenAPIV3` is disabled, this does nothing OutputFormat string @@ -82,10 +78,9 @@ type ExplainOptions struct { func NewExplainOptions(parent string, streams genericclioptions.IOStreams) *ExplainOptions { return &ExplainOptions{ - IOStreams: streams, - CmdParent: parent, - EnableOpenAPIV3: cmdutil.ExplainOpenapiV3.IsEnabled(), - OutputFormat: plaintextTemplateName, + IOStreams: streams, + CmdParent: parent, + 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)") // 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 } @@ -128,12 +121,10 @@ func (o *ExplainOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [] return err } - // Only openapi v3 needs the openapiv3client - if o.EnableOpenAPIV3 { - o.OpenAPIV3Client, err = f.OpenAPIV3Client() - if err != nil { - return err - } + // Only openapi v3 needs the discovery client. + o.OpenAPIV3Client, err = f.OpenAPIV3Client() + if err != nil { + return err } o.args = args @@ -172,39 +163,36 @@ func (o *ExplainOptions) Run() error { } // Fallback to openapiv2 implementation using special template name - if o.EnableOpenAPIV3 { - switch o.OutputFormat { - case plaintextOpenAPIV2TemplateName: + switch o.OutputFormat { + 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) - 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( diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain_test.go index 045d4bac8e7..d5347ddeb70 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/explain/explain_test.go @@ -22,7 +22,6 @@ import ( "regexp" "testing" - "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/api/meta" sptest "k8s.io/apimachinery/pkg/util/strategicpatch/testing" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -278,21 +277,3 @@ func runExplainTestCases(t *testing.T, cases []explainTestCase) { 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) - }) - } -}