@@ -20,6 +20,8 @@ import (
20
20
"flag"
21
21
"fmt"
22
22
"os"
23
+ "os/user"
24
+ "path"
23
25
24
26
"k8s.io/client-go/rest"
25
27
"k8s.io/client-go/tools/clientcmd"
@@ -117,7 +119,21 @@ func loadConfig(context string) (*rest.Config, error) {
117
119
118
120
// If the recommended kubeconfig env variable is set, or there
119
121
// 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 {
121
137
return c , nil
122
138
}
123
139
0 commit comments