Promote KUBECTL_COMMAND_HEADERS to stable

Signed-off-by: Maciej Szulik <soltysh@gmail.com>

Kubernetes-commit: f82a139dc0840b51aa41931535f5eef6fc3507b4
This commit is contained in:
Maciej Szulik 2025-10-22 13:56:32 +02:00 committed by Kubernetes Publisher
parent d1b399db57
commit 164e47abb8
3 changed files with 0 additions and 58 deletions

View file

@ -526,17 +526,10 @@ func NewKubectlCommand(o KubectlOptions) *cobra.Command {
// RoundTripper. CommandHeaderRoundTripper adds X-Headers then delegates
// to standard RoundTripper.
//
// For beta, these hooks are updated unless the KUBECTL_COMMAND_HEADERS environment variable
// is set, and the value of the env var is false (or zero).
// See SIG CLI KEP 859 for more information:
//
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/859-kubectl-headers
func addCmdHeaderHooks(cmds *cobra.Command, kubeConfigFlags *genericclioptions.ConfigFlags) {
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{}
existingPreRunE := cmds.PersistentPreRunE
// Add command parsing to the existing persistent pre-run function.

View file

@ -26,10 +26,8 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/cobra"
"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) {
@ -360,46 +358,3 @@ func (h *testPluginHandler) Execute(executablePath string, cmdArgs, env []string
h.withEnv = env
return nil
}
func TestKubectlCommandHeadersHooks(t *testing.T) {
tests := map[string]struct {
envVar string
addsHooks bool
}{
"empty environment variable; hooks added": {
envVar: "",
addsHooks: true,
},
"random env var value; hooks added": {
envVar: "foo",
addsHooks: true,
},
"true env var value; hooks added": {
envVar: "true",
addsHooks: true,
},
"false env var value; hooks NOT added": {
envVar: "false",
addsHooks: false,
},
}
for name, testCase := range tests {
t.Run(name, func(t *testing.T) {
cmds := &cobra.Command{}
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
if kubeConfigFlags.WrapConfigFn != nil {
t.Fatal("expected initial nil WrapConfigFn")
}
t.Setenv(string(cmdutil.CmdHeaders), testCase.envVar)
addCmdHeaderHooks(cmds, kubeConfigFlags)
// Valdidate whether the hooks were added.
if testCase.addsHooks && kubeConfigFlags.WrapConfigFn == nil {
t.Error("after adding kubectl command header, expecting non-nil WrapConfigFn")
}
if !testCase.addsHooks && kubeConfigFlags.WrapConfigFn != nil {
t.Error("env var feature gate should have blocked setting WrapConfigFn")
}
})
}
}

View file

@ -425,12 +425,6 @@ func GetPodRunningTimeoutFlag(cmd *cobra.Command) (time.Duration, error) {
type FeatureGate string
const (
// owner: @soltysh
// kep: https://kep.k8s.io/859
//
// HTTP headers with command name and flags used.
CmdHeaders FeatureGate = "KUBECTL_COMMAND_HEADERS"
// owner: @ardaguclu
// kep: https://kep.k8s.io/3104
//