Skip to content

Commit 0fcf28e

Browse files
authored
Merge pull request kubernetes-sigs#753 from joelanford/home-client-config-fix
🐛 When loading client config, check os/user.HomeDir if $HOME is unset
2 parents 703858f + 5515e31 commit 0fcf28e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pkg/client/config/config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"flag"
2121
"fmt"
2222
"os"
23+
"os/user"
24+
"path"
2325

2426
"k8s.io/client-go/rest"
2527
"k8s.io/client-go/tools/clientcmd"
@@ -117,7 +119,21 @@ func loadConfig(context string) (*rest.Config, error) {
117119

118120
// If the recommended kubeconfig env variable is set, or there
119121
// is no in-cluster config, try the default recommended locations.
120-
if c, err := loadConfigWithContext(apiServerURL, clientcmd.NewDefaultClientConfigLoadingRules(), context); err == nil {
122+
//
123+
// NOTE: For default config file locations, upstream only checks
124+
// $HOME for the user's home directory, but we can also try
125+
// os/user.HomeDir when $HOME is unset.
126+
//
127+
// TODO(jlanford): could this be done upstream?
128+
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
129+
if _, ok := os.LookupEnv("HOME"); !ok {
130+
u, err := user.Current()
131+
if err != nil {
132+
return nil, fmt.Errorf("could not get current user: %v", err)
133+
}
134+
loadingRules.Precedence = append(loadingRules.Precedence, path.Join(u.HomeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName))
135+
}
136+
if c, err := loadConfigWithContext(apiServerURL, loadingRules, context); err == nil {
121137
return c, nil
122138
}
123139

0 commit comments

Comments
 (0)