From 755833315a549b070ee97b2c338ee278a31e830e Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 13 Aug 2019 17:09:28 +0200 Subject: [PATCH] Fix error when KUBECONFIG has empty entries (#4100) Signed-off-by: Carlos Sanchez --- cmd/util/util.go | 9 ++++++++- cmd/util/util_test.go | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/util/util.go b/cmd/util/util.go index ca103bfca12e..26f9c4c186c1 100644 --- a/cmd/util/util.go +++ b/cmd/util/util.go @@ -100,5 +100,12 @@ func GetKubeConfigPath() string { if kubeConfigEnv == "" { return constants.KubeconfigPath } - return filepath.SplitList(kubeConfigEnv)[0] + kubeConfigFiles := filepath.SplitList(kubeConfigEnv) + for _, kubeConfigFile := range kubeConfigFiles { + if kubeConfigFile != "" { + return kubeConfigFile + } + glog.Infof("Ignoring empty entry in %s env var", constants.KubeconfigEnvVar) + } + return constants.KubeconfigPath } diff --git a/cmd/util/util_test.go b/cmd/util/util_test.go index 315762078b02..7e1765cb2b16 100644 --- a/cmd/util/util_test.go +++ b/cmd/util/util_test.go @@ -36,11 +36,19 @@ func TestGetKubeConfigPath(t *testing.T) { input: "/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig", want: "/home/fake/.kube/.kubeconfig", }, + { + input: ":/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig", + want: "/home/fake/.kube/.kubeconfig", + }, + { + input: ":", + want: "$HOME/.kube/config", + }, } for _, test := range tests { os.Setenv(clientcmd.RecommendedConfigPathEnvVar, test.input) - if result := GetKubeConfigPath(); result != test.want { + if result := GetKubeConfigPath(); result != os.ExpandEnv(test.want) { t.Errorf("Expected first splitted chunk, got: %s", result) } }