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 .ByteArrayInputStream ;
16
+ import java .io .File ;
17
+ import java .io .FileInputStream ;
18
+ import java .io .FileNotFoundException ;
19
+ import java .io .FileReader ;
20
+ import java .io .IOException ;
21
+ import java .io .InputStream ;
22
+ import java .io .InputStreamReader ;
23
+ import java .io .Reader ;
24
+ import java .nio .charset .Charset ;
25
+
26
+ import javax .net .ssl .KeyManager ;
27
+
28
+ import org .apache .commons .codec .binary .Base64 ;
29
+ import org .apache .log4j .Logger ;
30
+
31
+ import okio .ByteString ;
32
+ import io .kubernetes .client .ApiClient ;
33
+ import io .kubernetes .client .util .Config ;
34
+ import io .kubernetes .client .util .KubeConfig ;
35
+
36
+ public class ClientBuilder {
37
+
38
+ private boolean verifyingSsl = false ;
39
+ private String basePath = null ;
40
+ private File certificateAuthorityFile = null ;
41
+ private String certificateAuthorityData = null ;
42
+ private String apiKey = null ;
43
+ private String userName = null ;
44
+ private String password = null ;
45
+ private KeyManager [] keyMgrs = null ;
46
+ private String accessToken = null ;
47
+ private String apiKeyPrefix = null ;
48
+ private KubeConfig kubeConfig = null ;
49
+ private ApiClient client = null ;
50
+
51
+ private static final Logger log = Logger .getLogger (Config .class );
52
+
53
+ public String getUserName () {
54
+ return userName ;
55
+ }
56
+
57
+ public ClientBuilder setUserName (String userName ) {
58
+ this .userName = userName ;
59
+ return this ;
60
+ }
61
+
62
+ public String getPassword () {
63
+ return password ;
64
+ }
65
+
66
+ public ClientBuilder setPassword (String password ) {
67
+ this .password = password ;
68
+ return this ;
69
+ }
70
+
71
+ public String getApiKey () {
72
+ return apiKey ;
73
+ }
74
+
75
+ public ClientBuilder setApiKey (String apiKey ) {
76
+ this .apiKey = apiKey ;
77
+ return this ;
78
+ }
79
+
80
+ public String getBasePath () {
81
+ return basePath ;
82
+ }
83
+
84
+ public ClientBuilder setBasePath (String basePath ) {
85
+ this .basePath = basePath ;
86
+ return this ;
87
+ }
88
+
89
+ public File getCertificateAuthorityFile () {
90
+ return certificateAuthorityFile ;
91
+ }
92
+
93
+ public ClientBuilder setCertificateAuthority (File certificateAuthorityFile ) {
94
+ this .certificateAuthorityFile = certificateAuthorityFile ;
95
+ this .verifyingSsl = true ;
96
+ return this ;
97
+ }
98
+
99
+ public String getCertificateAuthorityData () {
100
+ return certificateAuthorityData ;
101
+ }
102
+
103
+ public ClientBuilder setCertificateAuthority (String certificateAuthorityData ) {
104
+ this .certificateAuthorityData = certificateAuthorityData ;
105
+ this .verifyingSsl = true ;
106
+ return this ;
107
+ }
108
+
109
+ public ClientBuilder setClusterMode () throws IOException {
110
+ this .client = Config .fromCluster ();
111
+ return this ;
112
+ }
113
+
114
+ public ClientBuilder setKubeConfig (KubeConfig config ) {
115
+ this .kubeConfig = config ;
116
+ if ( this .kubeConfig !=null ) {
117
+ this .client = Config .fromConfig (this .kubeConfig );
118
+ }
119
+ return this ;
120
+ }
121
+
122
+ public ClientBuilder setDefaultKubeConfigMode () throws FileNotFoundException {
123
+ this .client = Config .fromConfig (KubeConfig .loadDefaultKubeConfig ());
124
+ return this ;
125
+ }
126
+
127
+ public ClientBuilder setKubeConfig (File kubeFile ) throws FileNotFoundException {
128
+ this .kubeConfig = KubeConfig .loadKubeConfig (new FileReader (kubeFile ));
129
+ return this ;
130
+ }
131
+
132
+ public ClientBuilder setKubeConfig (Reader input ) {
133
+ this .kubeConfig = KubeConfig .loadKubeConfig (input );
134
+ return this ;
135
+ }
136
+
137
+ public ClientBuilder setKubeConfig (InputStream stream ) {
138
+ this .kubeConfig = KubeConfig .loadKubeConfig (new InputStreamReader (stream ));
139
+ return this ;
140
+ }
141
+
142
+ public KeyManager [] getKeyMgrs () {
143
+ return keyMgrs ;
144
+ }
145
+
146
+ public ClientBuilder setKeyMgrs (KeyManager [] keyMgrs ) {
147
+ this .keyMgrs = keyMgrs ;
148
+ return this ;
149
+ }
150
+
151
+ public boolean isVerifyingSsl () {
152
+ return verifyingSsl ;
153
+ }
154
+
155
+ public ClientBuilder setVerifyingSsl (boolean verifyingSsl ) {
156
+ this .verifyingSsl = verifyingSsl ;
157
+ return this ;
158
+ }
159
+
160
+
161
+ public ClientBuilder setDefaultClientMode () throws IOException {
162
+ client = Config .defaultClient ();
163
+ return this ;
164
+ }
165
+
166
+ public String getApiKeyPrefix () {
167
+ return apiKeyPrefix ;
168
+ }
169
+
170
+ public ClientBuilder setApiKeyPrefix (String apiKeyPrefix ) {
171
+ this .apiKeyPrefix = apiKeyPrefix ;
172
+ return this ;
173
+ }
174
+
175
+ public ApiClient build () throws FileNotFoundException {
176
+ if (client == null ) {
177
+ client = new ApiClient ();
178
+ }
179
+
180
+ String localBasePath = client .getBasePath ();
181
+
182
+ if (basePath != null ) {
183
+ if (basePath .endsWith ("/" )) {
184
+ basePath = basePath .substring (0 , basePath .length () - 1 );
185
+ }
186
+ client .setBasePath (basePath );
187
+ }else {
188
+ if (localBasePath .length () == 0 ) {
189
+ client .setBasePath ("http://localhost:8080" );
190
+ }
191
+ }
192
+
193
+ if (keyMgrs != null ) {
194
+ client .setKeyManagers (keyMgrs );
195
+ }
196
+
197
+ if (userName != null ){
198
+ client .setUsername (userName );
199
+ }
200
+
201
+ if (password != null ){
202
+ client .setPassword (password );
203
+ }
204
+
205
+ if (( userName != null )&&(password != null )) {
206
+ final String usernameAndPassword = userName + ":" + password ;
207
+ client .setApiKeyPrefix ("Basic" );
208
+ client .setApiKey (ByteString .of (usernameAndPassword .getBytes (Charset .forName ("ISO-8859-1" ))).base64 ());
209
+ }
210
+
211
+ if (accessToken != null ) {
212
+ if (apiKeyPrefix == null ){
213
+ client .setApiKeyPrefix ("Bearer" );
214
+ }
215
+ client .setAccessToken (accessToken );
216
+ }
217
+
218
+ if (apiKeyPrefix != null ) {
219
+ client .setApiKeyPrefix (apiKeyPrefix );
220
+ }
221
+
222
+ if (apiKey != null ) {
223
+ if (apiKeyPrefix == null ){
224
+ client .setApiKeyPrefix ("Bearer" );
225
+ }
226
+ client .setApiKey (apiKey );
227
+ }
228
+
229
+ client .setVerifyingSsl (verifyingSsl );
230
+
231
+ if (certificateAuthorityFile != null ) {
232
+ client .setSslCaCert (new FileInputStream (certificateAuthorityFile ));
233
+ }
234
+
235
+ if (certificateAuthorityData != null ) {
236
+ byte [] bytes = Base64 .decodeBase64 (certificateAuthorityData );
237
+ client .setSslCaCert (new ByteArrayInputStream (bytes ));
238
+ }
239
+
240
+ return client ;
241
+ }
242
+ }
0 commit comments