Merge pull request #5056 from carlossg/issue-4100

Improve handling KUBECONFIG environment variable with invalid entries
pull/5082/head
Medya Ghazizadeh 2019-08-14 11:14:57 -07:00 committed by GitHub
commit a1fa6bc7b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -87,7 +87,14 @@ func PathFromEnv() 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
}
// extractIP returns the IP address stored for minikube in the kubeconfig specified

View File

@ -555,11 +555,23 @@ 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",
},
{
input: "",
want: "$HOME/.kube/config",
},
}
for _, test := range tests {
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, test.input)
if result := PathFromEnv(); result != test.want {
if result := PathFromEnv(); result != os.ExpandEnv(test.want) {
t.Errorf("Expected first splitted chunk, got: %s", result)
}
}