Skip to content

Commit 268d429

Browse files
authored
Merge pull request #390 from brendandburns/fix
Expand HOME detection on Windows.
2 parents e3f7b18 + f146afe commit 268d429

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

util/src/main/java/io/kubernetes/client/util/ClientBuilder.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import static io.kubernetes.client.util.Config.ENV_SERVICE_PORT;
1818
import static io.kubernetes.client.util.Config.SERVICEACCOUNT_CA_PATH;
1919
import static io.kubernetes.client.util.Config.SERVICEACCOUNT_TOKEN_PATH;
20-
import static io.kubernetes.client.util.KubeConfig.*;
20+
import static io.kubernetes.client.util.KubeConfig.ENV_HOME;
21+
import static io.kubernetes.client.util.KubeConfig.KUBECONFIG;
22+
import static io.kubernetes.client.util.KubeConfig.KUBEDIR;
2123

2224
import io.kubernetes.client.ApiClient;
2325
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
@@ -77,7 +79,7 @@ public static ClientBuilder standard(boolean persistConfig) throws IOException {
7779
final File kubeConfig = findConfigFromEnv();
7880
if (kubeConfig != null) {
7981
try (FileReader kubeConfigReader = new FileReader(kubeConfig)) {
80-
KubeConfig kc = loadKubeConfig(kubeConfigReader);
82+
KubeConfig kc = KubeConfig.loadKubeConfig(kubeConfigReader);
8183
if (persistConfig) {
8284
kc.setPersistConfig(new FilePersister(kubeConfig));
8385
}
@@ -87,7 +89,7 @@ public static ClientBuilder standard(boolean persistConfig) throws IOException {
8789
final File config = findConfigInHomeDir();
8890
if (config != null) {
8991
try (FileReader configReader = new FileReader(config)) {
90-
KubeConfig kc = loadKubeConfig(configReader);
92+
KubeConfig kc = KubeConfig.loadKubeConfig(configReader);
9193
if (persistConfig) {
9294
kc.setPersistConfig(new FilePersister(config));
9395
}
@@ -115,14 +117,44 @@ private static File findConfigFromEnv() {
115117
}
116118
}
117119

118-
private static File findConfigInHomeDir() {
119-
final File config = new File(new File(System.getenv(ENV_HOME), KUBEDIR), KUBECONFIG);
120+
private static File findHomeDir() {
121+
final File config = new File(System.getenv(ENV_HOME));
120122
if (config.exists()) {
121123
return config;
122-
} else {
123-
log.debug("Could not find ~/.kube/config");
124-
return null;
125124
}
125+
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
126+
String homeDrive = System.getenv("HOMEDRIVE");
127+
String homePath = System.getenv("HOMEPATH");
128+
if (homeDrive != null
129+
&& homeDrive.length() > 0
130+
&& homePath != null
131+
&& homePath.length() > 0) {
132+
File homeDir = new File(new File(homeDrive), homePath);
133+
if (homeDir.exists()) {
134+
return homeDir;
135+
}
136+
}
137+
String userProfile = System.getenv("USERPROFILE");
138+
if (userProfile != null && userProfile.length() > 0) {
139+
File profileDir = new File(userProfile);
140+
if (profileDir.exists()) {
141+
return profileDir;
142+
}
143+
}
144+
}
145+
return null;
146+
}
147+
148+
private static File findConfigInHomeDir() {
149+
final File homeDir = findHomeDir();
150+
if (homeDir != null) {
151+
final File config = new File(new File(homeDir, KUBEDIR), KUBECONFIG);
152+
if (config.exists()) {
153+
return config;
154+
}
155+
}
156+
log.debug("Could not find ~/.kube/config");
157+
return null;
126158
}
127159

128160
/**

util/src/main/java/io/kubernetes/client/util/KubeConfig.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class KubeConfig {
3434
private static final Logger log = LoggerFactory.getLogger(KubeConfig.class);
3535

3636
// Defaults for where to find a kubeconfig file
37-
public static final String ENV_HOME;
37+
public static final String ENV_HOME = "HOME";
3838
public static final String KUBEDIR = ".kube";
3939
public static final String KUBECONFIG = "config";
4040
private static Map<String, Authenticator> authenticators = new HashMap<>();
@@ -63,12 +63,6 @@ public static void registerAuthenticator(Authenticator auth) {
6363
static {
6464
registerAuthenticator(new GCPAuthenticator());
6565
registerAuthenticator(new AzureActiveDirectoryAuthenticator());
66-
67-
if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
68-
ENV_HOME = "USERPROFILE";
69-
} else {
70-
ENV_HOME = "HOME";
71-
}
7266
}
7367

7468
/** Load a Kubernetes config from a Reader */

0 commit comments

Comments
 (0)