mirror of https://github.com/kubernetes/kops.git
message when we change kubectl context
fixing kubectl config path when set to multiple paths
This commit is contained in:
parent
38676a07eb
commit
bad489cb75
|
|
@ -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, " "))
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue