From e3f3da5e795960508089aed08fe7fd9bec0a6db2 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Thu, 8 May 2025 14:09:17 +0200 Subject: [PATCH] Swap KUBECTL_COMMAND_HEADERS to use the proper feature gate mechanism Signed-off-by: Maciej Szulik --- staging/src/k8s.io/kubectl/pkg/cmd/cmd.go | 11 +++-------- staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go | 7 ++----- staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go | 1 + test/cmd/results.sh | 5 +++++ 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go b/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go index 42a2c21923d..bba80472166 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go +++ b/staging/src/k8s.io/kubectl/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/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go index 633bc242eb5..f624d5f8fc4 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go +++ b/staging/src/k8s.io/kubectl/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/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go index 083018c6f06..e978841cda6 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers.go +++ b/staging/src/k8s.io/kubectl/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" diff --git a/test/cmd/results.sh b/test/cmd/results.sh index a34253f20c2..498d058138f 100644 --- a/test/cmd/results.sh +++ b/test/cmd/results.sh @@ -53,6 +53,11 @@ Error from server (NotFound): pods "no-such-pod" not found EOF kube::test::results::diff "${TEMP}/actual_stdout" "${TEMP}/actual_stderr" "$res" "${TEMP}/empty" "${TEMP}/expected_stderr" 1 "kubectl get pod/no-such-pod" + output_message=$(kubectl get namespace kube-system 2>&1 "${kube_flags[@]:?}") + kube::test::if_has_not_string "${output_message}" "command headers turned on" + output_message=$(kubectl get namespace kube-system 2>&1 "${kube_flags[@]:?}" -v=5) + kube::test::if_has_string "${output_message}" "command headers turned on" + set +o nounset set +o errexit }