diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 42a2c219..bba80472 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -83,8 +83,6 @@ import ( "k8s.io/kubectl/pkg/cmd/kustomize" ) -const kubectlCmdHeaders = "KUBECTL_COMMAND_HEADERS" - type KubectlOptions struct { PluginHandler PluginHandler Arguments []string @@ -522,12 +520,9 @@ func NewKubectlCommand(o KubectlOptions) *cobra.Command { // // https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/859-kubectl-headers func addCmdHeaderHooks(cmds *cobra.Command, kubeConfigFlags *genericclioptions.ConfigFlags) { - // If the feature gate env var is set to "false", then do no add kubectl command headers. - if value, exists := os.LookupEnv(kubectlCmdHeaders); exists { - if value == "false" || value == "0" { - klog.V(5).Infoln("kubectl command headers turned off") - return - } + if cmdutil.CmdHeaders.IsDisabled() { + klog.V(5).Infoln("kubectl command headers turned off") + return } klog.V(5).Infoln("kubectl command headers turned on") crt := &genericclioptions.CommandHeaderRoundTripper{} diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go index 633bc242..f624d5f8 100644 --- a/pkg/cmd/cmd_test.go +++ b/pkg/cmd/cmd_test.go @@ -29,6 +29,7 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" "k8s.io/kubectl/pkg/cmd/plugin" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func TestNormalizationFuncGlobalExistence(t *testing.T) { @@ -381,10 +382,6 @@ func TestKubectlCommandHeadersHooks(t *testing.T) { envVar: "false", addsHooks: false, }, - "zero env var value; hooks NOT added": { - envVar: "0", - addsHooks: false, - }, } for name, testCase := range tests { @@ -394,7 +391,7 @@ func TestKubectlCommandHeadersHooks(t *testing.T) { if kubeConfigFlags.WrapConfigFn != nil { t.Fatal("expected initial nil WrapConfigFn") } - t.Setenv(kubectlCmdHeaders, testCase.envVar) + t.Setenv(string(cmdutil.CmdHeaders), testCase.envVar) addCmdHeaderHooks(cmds, kubeConfigFlags) // Valdidate whether the hooks were added. if testCase.addsHooks && kubeConfigFlags.WrapConfigFn == nil { diff --git a/pkg/cmd/util/helpers.go b/pkg/cmd/util/helpers.go index 083018c6..e978841c 100644 --- a/pkg/cmd/util/helpers.go +++ b/pkg/cmd/util/helpers.go @@ -427,6 +427,7 @@ type FeatureGate string const ( ApplySet FeatureGate = "KUBECTL_APPLYSET" OpenAPIV3Patch FeatureGate = "KUBECTL_OPENAPIV3_PATCH" + CmdHeaders FeatureGate = "KUBECTL_COMMAND_HEADERS" RemoteCommandWebsockets FeatureGate = "KUBECTL_REMOTE_COMMAND_WEBSOCKETS" PortForwardWebsockets FeatureGate = "KUBECTL_PORT_FORWARD_WEBSOCKETS" KubeRC FeatureGate = "KUBECTL_KUBERC"