Skip to content

Commit 6bacd2a

Browse files
committed
Allow using non default context
1 parent b18284d commit 6bacd2a

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,19 @@ public static ApiClient defaultClient() throws IOException {
9797
* cannot be read.
9898
*/
9999
public static ClientBuilder standard() throws IOException {
100-
return standard(true);
100+
return standard(true, null);
101101
}
102102

103-
public static ClientBuilder standard(boolean persistConfig) throws IOException {
103+
public static ClientBuilder standard(String context) throws IOException {
104+
return standard(true, context);
105+
}
106+
107+
public static ClientBuilder standard(boolean persistConfig, String context) throws IOException {
104108
final File kubeConfig = findConfigFromEnv();
105-
ClientBuilder clientBuilderEnv = getClientBuilder(persistConfig, kubeConfig);
109+
ClientBuilder clientBuilderEnv = getClientBuilder(persistConfig, kubeConfig, context);
106110
if (clientBuilderEnv != null) return clientBuilderEnv;
107111
final File config = findConfigInHomeDir();
108-
ClientBuilder clientBuilderHomeDir = getClientBuilder(persistConfig, config);
112+
ClientBuilder clientBuilderHomeDir = getClientBuilder(persistConfig, config, context);
109113
if (clientBuilderHomeDir != null) return clientBuilderHomeDir;
110114
final File clusterCa = new File(SERVICEACCOUNT_CA_PATH);
111115
if (clusterCa.exists()) {
@@ -114,14 +118,14 @@ public static ClientBuilder standard(boolean persistConfig) throws IOException {
114118
return new ClientBuilder();
115119
}
116120

117-
private static ClientBuilder getClientBuilder(boolean persistConfig, File kubeConfig)
121+
private static ClientBuilder getClientBuilder(boolean persistConfig, File kubeConfig, String context)
118122
throws IOException {
119123
if (kubeConfig != null) {
120124
try (BufferedReader kubeConfigReader =
121125
new BufferedReader(
122126
new InputStreamReader(
123127
new FileInputStream(kubeConfig), StandardCharsets.UTF_8.name()))) {
124-
KubeConfig kc = KubeConfig.loadKubeConfig(kubeConfigReader);
128+
KubeConfig kc = KubeConfig.loadKubeConfig(kubeConfigReader, context);
125129
if (persistConfig) {
126130
kc.setPersistConfig(new FilePersister(kubeConfig));
127131
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,36 @@ public static void registerAuthenticator(Authenticator auth) {
8181
registerAuthenticator(new OpenIDConnectAuthenticator());
8282
}
8383

84-
/** Load a Kubernetes config from a Reader */
8584
public static KubeConfig loadKubeConfig(Reader input) {
85+
return loadKubeConfig(input, null);
86+
}
87+
88+
/**
89+
* Load a Kubernetes config from a Reader.
90+
*
91+
* @param input
92+
* @param context if null will use current-context
93+
* @return KubeConfig object
94+
*/
95+
public static KubeConfig loadKubeConfig(Reader input, String context) {
8696
Yaml yaml = new Yaml(new SafeConstructor());
8797
Object config = yaml.load(input);
8898
Map<String, Object> configMap = (Map<String, Object>) config;
8999

90-
String currentContext = (String) configMap.get("current-context");
91100
ArrayList<Object> contexts = (ArrayList<Object>) configMap.get("contexts");
92101
ArrayList<Object> clusters = (ArrayList<Object>) configMap.get("clusters");
93102
ArrayList<Object> users = (ArrayList<Object>) configMap.get("users");
94103
Object preferences = configMap.get("preferences");
95104

96105
KubeConfig kubeConfig = new KubeConfig(contexts, clusters, users);
97-
kubeConfig.setContext(currentContext);
106+
107+
if (context != null) {
108+
kubeConfig.setContext(context);
109+
} else {
110+
String currentContext = (String) configMap.get("current-context");
111+
kubeConfig.setContext(currentContext);
112+
}
113+
98114
kubeConfig.setPreferences(preferences);
99115

100116
return kubeConfig;

util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,18 @@ public void testDetectsServerNotSet() {
317317
ClientBuilder.kubeconfig(kubeConfigWithoutServer);
318318
});
319319
}
320+
321+
@Test
322+
public void testKubeConfigWithContext() throws Exception {
323+
String path =
324+
withEnvironmentVariable("HOME", HOME_PATH)
325+
.and("KUBECONFIG", KUBECONFIG_FILE_PATH)
326+
.execute(
327+
() -> {
328+
final ApiClient client = ClientBuilder.standard("foo-second-context").build();
329+
return client.getBasePath();
330+
});
331+
assertEquals("http://kubeconfig.second.dir.com", path);
332+
}
333+
320334
}

util/src/test/java/io/kubernetes/client/util/KubeConfigTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,30 @@ public void testExecCredentialsCertificate() throws Exception {
491491
assertEquals("key", kc.getCredentials().get(KubeConfig.CRED_CLIENT_KEY_DATA_KEY));
492492
assertNull(kc.getCredentials().get(KubeConfig.CRED_TOKEN_KEY));
493493
}
494+
495+
private static final String KUBECONFIG_WITH_MULTIPLE_CONTEXTS =
496+
"apiVersion: v1\n" +
497+
"clusters:\n" +
498+
" - cluster:\n" +
499+
" server: http://kubeconfig.dir.com\n" +
500+
" name: foo\n" +
501+
" - cluster:\n" +
502+
" server: http://kubeconfig.second.dir.com\n" +
503+
" name: foo-second-cluster\n" +
504+
"contexts:\n" +
505+
" - context:\n" +
506+
" cluster: foo\n" +
507+
" name: foo-context\n" +
508+
" name: foo-context \n" +
509+
" - context:\n" +
510+
" cluster: foo-second-cluster\n" +
511+
" name: foo-second-context\n" +
512+
"current-context: foo-context\n";
513+
514+
@Test
515+
public void testConfigWithContext() throws Exception {
516+
String expectedContext = "foo-second-context";
517+
KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_WITH_MULTIPLE_CONTEXTS), expectedContext);
518+
assertEquals(expectedContext, kc.getCurrentContext());
519+
}
494520
}

util/src/test/resources/kubeconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ clusters:
33
- cluster:
44
server: http://kubeconfig.dir.com
55
name: foo
6+
- cluster:
7+
server: http://kubeconfig.second.dir.com
8+
name: foo-second-cluster
69
contexts:
710
- context:
811
cluster: foo
912
name: foo-context
13+
- context:
14+
cluster: foo-second-cluster
15+
name: foo-second-context
1016
current-context: foo-context

0 commit comments

Comments
 (0)