From 300daa13a48044f8bb2b406c9289df2dae9ff0ed Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Mon, 4 Nov 2019 09:11:20 -0500 Subject: [PATCH] Set user agent for e2e consistently --- test/e2e/framework/framework.go | 8 -------- test/e2e/framework/util.go | 21 ++++++++++++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 5dfefdb39e4..2208653d193 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -160,14 +160,6 @@ func (f *Framework) BeforeEach() { ginkgo.By("Creating a kubernetes client") config, err := LoadConfig() ExpectNoError(err) - testDesc := ginkgo.CurrentGinkgoTestDescription() - if len(testDesc.ComponentTexts) > 0 { - componentTexts := strings.Join(testDesc.ComponentTexts, " ") - config.UserAgent = fmt.Sprintf( - "%v -- %v", - rest.DefaultKubernetesUserAgent(), - componentTexts) - } config.QPS = f.Options.ClientQPS config.Burst = f.Options.ClientBurst diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index 0319b12e9dc..178d3a89d76 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -64,6 +64,7 @@ import ( "k8s.io/client-go/dynamic" clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" restclient "k8s.io/client-go/rest" scaleclient "k8s.io/client-go/scale" "k8s.io/client-go/tools/clientcmd" @@ -881,8 +882,8 @@ func KubectlVersion() (*utilversion.Version, error) { return utilversion.ParseSemantic(matches[1]) } -// RestclientConfig returns a config holds the information needed to build connection to kubernetes clusters. -func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) { +// restclientConfig returns a config holds the information needed to build connection to kubernetes clusters. +func restclientConfig(kubeContext string) (*clientcmdapi.Config, error) { Logf(">>> kubeConfig: %s", TestContext.KubeConfig) if TestContext.KubeConfig == "" { return nil, fmt.Errorf("KubeConfig must be specified to load client config") @@ -901,13 +902,23 @@ func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) { // ClientConfigGetter is a func that returns getter to return a config. type ClientConfigGetter func() (*restclient.Config, error) -// LoadConfig returns a config for a rest client. -func LoadConfig() (*restclient.Config, error) { +// LoadConfig returns a config for a rest client with the UserAgent set to include the current test name. +func LoadConfig() (config *restclient.Config, err error) { + defer func() { + if err == nil && config != nil { + testDesc := ginkgo.CurrentGinkgoTestDescription() + if len(testDesc.ComponentTexts) > 0 { + componentTexts := strings.Join(testDesc.ComponentTexts, " ") + config.UserAgent = fmt.Sprintf("%s -- %s", rest.DefaultKubernetesUserAgent(), componentTexts) + } + } + }() + if TestContext.NodeE2E { // This is a node e2e test, apply the node e2e configuration return &restclient.Config{Host: TestContext.Host}, nil } - c, err := RestclientConfig(TestContext.KubeContext) + c, err := restclientConfig(TestContext.KubeContext) if err != nil { if TestContext.KubeConfig == "" { return restclient.InClusterConfig()