Skip to content

Commit a1fa6bc

Browse files
authored
Merge pull request #5056 from carlossg/issue-4100
Improve handling KUBECONFIG environment variable with invalid entries
2 parents 45e5093 + 58de23f commit a1fa6bc

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pkg/minikube/kubeconfig/kubeconfig.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ func PathFromEnv() string {
8787
if kubeConfigEnv == "" {
8888
return constants.KubeconfigPath
8989
}
90-
return filepath.SplitList(kubeConfigEnv)[0]
90+
kubeConfigFiles := filepath.SplitList(kubeConfigEnv)
91+
for _, kubeConfigFile := range kubeConfigFiles {
92+
if kubeConfigFile != "" {
93+
return kubeConfigFile
94+
}
95+
glog.Infof("Ignoring empty entry in %s env var", constants.KubeconfigEnvVar)
96+
}
97+
return constants.KubeconfigPath
9198
}
9299

93100
// extractIP returns the IP address stored for minikube in the kubeconfig specified

pkg/minikube/kubeconfig/kubeconfig_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,23 @@ func TestGetKubeConfigPath(t *testing.T) {
555555
input: "/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig",
556556
want: "/home/fake/.kube/.kubeconfig",
557557
},
558+
{
559+
input: ":/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig",
560+
want: "/home/fake/.kube/.kubeconfig",
561+
},
562+
{
563+
input: ":",
564+
want: "$HOME/.kube/config",
565+
},
566+
{
567+
input: "",
568+
want: "$HOME/.kube/config",
569+
},
558570
}
559571

560572
for _, test := range tests {
561573
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, test.input)
562-
if result := PathFromEnv(); result != test.want {
574+
if result := PathFromEnv(); result != os.ExpandEnv(test.want) {
563575
t.Errorf("Expected first splitted chunk, got: %s", result)
564576
}
565577
}

0 commit comments

Comments
 (0)