mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
Merge pull request #134777 from soltysh/headers_stable
Promote KUBECTL_COMMAND_HEADERS to stable
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
//
|
||||
|
||||
@@ -53,11 +53,6 @@ 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user