Skip to content

Commit dde71be

Browse files
Merge pull request #149 from brendandburns/namespace
Add code for extracting the current namespace.
2 parents 415a2e8 + a839e28 commit dde71be

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class KubeConfig {
4848
Map<String, Object> currentContext;
4949
Map<String, Object> currentCluster;
5050
Map<String, Object> currentUser;
51+
String currentNamespace;
5152

5253
private static final Logger log = Logger.getLogger(KubeConfig.class);
5354

@@ -112,7 +113,7 @@ public boolean setContext(String context) {
112113
}
113114
String cluster = (String) currentContext.get("cluster");
114115
String user = (String) currentContext.get("user");
115-
116+
currentNamespace = (String) currentContext.get("namespace");
116117
if (cluster != null) {
117118
Map<String, Object> obj = findObject(clusters, cluster);
118119
if (obj != null) {
@@ -128,6 +129,10 @@ public boolean setContext(String context) {
128129
return true;
129130
}
130131

132+
public String getNamespace() {
133+
return currentNamespace;
134+
}
135+
131136
public String getServer() {
132137
return getData(currentCluster, "server");
133138
}
@@ -175,7 +180,6 @@ public String getAccessToken() {
175180
if (authConfig != null) {
176181
String name = (String) authProviderMap.get("name");
177182
Authenticator auth = authenticators.get(name);
178-
System.out.println(auth + " for " + name);
179183
if (auth != null) {
180184
if (auth.isExpired(authConfig)) {
181185
authConfig = auth.refresh(authConfig);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public class KubeConfigTest {
4949
" name: foo\n" +
5050
"contexts:\n" +
5151
"- context:\n" +
52-
" cluster: foo\n" +
52+
" cluster: foo\n" +
53+
" namespace: some_namespace\n" +
5354
" user: foo\n" +
5455
" name: foo-context\n" +
5556
"current-context: foo-context\n";
@@ -110,4 +111,16 @@ public void testGCPAuthProvider() {
110111
fail("Unexpected exception: " + ex);
111112
}
112113
}
114+
115+
@Test
116+
public void testNamespace() {
117+
KubeConfig config = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_TOKEN));
118+
assertEquals(config.getNamespace(), "some_namespace");
119+
}
120+
121+
@Test
122+
public void testNullNamespace() {
123+
KubeConfig config = KubeConfig.loadKubeConfig(new StringReader(GCP_CONFIG));
124+
assertNull(config.getNamespace());
125+
}
113126
}

0 commit comments

Comments
 (0)