From bad489cb75892bbb115e1146a3ea502770759ad0 Mon Sep 17 00:00:00 2001 From: chrislovecnm Date: Sat, 3 Dec 2016 20:49:22 -0700 Subject: [PATCH] message when we change kubectl context fixing kubectl config path when set to multiple paths --- upup/pkg/kutil/kubecfg_builder.go | 40 +++++++++----- upup/pkg/kutil/kubecfg_builder_test.go | 73 ++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 upup/pkg/kutil/kubecfg_builder_test.go diff --git a/upup/pkg/kutil/kubecfg_builder.go b/upup/pkg/kutil/kubecfg_builder.go index 9d0b9b261b..fbf27e6fa2 100644 --- a/upup/pkg/kutil/kubecfg_builder.go +++ b/upup/pkg/kutil/kubecfg_builder.go @@ -48,18 +48,18 @@ type KubeconfigBuilder struct { ClientKey []byte } +const KUBE_CFG_ENV = clientcmd.RecommendedConfigPathEnvVar + "=%s" + +// Create new KubeconfigBuilder func NewKubeconfigBuilder() *KubeconfigBuilder { c := &KubeconfigBuilder{} c.KubectlPath = "kubectl" // default to in-path - - kubeconfig := os.Getenv(clientcmd.RecommendedConfigPathEnvVar) - if kubeconfig == "" { - kubeconfig = clientcmd.RecommendedHomeFile - } - c.KubeconfigPath = kubeconfig + kubeConfig := os.Getenv(clientcmd.RecommendedConfigPathEnvVar) + c.KubeconfigPath = c.getKubectlPath(kubeConfig) return c } +// Create new Rest Client func (c *KubeconfigBuilder) BuildRestConfig() (*restclient.Config, error) { restConfig := &restclient.Config{ Host: "https://" + c.KubeMasterIP, @@ -79,6 +79,7 @@ func (c *KubeconfigBuilder) BuildRestConfig() (*restclient.Config, error) { return restConfig, nil } +// Write out a new kubeconfig func (c *KubeconfigBuilder) WriteKubecfg() error { tmpdir, err := ioutil.TempDir("", "k8s") if err != nil { @@ -183,20 +184,31 @@ func (c *KubeconfigBuilder) WriteKubecfg() error { return err } } - - split := strings.Split(c.KubeconfigPath, ":") - path := c.KubeconfigPath - if len(split) > 1 { - path = split[0] - } - fmt.Printf("Wrote config for %s to %q\n", c.Context, path) + fmt.Printf("Wrote config for %s to %q\n", c.Context, c.KubeconfigPath) + fmt.Printf("Kop has changed your kubectl context to %s\n", c.Context) return nil } +// get the correct path. Handle empty and multiple values. +func (c *KubeconfigBuilder) getKubectlPath(kubeConfig string) string { + + if kubeConfig == "" { + return clientcmd.RecommendedHomeFile + } + + split := strings.Split(kubeConfig, ":") + if len(split) > 1 { + return split[0] + } + + return kubeConfig +} + + func (c *KubeconfigBuilder) execKubectl(args ...string) error { cmd := exec.Command(c.KubectlPath, args...) env := os.Environ() - env = append(env, fmt.Sprintf("KUBECONFIG=%s", c.KubeconfigPath)) + env = append(env, fmt.Sprintf(KUBE_CFG_ENV, c.KubeconfigPath)) cmd.Env = env glog.V(2).Infof("Running command: %s", strings.Join(cmd.Args, " ")) diff --git a/upup/pkg/kutil/kubecfg_builder_test.go b/upup/pkg/kutil/kubecfg_builder_test.go new file mode 100644 index 0000000000..e9b446710b --- /dev/null +++ b/upup/pkg/kutil/kubecfg_builder_test.go @@ -0,0 +1,73 @@ +/* +Copyright 2016 The Kubernetes 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 kutil + +import ( + "path" + "testing" + + "k8s.io/kubernetes/pkg/util/homedir" +) + +const ( + RecommendedHomeDir = ".kube" + RecommendedFileName = "config" +) + +func TestGetKubectlMultiplePath(t *testing.T) { + c := testCreateKubectlBuilder() + path := c.getKubectlPath(c.KubeconfigPath) + + if path != "/tmp/config" { + t.Fatalf("Wrong path got: %s, but expected /tmp/config", path) + } +} + +func TestGetKubectlSinglePath(t *testing.T) { + c := testCreateKubectlBuilder() + c.KubeconfigPath = "/bar/config" + path := c.getKubectlPath(c.KubeconfigPath) + + if path != "/bar/config" { + t.Fatalf("Wrong path got: %s, but expected /bar/config", path) + } +} + +func TestGetKubectlDefault(t *testing.T) { + c := testCreateKubectlBuilder() + c.KubeconfigPath = "/bar/config" + recommendedHomeFile := path.Join(homedir.HomeDir(), RecommendedHomeDir, RecommendedFileName) + path := c.getKubectlPath("") + + if path != recommendedHomeFile { + t.Fatalf("Wrong path got: %s, but expected /bar/config", path) + } +} + +func testCreateKubectlBuilder() *KubeconfigBuilder { + return &KubeconfigBuilder{ + KubectlPath: "/usr/local/bin/kubectl", + KubeconfigPath: "/tmp/config:/config:path2:path3", + KubeMasterIP: "127.0.0.1", + Context: "my-context", + Namespace: "default", + KubeBearerToken: "token", + KubeUser: "user", + KubePassword: "password", + } + +}