Skip to content

Commit 8ea0239

Browse files
committed
Add support for tokenFile in KubeConfig, fix up/add some tests.
1 parent 5b85b00 commit 8ea0239

File tree

4 files changed

+98
-20
lines changed

4 files changed

+98
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.classpath
55
.settings/
66
.project
7+
.vscode/
78

89
# Maven builds
910
*/target/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
*/
1313
package io.kubernetes.client.util;
1414

15+
import com.google.common.io.CharStreams;
1516
import java.io.File;
1617
import java.io.FileNotFoundException;
1718
import java.io.FileReader;
19+
import java.io.IOException;
1820
import java.io.Reader;
21+
import java.nio.file.Files;
22+
import java.nio.file.FileSystems;
1923
import java.util.ArrayList;
2024
import java.util.Date;
2125
import java.util.Map;
@@ -168,6 +172,16 @@ public String getAccessToken() {
168172
if (currentUser.containsKey("token")) {
169173
return (String) currentUser.get("token");
170174
}
175+
if (currentUser.containsKey("tokenFile")) {
176+
String tokenFile = (String) currentUser.get("tokenFile");
177+
try {
178+
byte[] data = Files.readAllBytes(FileSystems.getDefault().getPath(tokenFile));
179+
return new String(data, "UTF-8");
180+
} catch (IOException ex) {
181+
// TODO use logger here
182+
ex.printStackTrace();
183+
}
184+
}
171185
return null;
172186
}
173187

util/src/test/java/io/kuberentes/client/util/ConfigTest.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Ignore;
2525
import org.junit.Rule;
2626
import org.junit.Test;
27+
import org.junit.rules.TemporaryFolder;
2728
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2829

2930
/**
@@ -33,7 +34,11 @@ public class ConfigTest {
3334
@Rule
3435
public final EnvironmentVariables environmentVariables
3536
= new EnvironmentVariables();
36-
37+
38+
@Rule
39+
public TemporaryFolder folder= new TemporaryFolder();
40+
41+
3742
@Test
3843
public void testDefaultClientNothingPresent() {
3944
environmentVariables.set("HOME", "/non-existent");
@@ -76,8 +81,7 @@ public void testDefaultClientNothingPresent() {
7681

7782
@Before
7883
public void setUp() throws IOException {
79-
dir = new File("/tmp/tester");
80-
dir.mkdir();
84+
dir = folder.newFolder();
8185
kubedir = new File(dir, ".kube");
8286
kubedir.mkdir();
8387
config = new File(kubedir, "config");
@@ -86,29 +90,13 @@ public void setUp() throws IOException {
8690
writer.flush();
8791
writer.close();
8892

89-
configFile = File.createTempFile("config", "");
93+
configFile = folder.newFile("config");
9094
writer = new FileWriter(configFile);
9195
writer.write(KUBECONFIG);
9296
writer.flush();
9397
writer.close();
9498
}
9599

96-
@After
97-
public void tearDown() throws IOException {
98-
if (config != null) {
99-
config.delete();
100-
}
101-
if (kubedir != null) {
102-
kubedir.delete();
103-
}
104-
if (dir != null) {
105-
dir.delete();
106-
}
107-
if (configFile != null) {
108-
configFile.delete();
109-
}
110-
}
111-
112100
@Test
113101
public void testDefaultClientHomeDir() {
114102
try {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util;
14+
15+
import com.google.common.io.ByteStreams;
16+
import io.kubernetes.client.ApiClient;
17+
18+
import java.io.File;
19+
import java.io.FileWriter;
20+
import java.io.IOException;
21+
import java.io.StringReader;
22+
import java.nio.file.Files;
23+
24+
import static org.junit.Assert.*;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Ignore;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import org.junit.rules.TemporaryFolder;
31+
import org.junit.contrib.java.lang.system.EnvironmentVariables;
32+
33+
/**
34+
* Tests for the KubeConfigConfig helper class
35+
*/
36+
public class KubeConfigTest {
37+
38+
@Rule
39+
public TemporaryFolder folder= new TemporaryFolder();
40+
41+
public static String KUBECONFIG_TOKEN =
42+
"apiVersion: v1\n" +
43+
"clusters:\n" +
44+
"- cluster:\n" +
45+
" server: http://kubeconfig.dir.com\n" +
46+
" name: foo\n" +
47+
"users:\n" +
48+
"- user:\n" +
49+
" token: foobaz\n" +
50+
" name: foo\n" +
51+
"contexts:\n" +
52+
"- context:\n" +
53+
" cluster: foo\n" +
54+
" user: foo\n" +
55+
" name: foo-context\n" +
56+
"current-context: foo-context\n";
57+
58+
@Test
59+
public void testToken() {
60+
KubeConfig config = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_TOKEN));
61+
assertEquals(config.getAccessToken(), "foobaz");
62+
}
63+
64+
@Test
65+
public void testTokenFile() throws IOException {
66+
String token = "flubble";
67+
File tokenFile = folder.newFile("token-file.txt");
68+
Files.write(tokenFile.toPath(), token.getBytes("UTF-8"));
69+
70+
String replace = KUBECONFIG_TOKEN.replace("foobaz", tokenFile.getCanonicalPath());
71+
replace = replace.replace("token:", "tokenFile:");
72+
KubeConfig config = KubeConfig.loadKubeConfig(new StringReader(replace));
73+
assertEquals(config.getAccessToken(), token);
74+
}
75+
}

0 commit comments

Comments
 (0)