Skip to content

Commit 2428033

Browse files
review changes and ConfigBuilder to ClientBuilder name change
1 parent bf764a4 commit 2428033

File tree

2 files changed

+66
-89
lines changed

2 files changed

+66
-89
lines changed

util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java renamed to util/src/main/java/io/kubernetes/client/util/ClientBuilder.java

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@
3333
import io.kubernetes.client.util.Config;
3434
import io.kubernetes.client.util.KubeConfig;
3535

36-
public class ConfigBuilder {
36+
public class ClientBuilder {
3737

38-
private boolean clusterMode = false;
39-
private boolean defaultKubeConfigMode = false;
40-
private boolean defaultClientMode = false;
4138
private boolean verifyingSsl = false;
4239
private String basePath = null;
4340
private File certificateAuthorityFile = null;
@@ -49,14 +46,15 @@ public class ConfigBuilder {
4946
private String accessToken = null;
5047
private String apiKeyPrefix = null;
5148
private KubeConfig kubeConfig = null;
49+
private ApiClient client = null;
5250

5351
private static final Logger log = Logger.getLogger(Config.class);
5452

5553
public String getUserName() {
5654
return userName;
5755
}
5856

59-
public ConfigBuilder setUserName(String userName) {
57+
public ClientBuilder setUserName(String userName) {
6058
this.userName = userName;
6159
return this;
6260
}
@@ -65,7 +63,7 @@ public String getPassword() {
6563
return password;
6664
}
6765

68-
public ConfigBuilder setPassword(String password) {
66+
public ClientBuilder setPassword(String password) {
6967
this.password = password;
7068
return this;
7169
}
@@ -74,7 +72,7 @@ public String getApiKey() {
7472
return apiKey;
7573
}
7674

77-
public ConfigBuilder setApiKey(String apiKey) {
75+
public ClientBuilder setApiKey(String apiKey) {
7876
this.apiKey = apiKey;
7977
return this;
8078
}
@@ -83,7 +81,7 @@ public String getBasePath() {
8381
return basePath;
8482
}
8583

86-
public ConfigBuilder setBasePath(String basePath) {
84+
public ClientBuilder setBasePath(String basePath) {
8785
this.basePath = basePath;
8886
return this;
8987
}
@@ -92,7 +90,7 @@ public File getCertificateAuthorityFile() {
9290
return certificateAuthorityFile;
9391
}
9492

95-
public ConfigBuilder setCertificateAuthority(File certificateAuthorityFile) {
93+
public ClientBuilder setCertificateAuthority(File certificateAuthorityFile) {
9694
this.certificateAuthorityFile = certificateAuthorityFile;
9795
this.verifyingSsl = true;
9896
return this;
@@ -102,38 +100,41 @@ public String getCertificateAuthorityData() {
102100
return certificateAuthorityData;
103101
}
104102

105-
public ConfigBuilder setCertificateAuthority(String certificateAuthorityData) {
103+
public ClientBuilder setCertificateAuthority(String certificateAuthorityData) {
106104
this.certificateAuthorityData = certificateAuthorityData;
107105
this.verifyingSsl = true;
108106
return this;
109107
}
110108

111-
public ConfigBuilder setClusterMode() {
112-
this.clusterMode = true;
109+
public ClientBuilder setClusterMode() throws IOException {
110+
this.client = Config.fromCluster();
113111
return this;
114112
}
115113

116-
public ConfigBuilder setKubeConfig(KubeConfig config) {
114+
public ClientBuilder setKubeConfig(KubeConfig config) {
117115
this.kubeConfig = config;
116+
if( this.kubeConfig !=null) {
117+
this.client = Config.fromConfig(this.kubeConfig);
118+
}
118119
return this;
119120
}
120121

121-
public ConfigBuilder setDefaultKubeConfigMode() {
122-
this.defaultKubeConfigMode = true;
122+
public ClientBuilder setDefaultKubeConfigMode() throws FileNotFoundException {
123+
this.client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig());
123124
return this;
124125
}
125126

126-
public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException {
127-
this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(fileName));
127+
public ClientBuilder setKubeConfig(File kubeFile) throws FileNotFoundException {
128+
this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(kubeFile));
128129
return this;
129130
}
130131

131-
public ConfigBuilder setKubeConfig(Reader input) {
132+
public ClientBuilder setKubeConfig(Reader input) {
132133
this.kubeConfig = KubeConfig.loadKubeConfig(input);
133134
return this;
134135
}
135136

136-
public ConfigBuilder setKubeConfig(InputStream stream) {
137+
public ClientBuilder setKubeConfig(InputStream stream) {
137138
this.kubeConfig = KubeConfig.loadKubeConfig(new InputStreamReader(stream));
138139
return this;
139140
}
@@ -142,7 +143,7 @@ public KeyManager[] getKeyMgrs() {
142143
return keyMgrs;
143144
}
144145

145-
public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) {
146+
public ClientBuilder setKeyMgrs(KeyManager[] keyMgrs) {
146147
this.keyMgrs = keyMgrs;
147148
return this;
148149
}
@@ -151,68 +152,41 @@ public boolean isVerifyingSsl() {
151152
return verifyingSsl;
152153
}
153154

154-
public ConfigBuilder setVerifyingSsl(boolean verifyingSsl) {
155+
public ClientBuilder setVerifyingSsl(boolean verifyingSsl) {
155156
this.verifyingSsl = verifyingSsl;
156157
return this;
157158
}
158159

159-
public boolean isDefaultClientMode() {
160-
return defaultClientMode;
161-
}
162160

163-
public ConfigBuilder setDefaultClientMode() {
164-
this.defaultClientMode = true;
161+
public ClientBuilder setDefaultClientMode() throws IOException {
162+
client = Config.defaultClient();
165163
return this;
166164
}
167165

168166
public String getApiKeyPrefix() {
169167
return apiKeyPrefix;
170168
}
171169

172-
public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) {
170+
public ClientBuilder setApiKeyPrefix(String apiKeyPrefix) {
173171
this.apiKeyPrefix = apiKeyPrefix;
174172
return this;
175173
}
176174

177-
public ApiClient build() {
178-
ApiClient client = new ApiClient();
179-
180-
if( kubeConfig !=null) {
181-
client = Config.fromConfig(kubeConfig);
182-
}
183-
184-
if(defaultKubeConfigMode == true) {
185-
try {
186-
client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig());
187-
} catch (FileNotFoundException e) {
188-
log.error("Unable to find the file", e);
189-
}
190-
}
191-
192-
if(clusterMode == true) {
193-
try {
194-
client = Config.fromCluster();
195-
} catch (IOException e) {
196-
log.error("Exception ->", e);
197-
}
175+
public ApiClient build() throws FileNotFoundException {
176+
if(client == null) {
177+
client = new ApiClient();
198178
}
199179

200-
if(defaultClientMode ==true ) {
201-
try {
202-
client = Config.defaultClient();
203-
} catch (IOException e) {
204-
log.error("Exception -> ", e);
205-
}
206-
}
180+
String localBasePath = client.getBasePath();
207181

208-
if (basePath != null ) {
182+
if (basePath != null) {
209183
if(basePath.endsWith("/")) {
210184
basePath = basePath.substring(0, basePath.length() - 1);
211185
}
212186
client.setBasePath(basePath);
213-
} else {
214-
if((clusterMode == false) && (defaultClientMode == false) && (defaultKubeConfigMode == false)) {
215-
throw new IllegalArgumentException("please set kubernetes URL ex:http://localhost");
187+
}else {
188+
if (localBasePath.length() == 0) {
189+
client.setBasePath("http://localhost:8080");
216190
}
217191
}
218192

@@ -255,11 +229,7 @@ public ApiClient build() {
255229
client.setVerifyingSsl(verifyingSsl);
256230

257231
if(certificateAuthorityFile != null) {
258-
try {
259-
client.setSslCaCert(new FileInputStream(certificateAuthorityFile));
260-
} catch (FileNotFoundException e) {
261-
log.error("Unable to find the file", e);
262-
}
232+
client.setSslCaCert(new FileInputStream(certificateAuthorityFile));
263233
}
264234

265235
if(certificateAuthorityData != null) {

util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java renamed to util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
/**
3636
* Tests for the ConfigBuilder helper class
3737
*/
38-
public class ConfigBuilderTest {
38+
public class ClientBuilderTest {
3939
String basePath = "http://localhost";
4040
String apiKey = "ABCD";
4141
String userName = "userName";
@@ -64,10 +64,16 @@ public class ConfigBuilderTest {
6464
@Test
6565
public void testDefaultClientNothingPresent() {
6666
environmentVariables.set("HOME", "/non-existent");
67-
ApiClient client = (new ConfigBuilder())
68-
.setDefaultClientMode()
69-
.build();
70-
assertEquals("http://localhost:8080", client.getBasePath());
67+
ApiClient client;
68+
try {
69+
client = (new ClientBuilder())
70+
.setDefaultClientMode()
71+
.build();
72+
assertEquals("http://localhost:8080", client.getBasePath());
73+
} catch (IOException e) {
74+
// TODO Auto-generated catch block
75+
e.printStackTrace();
76+
}
7177
}
7278

7379
public static String HOME_CONFIG =
@@ -121,7 +127,7 @@ public void setUp() throws IOException {
121127
public void testDefaultClientHomeDir() {
122128
try {
123129
environmentVariables.set("HOME", dir.getCanonicalPath());
124-
ApiClient client = new ConfigBuilder()
130+
ApiClient client = new ClientBuilder()
125131
.setDefaultClientMode()
126132
.build();
127133
assertEquals("http://home.dir.com", client.getBasePath());
@@ -135,7 +141,7 @@ public void testDefaultClientHomeDir() {
135141
public void testDefaultClientKubeConfig() {
136142
try {
137143
environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath());
138-
ApiClient client = new ConfigBuilder()
144+
ApiClient client = new ClientBuilder()
139145
.setDefaultClientMode()
140146
.build();
141147
assertEquals("http://kubeconfig.dir.com", client.getBasePath());
@@ -150,7 +156,7 @@ public void testDefaultClientPrecedence() {
150156
try {
151157
environmentVariables.set("HOME", dir.getCanonicalPath());
152158
environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath());
153-
ApiClient client = new ConfigBuilder()
159+
ApiClient client = new ClientBuilder()
154160
.setDefaultClientMode()
155161
.build();
156162
// $KUBECONFIG should take precedence over $HOME/.kube/config
@@ -162,9 +168,9 @@ public void testDefaultClientPrecedence() {
162168
}
163169

164170
@Test
165-
public void testUserNamePasswordConfigBuilder() {
171+
public void testUserNamePasswordClientBuilder() {
166172
try {
167-
ApiClient client = (new ConfigBuilder())
173+
ApiClient client = (new ClientBuilder())
168174
.setBasePath(basePath)
169175
.setUserName(userName)
170176
.setPassword(password)
@@ -185,11 +191,16 @@ public void testUserNamePasswordConfigBuilder() {
185191
@Test
186192
public void testApiKeyConfigbuilder() {
187193
ApiClient client = null;
188-
client = (new ConfigBuilder())
189-
.setBasePath(basePath)
190-
.setApiKeyPrefix(apiKeyPrefix)
191-
.setApiKey(apiKey)
192-
.build();
194+
try {
195+
client = (new ClientBuilder())
196+
.setBasePath(basePath)
197+
.setApiKeyPrefix(apiKeyPrefix)
198+
.setApiKey(apiKey)
199+
.build();
200+
} catch (FileNotFoundException e) {
201+
// TODO Auto-generated catch block
202+
e.printStackTrace();
203+
}
193204
assertEquals(basePath, client.getBasePath());
194205
assertEquals(false, client.isVerifyingSsl());
195206
assertEquals(apiKeyPrefix, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix());
@@ -203,7 +214,7 @@ public void testKeyMgrANDCertConfigBUilder() {
203214
try{
204215
keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase);
205216
//by default verify ssl is false
206-
ApiClient client = (new ConfigBuilder())
217+
ApiClient client = (new ClientBuilder())
207218
.setBasePath(basePath)
208219
.setKeyMgrs(keyMgrs)
209220
.setCertificateAuthority(certificateAuthorityData)
@@ -220,24 +231,20 @@ public void testKeyMgrANDCertConfigBUilder() {
220231
}
221232

222233
@Test
223-
public void testBasePathIllegalArgumentException() throws IOException {
234+
public void testBasePath() throws IOException {
224235
ApiClient client = null ;
225-
try {
226-
client = (new ConfigBuilder())
236+
client = (new ClientBuilder())
227237
.setUserName("user")
228238
.build();
229-
}
230-
catch(IllegalArgumentException ie) {
231-
assertEquals(IllegalArgumentException.class, ie.getClass());
232-
}
239+
233240
environmentVariables.set("HOME", "/non-existent");
234-
client = (new ConfigBuilder())
241+
client = (new ClientBuilder())
235242
.setDefaultClientMode()
236243
.setUserName("user")
237244
.build();
238245
assertEquals("http://localhost:8080", client.getBasePath());
239246
environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath());
240-
client = new ConfigBuilder()
247+
client = new ClientBuilder()
241248
.setDefaultClientMode()
242249
.setBasePath("http://testkubeconfig.dir.com")
243250
.build();

0 commit comments

Comments
 (0)