From a2e577814d440ddd4d680bdf4633ad423b77fe47 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Tue, 11 Oct 2016 11:33:47 -0700 Subject: [PATCH] feat(helm): add kubeconfig context switching closes: #1127 --- cmd/helm/helm.go | 8 +++++--- cmd/helm/tunnel.go | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 540eb3871..678314441 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -35,8 +35,9 @@ const ( ) var ( - helmHome string - tillerHost string + helmHome string + tillerHost string + kubeContext string ) // flagDebug is a signal that the user wants additional output. @@ -82,6 +83,7 @@ func newRootCmd(out io.Writer) *cobra.Command { p := cmd.PersistentFlags() p.StringVar(&helmHome, "home", home, "location of your Helm config. Overrides $HELM_HOME") p.StringVar(&tillerHost, "host", thost, "address of tiller. Overrides $HELM_HOST") + p.StringVar(&kubeContext, "kube-context", "", "name of the kubeconfig context to use") p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output") rup := newRepoUpdateCmd(out) @@ -124,7 +126,7 @@ func main() { func setupConnection(c *cobra.Command, args []string) error { if tillerHost == "" { - tunnel, err := newTillerPortForwarder(tillerNamespace) + tunnel, err := newTillerPortForwarder(tillerNamespace, kubeContext) if err != nil { return err } diff --git a/cmd/helm/tunnel.go b/cmd/helm/tunnel.go index f5be0cfff..b6b7fbebd 100644 --- a/cmd/helm/tunnel.go +++ b/cmd/helm/tunnel.go @@ -21,6 +21,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/client/unversioned" + "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" "k8s.io/kubernetes/pkg/labels" "k8s.io/helm/pkg/kube" @@ -29,8 +30,17 @@ import ( // TODO refactor out this global var var tunnel *kube.Tunnel -func newTillerPortForwarder(namespace string) (*kube.Tunnel, error) { - kc := kube.New(nil) +func getKubeConfig(context string) clientcmd.ClientConfig { + rules := clientcmd.NewDefaultClientConfigLoadingRules() + overrides := &clientcmd.ConfigOverrides{} + if context != "" { + overrides.CurrentContext = context + } + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides) +} + +func newTillerPortForwarder(namespace, context string) (*kube.Tunnel, error) { + kc := kube.New(getKubeConfig(context)) client, err := kc.Client() if err != nil { return nil, err