Skip to content

Commit 5491ea3

Browse files
committed
pkg/client/config: when loading config, check os/user.HomeDir if $HOME is unset
1 parent 8022402 commit 5491ea3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

pkg/client/config/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"flag"
2121
"fmt"
2222
"os"
23+
"os/user"
2324

2425
"k8s.io/client-go/rest"
2526
"k8s.io/client-go/tools/clientcmd"
@@ -117,6 +118,22 @@ func loadConfig(context string) (*rest.Config, error) {
117118

118119
// If the recommended kubeconfig env variable is set, or there
119120
// is no in-cluster config, try the default recommended locations.
121+
//
122+
// NOTE: For default config file locations, upstream only checks
123+
// $HOME for the user's home directory, but we can also use
124+
// os/user.HomeDir when $HOME is unset.
125+
//
126+
// TODO(jlanford): could this be done upstream?
127+
if _, ok := os.LookupEnv("HOME"); !ok {
128+
u, err := user.Current()
129+
if err != nil {
130+
return nil, fmt.Errorf("could not get current user: %v", err)
131+
}
132+
if err := os.Setenv("HOME", u.HomeDir); err != nil {
133+
return nil, fmt.Errorf("could not set HOME env var: %v", err)
134+
}
135+
defer func() { _ = os.Unsetenv("HOME") }()
136+
}
120137
if c, err := loadConfigWithContext(apiServerURL, clientcmd.NewDefaultClientConfigLoadingRules(), context); err == nil {
121138
return c, nil
122139
}

0 commit comments

Comments
 (0)