Skip to content

Commit

Permalink
Fix error when KUBECONFIG has empty entries (#4100)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Sanchez <carlos@apache.org>
  • Loading branch information
carlossg committed Aug 13, 2019
1 parent 1842311 commit 7558333
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion cmd/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 7558333

Please sign in to comment.