From 430474db74d7991629dba99d4b21a9c90155195a Mon Sep 17 00:00:00 2001 From: Dong Gang Date: Thu, 5 Mar 2020 09:51:31 +0800 Subject: [PATCH] fix(helm): report User-Agent to kubernetes calls Close #7719 Signed-off-by: Dong Gang --- pkg/kube/config.go | 4 ++-- pkg/kube/config_flags.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 pkg/kube/config_flags.go diff --git a/pkg/kube/config.go b/pkg/kube/config.go index 624c4a1f7..cf9e7fd46 100644 --- a/pkg/kube/config.go +++ b/pkg/kube/config.go @@ -19,8 +19,8 @@ package kube // import "helm.sh/helm/v3/pkg/kube" import "k8s.io/cli-runtime/pkg/genericclioptions" // GetConfig returns a Kubernetes client config. -func GetConfig(kubeconfig, context, namespace string) *genericclioptions.ConfigFlags { - cf := genericclioptions.NewConfigFlags(true) +func GetConfig(kubeconfig, context, namespace string) *ConfigFlags { + cf := &ConfigFlags{*genericclioptions.NewConfigFlags(true)} cf.Namespace = &namespace cf.Context = &context cf.KubeConfig = &kubeconfig diff --git a/pkg/kube/config_flags.go b/pkg/kube/config_flags.go new file mode 100644 index 000000000..541bbef21 --- /dev/null +++ b/pkg/kube/config_flags.go @@ -0,0 +1,40 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kube + +import ( + "helm.sh/helm/v3/internal/version" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/client-go/rest" +) + +type ConfigFlags struct { + genericclioptions.ConfigFlags +} + +// ToRESTConfig implements RESTClientGetter. +// Returns a REST client configuration based on a provided path +// to a .kubeconfig file, loading rules, and config flag overrides. +// Expects the AddFlags method to have been called. +func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error) { + config, err := f.ToRawKubeConfigLoader().ClientConfig() + if err != nil { + return nil, err + } + config.UserAgent = version.GetUserAgent() + return config, err +}