From 3fd4af9209d73a99186dc030a2175fcd0115bc3f Mon Sep 17 00:00:00 2001 From: gardlt Date: Sat, 24 Jun 2017 20:01:59 -0500 Subject: [PATCH] feat(helm): adding kubeconfig flag --- cmd/helm/helm.go | 4 +++- cmd/helm/install.go | 2 +- pkg/helm/environment/environment.go | 4 ++++ pkg/kube/config.go | 7 ++++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 7e08921ba..9fe417774 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -95,6 +95,7 @@ func addRootFlags(cmd *cobra.Command) { pf.StringVar((*string)(&settings.Home), "home", helm_env.DefaultHelmHome, "location of your Helm config. Overrides $HELM_HOME") pf.StringVar(&settings.TillerHost, "host", "", "address of tiller. Overrides $HELM_HOST") pf.StringVar(&kubeContext, "kube-context", "", "name of the kubeconfig context to use") + pf.StringVar(&settings.Kubeconfig, "kubeconfig", "", "path to kubeconfig file") pf.BoolVar(&settings.Debug, "debug", false, "enable verbose output") pf.StringVar(&settings.TillerNamespace, "tiller-namespace", tiller_env.DefaultTillerNamespace, "namespace of tiller") } @@ -102,6 +103,7 @@ func addRootFlags(cmd *cobra.Command) { func initRootFlags(cmd *cobra.Command) { setFlagsFromEnv(map[string]string{ "debug": helm_env.DebugEnvVar, + "kubeconfig": helm_env.KubeconfigEnvVar, "home": helm_env.HomeEnvVar, "host": helm_env.HostEnvVar, "tiller-namespace": tiller_env.TillerNamespaceEnvVar, @@ -244,7 +246,7 @@ func prettyError(err error) error { // configForContext creates a Kubernetes REST client configuration for a given kubeconfig context. func configForContext(context string) (*rest.Config, error) { - config, err := kube.GetConfig(context).ClientConfig() + config, err := kube.GetConfig(context, settings.Kubeconfig).ClientConfig() if err != nil { return nil, fmt.Errorf("could not get Kubernetes config for context %q: %s", context, err) } diff --git a/cmd/helm/install.go b/cmd/helm/install.go index c6decc9cb..7e96c9695 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -432,7 +432,7 @@ func generateName(nameTemplate string) (string, error) { } func defaultNamespace() string { - if ns, _, err := kube.GetConfig(kubeContext).Namespace(); err == nil { + if ns, _, err := kube.GetConfig(kubeContext, settings.Kubeconfig).Namespace(); err == nil { return ns } return "default" diff --git a/pkg/helm/environment/environment.go b/pkg/helm/environment/environment.go index 2092450e1..1ad83e32d 100644 --- a/pkg/helm/environment/environment.go +++ b/pkg/helm/environment/environment.go @@ -40,6 +40,8 @@ const ( HostEnvVar = "HELM_HOST" // DebugEnvVar is the HELM_DEBUG environment variable key. DebugEnvVar = "HELM_DEBUG" + // KubeconfigVar is the HELM_KUBECONFIG enviroment variable key. + KubeconfigEnvVar = "HELM_KUBECONFIG" ) // DefaultHelmHome is the default HELM_HOME. @@ -55,6 +57,8 @@ type EnvSettings struct { Home helmpath.Home // Debug indicates whether or not Helm is running in Debug mode. Debug bool + // Kubeconfig is the local paht to the kubernetes config directory + Kubeconfig string } // PluginDirs is the path to the plugin directories. diff --git a/pkg/kube/config.go b/pkg/kube/config.go index 27b22b446..841c95784 100644 --- a/pkg/kube/config.go +++ b/pkg/kube/config.go @@ -19,7 +19,7 @@ package kube // import "k8s.io/helm/pkg/kube" import "k8s.io/client-go/tools/clientcmd" // GetConfig returns a kubernetes client config for a given context. -func GetConfig(context string) clientcmd.ClientConfig { +func GetConfig(context string, kubeconfig string) clientcmd.ClientConfig { rules := clientcmd.NewDefaultClientConfigLoadingRules() rules.DefaultClientConfig = &clientcmd.DefaultClientConfig @@ -28,5 +28,10 @@ func GetConfig(context string) clientcmd.ClientConfig { if context != "" { overrides.CurrentContext = context } + + if kubeconfig != "" { + rules.ExplicitPath = kubeconfig + } + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides) }