33
33
import io .kubernetes .client .util .Config ;
34
34
import io .kubernetes .client .util .KubeConfig ;
35
35
36
- public class ConfigBuilder {
36
+ public class ClientBuilder {
37
37
38
- private boolean clusterMode = false ;
39
- private boolean defaultKubeConfigMode = false ;
40
- private boolean defaultClientMode = false ;
41
38
private boolean verifyingSsl = false ;
42
39
private String basePath = null ;
43
40
private File certificateAuthorityFile = null ;
@@ -49,14 +46,15 @@ public class ConfigBuilder {
49
46
private String accessToken = null ;
50
47
private String apiKeyPrefix = null ;
51
48
private KubeConfig kubeConfig = null ;
49
+ private ApiClient client = null ;
52
50
53
51
private static final Logger log = Logger .getLogger (Config .class );
54
52
55
53
public String getUserName () {
56
54
return userName ;
57
55
}
58
56
59
- public ConfigBuilder setUserName (String userName ) {
57
+ public ClientBuilder setUserName (String userName ) {
60
58
this .userName = userName ;
61
59
return this ;
62
60
}
@@ -65,7 +63,7 @@ public String getPassword() {
65
63
return password ;
66
64
}
67
65
68
- public ConfigBuilder setPassword (String password ) {
66
+ public ClientBuilder setPassword (String password ) {
69
67
this .password = password ;
70
68
return this ;
71
69
}
@@ -74,7 +72,7 @@ public String getApiKey() {
74
72
return apiKey ;
75
73
}
76
74
77
- public ConfigBuilder setApiKey (String apiKey ) {
75
+ public ClientBuilder setApiKey (String apiKey ) {
78
76
this .apiKey = apiKey ;
79
77
return this ;
80
78
}
@@ -83,7 +81,7 @@ public String getBasePath() {
83
81
return basePath ;
84
82
}
85
83
86
- public ConfigBuilder setBasePath (String basePath ) {
84
+ public ClientBuilder setBasePath (String basePath ) {
87
85
this .basePath = basePath ;
88
86
return this ;
89
87
}
@@ -92,7 +90,7 @@ public File getCertificateAuthorityFile() {
92
90
return certificateAuthorityFile ;
93
91
}
94
92
95
- public ConfigBuilder setCertificateAuthority (File certificateAuthorityFile ) {
93
+ public ClientBuilder setCertificateAuthority (File certificateAuthorityFile ) {
96
94
this .certificateAuthorityFile = certificateAuthorityFile ;
97
95
this .verifyingSsl = true ;
98
96
return this ;
@@ -102,38 +100,41 @@ public String getCertificateAuthorityData() {
102
100
return certificateAuthorityData ;
103
101
}
104
102
105
- public ConfigBuilder setCertificateAuthority (String certificateAuthorityData ) {
103
+ public ClientBuilder setCertificateAuthority (String certificateAuthorityData ) {
106
104
this .certificateAuthorityData = certificateAuthorityData ;
107
105
this .verifyingSsl = true ;
108
106
return this ;
109
107
}
110
108
111
- public ConfigBuilder setClusterMode () {
112
- this .clusterMode = true ;
109
+ public ClientBuilder setClusterMode () throws IOException {
110
+ this .client = Config . fromCluster () ;
113
111
return this ;
114
112
}
115
113
116
- public ConfigBuilder setKubeConfig (KubeConfig config ) {
114
+ public ClientBuilder setKubeConfig (KubeConfig config ) {
117
115
this .kubeConfig = config ;
116
+ if ( this .kubeConfig !=null ) {
117
+ this .client = Config .fromConfig (this .kubeConfig );
118
+ }
118
119
return this ;
119
120
}
120
121
121
- public ConfigBuilder setDefaultKubeConfigMode () {
122
- this .defaultKubeConfigMode = true ;
122
+ public ClientBuilder setDefaultKubeConfigMode () throws FileNotFoundException {
123
+ this .client = Config . fromConfig ( KubeConfig . loadDefaultKubeConfig ()) ;
123
124
return this ;
124
125
}
125
126
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 ));
128
129
return this ;
129
130
}
130
131
131
- public ConfigBuilder setKubeConfig (Reader input ) {
132
+ public ClientBuilder setKubeConfig (Reader input ) {
132
133
this .kubeConfig = KubeConfig .loadKubeConfig (input );
133
134
return this ;
134
135
}
135
136
136
- public ConfigBuilder setKubeConfig (InputStream stream ) {
137
+ public ClientBuilder setKubeConfig (InputStream stream ) {
137
138
this .kubeConfig = KubeConfig .loadKubeConfig (new InputStreamReader (stream ));
138
139
return this ;
139
140
}
@@ -142,7 +143,7 @@ public KeyManager[] getKeyMgrs() {
142
143
return keyMgrs ;
143
144
}
144
145
145
- public ConfigBuilder setKeyMgrs (KeyManager [] keyMgrs ) {
146
+ public ClientBuilder setKeyMgrs (KeyManager [] keyMgrs ) {
146
147
this .keyMgrs = keyMgrs ;
147
148
return this ;
148
149
}
@@ -151,68 +152,41 @@ public boolean isVerifyingSsl() {
151
152
return verifyingSsl ;
152
153
}
153
154
154
- public ConfigBuilder setVerifyingSsl (boolean verifyingSsl ) {
155
+ public ClientBuilder setVerifyingSsl (boolean verifyingSsl ) {
155
156
this .verifyingSsl = verifyingSsl ;
156
157
return this ;
157
158
}
158
159
159
- public boolean isDefaultClientMode () {
160
- return defaultClientMode ;
161
- }
162
160
163
- public ConfigBuilder setDefaultClientMode () {
164
- this . defaultClientMode = true ;
161
+ public ClientBuilder setDefaultClientMode () throws IOException {
162
+ client = Config . defaultClient () ;
165
163
return this ;
166
164
}
167
165
168
166
public String getApiKeyPrefix () {
169
167
return apiKeyPrefix ;
170
168
}
171
169
172
- public ConfigBuilder setApiKeyPrefix (String apiKeyPrefix ) {
170
+ public ClientBuilder setApiKeyPrefix (String apiKeyPrefix ) {
173
171
this .apiKeyPrefix = apiKeyPrefix ;
174
172
return this ;
175
173
}
176
174
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 ();
198
178
}
199
179
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 ();
207
181
208
- if (basePath != null ) {
182
+ if (basePath != null ) {
209
183
if (basePath .endsWith ("/" )) {
210
184
basePath = basePath .substring (0 , basePath .length () - 1 );
211
185
}
212
186
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 " );
216
190
}
217
191
}
218
192
@@ -255,11 +229,7 @@ public ApiClient build() {
255
229
client .setVerifyingSsl (verifyingSsl );
256
230
257
231
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 ));
263
233
}
264
234
265
235
if (certificateAuthorityData != null ) {
0 commit comments