12
12
*/
13
13
package io .kubernetes .client .util ;
14
14
15
+ import java .io .ByteArrayInputStream ;
16
+ import java .io .File ;
17
+ import java .io .FileInputStream ;
15
18
import java .io .FileNotFoundException ;
16
19
import java .io .FileReader ;
17
20
import java .io .IOException ;
22
25
23
26
import javax .net .ssl .KeyManager ;
24
27
28
+ import org .apache .commons .codec .binary .Base64 ;
29
+ import org .apache .log4j .Logger ;
30
+
25
31
import okio .ByteString ;
26
32
import io .kubernetes .client .ApiClient ;
27
33
import io .kubernetes .client .util .Config ;
28
34
import io .kubernetes .client .util .KubeConfig ;
29
- import io .kubernetes .client .util .SSLUtils ;
30
35
31
- public class ConfigBuilder {
36
+ public class ConfigBuilder {
32
37
33
- private boolean trustCerts = false ;
34
38
private boolean clusterMode = false ;
35
39
private boolean defaultKubeConfigMode = false ;
36
40
private boolean defaultClientMode = false ;
37
41
private boolean verifyingSsl = false ;
38
42
private String basePath = null ;
39
- private String certificateAuthorityFile = null ;
43
+ private File certificateAuthorityFile = null ;
40
44
private String certificateAuthorityData = null ;
41
45
private String apiKey = null ;
42
46
private String userName = null ;
43
47
private String password = null ;
44
48
private KeyManager [] keyMgrs = null ;
45
49
private String accessToken = null ;
46
50
private String apiKeyPrefix = null ;
47
- private InputStream sslCaCert = null ;
48
51
private KubeConfig kubeConfig = null ;
49
52
53
+ private static final Logger log = Logger .getLogger (Config .class );
54
+
50
55
public String getUserName () {
51
56
return userName ;
52
57
}
@@ -74,45 +79,37 @@ public ConfigBuilder setApiKey(String apiKey) {
74
79
return this ;
75
80
}
76
81
77
- public boolean isTrustCerts () {
78
- return trustCerts ;
79
- }
80
-
81
- public ConfigBuilder setTrustCerts (boolean trustCerts ) {
82
- this .trustCerts = trustCerts ;
83
- return this ;
84
- }
85
-
86
- public String getbasePath () {
82
+ public String getBasePath () {
87
83
return basePath ;
88
84
}
89
85
90
- public ConfigBuilder setbasePath (String basePath ) {
86
+ public ConfigBuilder setBasePath (String basePath ) {
91
87
this .basePath = basePath ;
92
88
return this ;
93
89
}
94
90
95
- public String getCertificateAuthorityFile () {
91
+ public File getCertificateAuthorityFile () {
96
92
return certificateAuthorityFile ;
97
93
}
98
94
99
- public ConfigBuilder setCertificateAuthorityFile ( String certificateAuthorityFile ) {
95
+ public ConfigBuilder setCertificateAuthority ( File certificateAuthorityFile ) {
100
96
this .certificateAuthorityFile = certificateAuthorityFile ;
97
+ this .verifyingSsl = true ;
101
98
return this ;
102
-
103
99
}
104
100
105
101
public String getCertificateAuthorityData () {
106
102
return certificateAuthorityData ;
107
103
}
108
104
109
- public ConfigBuilder setCertificateAuthorityData (String certificateAuthorityData ) {
105
+ public ConfigBuilder setCertificateAuthority (String certificateAuthorityData ) {
110
106
this .certificateAuthorityData = certificateAuthorityData ;
107
+ this .verifyingSsl = true ;
111
108
return this ;
112
109
}
113
110
114
- public ConfigBuilder setClusterMode (boolean clusterMode ) {
115
- this .clusterMode = clusterMode ;
111
+ public ConfigBuilder setClusterMode () {
112
+ this .clusterMode = true ;
116
113
return this ;
117
114
}
118
115
@@ -121,8 +118,8 @@ public ConfigBuilder setKubeConfig(KubeConfig config) {
121
118
return this ;
122
119
}
123
120
124
- public ConfigBuilder setDefaultKubeConfigMode (boolean defaultKubeConfigMode ) {
125
- this .defaultKubeConfigMode = defaultKubeConfigMode ;
121
+ public ConfigBuilder setDefaultKubeConfigMode () {
122
+ this .defaultKubeConfigMode = true ;
126
123
return this ;
127
124
}
128
125
@@ -131,12 +128,12 @@ public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException
131
128
return this ;
132
129
}
133
130
134
- public ConfigBuilder setKubeConfig (Reader input ) {
131
+ public ConfigBuilder setKubeConfig (Reader input ) {
135
132
this .kubeConfig = KubeConfig .loadKubeConfig (input );
136
133
return this ;
137
134
}
138
135
139
- public ConfigBuilder setKubeConfig (InputStream stream ) {
136
+ public ConfigBuilder setKubeConfig (InputStream stream ) {
140
137
this .kubeConfig = KubeConfig .loadKubeConfig (new InputStreamReader (stream ));
141
138
return this ;
142
139
}
@@ -163,8 +160,8 @@ public boolean isDefaultClientMode() {
163
160
return defaultClientMode ;
164
161
}
165
162
166
- public ConfigBuilder setDefaultClientMode (boolean defaultClientMode ) {
167
- this .defaultClientMode = defaultClientMode ;
163
+ public ConfigBuilder setDefaultClientMode () {
164
+ this .defaultClientMode = true ;
168
165
return this ;
169
166
}
170
167
@@ -177,56 +174,45 @@ public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) {
177
174
return this ;
178
175
}
179
176
180
- public ApiClient build () {
181
-
177
+ public ApiClient build () {
182
178
ApiClient client = new ApiClient ();
183
179
184
180
if ( kubeConfig !=null ) {
185
181
client = Config .fromConfig (kubeConfig );
186
- return client ;
187
182
}
188
183
189
184
if (defaultKubeConfigMode == true ) {
190
185
try {
191
186
client = Config .fromConfig (KubeConfig .loadDefaultKubeConfig ());
192
187
} catch (FileNotFoundException e ) {
193
- e . printStackTrace ( );
188
+ log . error ( "Unable to find the file" , e );
194
189
}
195
- return client ;
196
190
}
197
191
198
192
if (clusterMode == true ) {
199
193
try {
200
194
client = Config .fromCluster ();
201
195
} catch (IOException e ) {
202
- e . printStackTrace ( );
196
+ log . error ( "Exception ->" , e );
203
197
}
204
- return client ;
205
198
}
206
199
207
200
if (defaultClientMode ==true ) {
208
201
try {
209
202
client = Config .defaultClient ();
210
203
} catch (IOException e ) {
211
- e . printStackTrace ( );
204
+ log . error ( "Exception -> " , e );
212
205
}
213
- return client ;
214
-
215
206
}
216
207
217
208
if (basePath != null ) {
218
209
if (basePath .endsWith ("/" )) {
219
210
basePath = basePath .substring (0 , basePath .length () - 1 );
220
211
}
221
- client .setBasePath (basePath )
222
- .setVerifyingSsl (verifyingSsl );
223
- }
224
-
225
- else {
226
- try {
227
- throw new Exception ("set kubernetes URL. example: http://localhost" );
228
- } catch (Exception e ) {
229
- e .printStackTrace ();
212
+ client .setBasePath (basePath );
213
+ } else {
214
+ if ((clusterMode == false ) && (defaultClientMode == false ) && (defaultKubeConfigMode == false )) {
215
+ throw new IllegalArgumentException ("please set kubernetes URL ex:http://localhost" );
230
216
}
231
217
}
232
218
@@ -266,20 +252,21 @@ public ApiClient build() {
266
252
client .setApiKey (apiKey );
267
253
}
268
254
269
- if (sslCaCert != null ) {
270
- client .setSslCaCert (sslCaCert );
271
- }
272
-
273
- if (verifyingSsl ){
274
- if ((certificateAuthorityData != null ) || (certificateAuthorityFile != null )){
275
- try {
276
- client .setSslCaCert (SSLUtils .getInputStreamFromDataOrFile (certificateAuthorityData , certificateAuthorityFile ));
277
- } catch (FileNotFoundException e ) {
278
- e .printStackTrace ();
279
- }
255
+ client .setVerifyingSsl (verifyingSsl );
256
+
257
+ if (certificateAuthorityFile != null ) {
258
+ try {
259
+ client .setSslCaCert (new FileInputStream (certificateAuthorityFile ));
260
+ } catch (FileNotFoundException e ) {
261
+ log .error ("Unable to find the file" , e );
280
262
}
281
263
}
282
264
265
+ if (certificateAuthorityData != null ) {
266
+ byte [] bytes = Base64 .decodeBase64 (certificateAuthorityData );
267
+ client .setSslCaCert (new ByteArrayInputStream (bytes ));
268
+ }
269
+
283
270
return client ;
284
271
}
285
- }
272
+ }
0 commit comments