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 java .io .FileNotFoundException ;
16
+ import java .io .FileReader ;
17
+ import java .io .IOException ;
18
+ import java .io .InputStream ;
19
+ import java .io .InputStreamReader ;
20
+ import java .io .Reader ;
21
+ import java .nio .charset .Charset ;
22
+
23
+ import javax .net .ssl .KeyManager ;
24
+
25
+ import okio .ByteString ;
26
+ import io .kubernetes .client .ApiClient ;
27
+ import io .kubernetes .client .util .Config ;
28
+ import io .kubernetes .client .util .KubeConfig ;
29
+ import io .kubernetes .client .util .SSLUtils ;
30
+
31
+ public class ConfigBuilder {
32
+
33
+ private boolean trustCerts = false ;
34
+ private boolean clusterMode = false ;
35
+ private boolean defaultKubeConfigMode = false ;
36
+ private boolean defaultClientMode = false ;
37
+ private boolean verifyingSsl = false ;
38
+ private String basePath = null ;
39
+ private String certificateAuthorityFile = null ;
40
+ private String certificateAuthorityData = null ;
41
+ private String apiKey = null ;
42
+ private String userName = null ;
43
+ private String password = null ;
44
+ private KeyManager [] keyMgrs = null ;
45
+ private String accessToken = null ;
46
+ private String apiKeyPrefix = null ;
47
+ private InputStream sslCaCert = null ;
48
+ private KubeConfig kubeConfig = null ;
49
+
50
+ public String getUserName () {
51
+ return userName ;
52
+ }
53
+
54
+ public ConfigBuilder setUserName (String userName ) {
55
+ this .userName = userName ;
56
+ return this ;
57
+ }
58
+
59
+ public String getPassword () {
60
+ return password ;
61
+ }
62
+
63
+ public ConfigBuilder setPassword (String password ) {
64
+ this .password = password ;
65
+ return this ;
66
+ }
67
+
68
+ public String getApiKey () {
69
+ return apiKey ;
70
+ }
71
+
72
+ public ConfigBuilder setApiKey (String apiKey ) {
73
+ this .apiKey = apiKey ;
74
+ return this ;
75
+ }
76
+
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 () {
87
+ return basePath ;
88
+ }
89
+
90
+ public ConfigBuilder setbasePath (String basePath ) {
91
+ this .basePath = basePath ;
92
+ return this ;
93
+ }
94
+
95
+ public String getCertificateAuthorityFile () {
96
+ return certificateAuthorityFile ;
97
+ }
98
+
99
+ public ConfigBuilder setCertificateAuthorityFile (String certificateAuthorityFile ) {
100
+ this .certificateAuthorityFile = certificateAuthorityFile ;
101
+ return this ;
102
+
103
+ }
104
+
105
+ public String getCertificateAuthorityData () {
106
+ return certificateAuthorityData ;
107
+ }
108
+
109
+ public ConfigBuilder setCertificateAuthorityData (String certificateAuthorityData ) {
110
+ this .certificateAuthorityData = certificateAuthorityData ;
111
+ return this ;
112
+ }
113
+
114
+ public ConfigBuilder setClusterMode (boolean clusterMode ) {
115
+ this .clusterMode = clusterMode ;
116
+ return this ;
117
+ }
118
+
119
+ public ConfigBuilder setKubeConfig (KubeConfig config ) {
120
+ this .kubeConfig = config ;
121
+ return this ;
122
+ }
123
+
124
+ public ConfigBuilder setDefaultKubeConfigMode (boolean defaultKubeConfigMode ) {
125
+ this .defaultKubeConfigMode = defaultKubeConfigMode ;
126
+ return this ;
127
+ }
128
+
129
+ public ConfigBuilder setKubeConfig (String fileName ) throws FileNotFoundException {
130
+ this .kubeConfig = KubeConfig .loadKubeConfig (new FileReader (fileName ));
131
+ return this ;
132
+ }
133
+
134
+ public ConfigBuilder setKubeConfig (Reader input ) {
135
+ this .kubeConfig = KubeConfig .loadKubeConfig (input );
136
+ return this ;
137
+ }
138
+
139
+ public ConfigBuilder setKubeConfig (InputStream stream ) {
140
+ this .kubeConfig = KubeConfig .loadKubeConfig (new InputStreamReader (stream ));
141
+ return this ;
142
+ }
143
+
144
+ public KeyManager [] getKeyMgrs () {
145
+ return keyMgrs ;
146
+ }
147
+
148
+ public ConfigBuilder setKeyMgrs (KeyManager [] keyMgrs ) {
149
+ this .keyMgrs = keyMgrs ;
150
+ return this ;
151
+ }
152
+
153
+ public boolean isVerifyingSsl () {
154
+ return verifyingSsl ;
155
+ }
156
+
157
+ public ConfigBuilder setVerifyingSsl (boolean verifyingSsl ) {
158
+ this .verifyingSsl = verifyingSsl ;
159
+ return this ;
160
+ }
161
+
162
+ public boolean isDefaultClientMode () {
163
+ return defaultClientMode ;
164
+ }
165
+
166
+ public ConfigBuilder setDefaultClientMode (boolean defaultClientMode ) {
167
+ this .defaultClientMode = defaultClientMode ;
168
+ return this ;
169
+ }
170
+
171
+ public String getApiKeyPrefix () {
172
+ return apiKeyPrefix ;
173
+ }
174
+
175
+ public ConfigBuilder setApiKeyPrefix (String apiKeyPrefix ) {
176
+ this .apiKeyPrefix = apiKeyPrefix ;
177
+ return this ;
178
+ }
179
+
180
+ public ApiClient build () {
181
+
182
+ ApiClient client = new ApiClient ();
183
+
184
+ if ( kubeConfig !=null ) {
185
+ client = Config .fromConfig (kubeConfig );
186
+ return client ;
187
+ }
188
+
189
+ if (defaultKubeConfigMode == true ) {
190
+ try {
191
+ client = Config .fromConfig (KubeConfig .loadDefaultKubeConfig ());
192
+ } catch (FileNotFoundException e ) {
193
+ e .printStackTrace ();
194
+ }
195
+ return client ;
196
+ }
197
+
198
+ if (clusterMode == true ) {
199
+ try {
200
+ client = Config .fromCluster ();
201
+ } catch (IOException e ) {
202
+ e .printStackTrace ();
203
+ }
204
+ return client ;
205
+ }
206
+
207
+ if (defaultClientMode ==true ) {
208
+ try {
209
+ client = Config .defaultClient ();
210
+ } catch (IOException e ) {
211
+ e .printStackTrace ();
212
+ }
213
+ return client ;
214
+
215
+ }
216
+
217
+ if (basePath != null ) {
218
+ if (basePath .endsWith ("/" )) {
219
+ basePath = basePath .substring (0 , basePath .length () - 1 );
220
+ }
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 ();
230
+ }
231
+ }
232
+
233
+ if (keyMgrs != null ) {
234
+ client .setKeyManagers (keyMgrs );
235
+ }
236
+
237
+ if (userName != null ){
238
+ client .setUsername (userName );
239
+ }
240
+
241
+ if (password != null ){
242
+ client .setPassword (password );
243
+ }
244
+
245
+ if (( userName != null )&&(password != null )) {
246
+ final String usernameAndPassword = userName + ":" + password ;
247
+ client .setApiKeyPrefix ("Basic" );
248
+ client .setApiKey (ByteString .of (usernameAndPassword .getBytes (Charset .forName ("ISO-8859-1" ))).base64 ());
249
+ }
250
+
251
+ if (accessToken != null ) {
252
+ if (apiKeyPrefix == null ){
253
+ client .setApiKeyPrefix ("Bearer" );
254
+ }
255
+ client .setAccessToken (accessToken );
256
+ }
257
+
258
+ if (apiKeyPrefix != null ) {
259
+ client .setApiKeyPrefix (apiKeyPrefix );
260
+ }
261
+
262
+ if (apiKey != null ) {
263
+ if (apiKeyPrefix == null ){
264
+ client .setApiKeyPrefix ("Bearer" );
265
+ }
266
+ client .setApiKey (apiKey );
267
+ }
268
+
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
+ }
280
+ }
281
+ }
282
+
283
+ return client ;
284
+ }
285
+ }
0 commit comments