0
# Client Configuration and Creation
1
2
This document covers OpenShift client configuration, authentication methods, and client creation patterns.
3
4
## Core Imports
5
6
```java { .api }
7
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
8
import io.fabric8.openshift.client.OpenShiftClient;
9
import io.fabric8.openshift.client.OpenShiftConfig;
10
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
11
import io.fabric8.openshift.client.NamespacedOpenShiftClient;
12
import io.fabric8.kubernetes.client.Config;
13
import io.fabric8.kubernetes.client.ConfigBuilder;
14
import io.fabric8.kubernetes.client.RequestConfig;
15
```
16
17
## Client Creation
18
19
### Basic Client Creation
20
21
Create a client using default configuration sources (kubeconfig, service accounts, environment variables):
22
23
```java { .api }
24
// Recommended approach: Auto-configure from environment
25
OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);
26
27
// With try-with-resources for automatic cleanup
28
try (OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class)) {
29
// Use client
30
}
31
```
32
33
### Client with Custom Configuration
34
35
```java { .api }
36
// Method 1: Using OpenShiftConfig
37
OpenShiftConfig config = new OpenShiftConfigBuilder()
38
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
39
.withOauthToken("your-oauth-token")
40
.withNamespace("my-project")
41
.withTrustCerts(true)
42
.build();
43
44
OpenShiftClient client = new KubernetesClientBuilder()
45
.withConfig(config)
46
.build()
47
.adapt(OpenShiftClient.class);
48
49
// Method 2: Using standard Config
50
Config config = new ConfigBuilder()
51
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
52
.withOauthToken("your-oauth-token")
53
.withNamespace("my-project")
54
.withTrustCerts(true)
55
.build();
56
57
OpenShiftClient client = new KubernetesClientBuilder()
58
.withConfig(config)
59
.build()
60
.adapt(OpenShiftClient.class);
61
```
62
63
### Wrapping Kubernetes Config
64
65
Convert existing Kubernetes configuration to OpenShift configuration:
66
67
```java { .api }
68
Config kubernetesConfig = new ConfigBuilder()
69
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
70
.withOauthToken("token")
71
.build();
72
73
OpenShiftConfig openShiftConfig = new OpenShiftConfig(kubernetesConfig);
74
OpenShiftClient client = new KubernetesClientBuilder()
75
.withConfig(openShiftConfig)
76
.build()
77
.adapt(OpenShiftClient.class);
78
```
79
80
## Authentication Methods
81
82
### OAuth Token Authentication
83
84
```java { .api }
85
OpenShiftConfig config = new OpenShiftConfigBuilder()
86
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
87
.withOauthToken("your-oauth-token")
88
.build();
89
```
90
91
### Username/Password Authentication
92
93
```java { .api }
94
OpenShiftConfig config = new OpenShiftConfigBuilder()
95
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
96
.withUsername("your-username")
97
.withPassword("your-password")
98
.build();
99
100
OpenShiftClient client = new KubernetesClientBuilder()
101
.withConfig(config)
102
.build()
103
.adapt(OpenShiftClient.class);
104
```
105
106
### Service Account Authentication
107
108
When running in a pod with a service account:
109
110
```java { .api }
111
// Automatically uses service account token from mounted volume
112
OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);
113
```
114
115
### Certificate-based Authentication
116
117
```java { .api }
118
OpenShiftConfig config = new OpenShiftConfigBuilder()
119
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
120
.withClientCertFile("/path/to/client.crt")
121
.withClientKeyFile("/path/to/client.key")
122
.withCaCertFile("/path/to/ca.crt")
123
.build();
124
125
OpenShiftClient client = new KubernetesClientBuilder()
126
.withConfig(config)
127
.build()
128
.adapt(OpenShiftClient.class);
129
```
130
131
## Configuration Options
132
133
### Connection Settings
134
135
```java { .api }
136
OpenShiftConfig config = new OpenShiftConfigBuilder()
137
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
138
.withConnectionTimeout(30000) // 30 seconds
139
.withRequestTimeout(60000) // 60 seconds
140
.withWatchReconnectInterval(1000) // 1 second
141
.withWatchReconnectLimit(10)
142
.build();
143
```
144
145
### TLS and Security Settings
146
147
```java { .api }
148
OpenShiftConfig config = new OpenShiftConfigBuilder()
149
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
150
.withTrustCerts(true) // Trust self-signed certificates
151
.withDisableHostnameVerification(true)
152
.withCaCertFile("/path/to/ca.crt")
153
.build();
154
```
155
156
### OpenShift-specific Settings
157
158
```java { .api }
159
OpenShiftConfig config = new OpenShiftConfigBuilder()
160
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
161
.withOapiVersion("v1") // OpenShift API version
162
.withBuildTimeout(300000L) // 5 minutes for build operations
163
.withDisableApiGroupCheck(false) // Check API group availability
164
.build();
165
```
166
167
### Proxy Configuration
168
169
```java { .api }
170
OpenShiftConfig config = new OpenShiftConfigBuilder()
171
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
172
.withHttpProxy("http://proxy.example.com:8080")
173
.withHttpsProxy("https://proxy.example.com:8080")
174
.withNoProxy(new String[]{"localhost", "127.0.0.1", ".local"})
175
.withProxyUsername("proxy-user")
176
.withProxyPassword("proxy-pass")
177
.build();
178
```
179
180
## Namespace Operations
181
182
### Namespace-scoped Client
183
184
Create a client scoped to a specific namespace:
185
186
```java { .api }
187
NamespacedOpenShiftClient namespacedClient = client.inNamespace("my-project");
188
189
// All operations will be scoped to "my-project" namespace
190
BuildList builds = namespacedClient.builds().list();
191
RouteList routes = namespacedClient.routes().list();
192
```
193
194
### Switching Namespaces
195
196
```java { .api }
197
// Work in different namespaces
198
client.inNamespace("project1").builds().list();
199
client.inNamespace("project2").deploymentConfigs().list();
200
201
// Remove namespace restrictions
202
client.inAnyNamespace().projects().list();
203
```
204
205
## Request Configuration
206
207
### Per-request Configuration
208
209
```java { .api }
210
RequestConfig requestConfig = new RequestConfig();
211
requestConfig.setConnectionTimeout(10000);
212
requestConfig.setRequestTimeout(30000);
213
214
NamespacedOpenShiftClient configuredClient = client.withRequestConfig(requestConfig);
215
```
216
217
## Client Information and Capabilities
218
219
### Cluster Information
220
221
```java { .api }
222
// Get OpenShift cluster URL
223
URL openshiftUrl = client.getOpenshiftUrl();
224
225
// Get version information
226
VersionInfo kubernetesVersion = client.getVersion();
227
VersionInfo openShiftV3Version = client.getOpenShiftV3Version();
228
String openShiftV4Version = client.getOpenShiftV4Version();
229
230
// Check API group support
231
boolean supportsConfig = client.supportsOpenShiftAPIGroup("config.openshift.io");
232
```
233
234
### Current User Information
235
236
```java { .api }
237
// Get current user (equivalent to 'oc whoami')
238
User currentUser = client.currentUser();
239
String username = currentUser.getMetadata().getName();
240
```
241
242
### Client Capabilities
243
244
```java { .api }
245
// Check if cluster supports OpenShift APIs
246
boolean isOpenShift = client.isSupported();
247
248
// Get client configuration
249
OpenShiftConfig config = (OpenShiftConfig) client.getConfiguration();
250
```
251
252
## Usage Examples
253
254
### Complete Configuration Example
255
256
```java
257
import io.fabric8.openshift.client.*;
258
259
public class OpenShiftClientExample {
260
public void createConfiguredClient() {
261
OpenShiftConfig config = new OpenShiftConfigBuilder()
262
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
263
.withOauthToken(System.getenv("OPENSHIFT_TOKEN"))
264
.withNamespace("my-application")
265
.withConnectionTimeout(30000)
266
.withRequestTimeout(60000)
267
.withTrustCerts(true)
268
.withBuildTimeout(600000L) // 10 minutes for builds
269
.build();
270
271
try (OpenShiftClient client = new OpenShiftClientBuilder()
272
.withConfig(config)
273
.build()) {
274
275
// Verify connection
276
System.out.println("Connected to: " + client.getOpenshiftUrl());
277
System.out.println("Current user: " + client.currentUser().getMetadata().getName());
278
279
// Use client for operations
280
performOperations(client);
281
}
282
}
283
284
private void performOperations(OpenShiftClient client) {
285
// Client operations here
286
}
287
}
288
```
289
290
### Environment-based Configuration
291
292
```java
293
public class EnvironmentBasedClient {
294
public OpenShiftClient createClient() {
295
// Uses environment variables, service account, or kubeconfig automatically
296
return new OpenShiftClientBuilder().build();
297
}
298
299
public OpenShiftClient createCustomClient() {
300
OpenShiftConfigBuilder builder = new OpenShiftConfigBuilder();
301
302
// Override with environment variables if present
303
String masterUrl = System.getenv("OPENSHIFT_MASTER_URL");
304
if (masterUrl != null) {
305
builder.withMasterUrl(masterUrl);
306
}
307
308
String token = System.getenv("OPENSHIFT_TOKEN");
309
if (token != null) {
310
builder.withOauthToken(token);
311
}
312
313
String namespace = System.getenv("OPENSHIFT_NAMESPACE");
314
if (namespace != null) {
315
builder.withNamespace(namespace);
316
}
317
318
return new OpenShiftClientBuilder()
319
.withConfig(builder.build())
320
.build();
321
}
322
}
323
```
324
325
## Types
326
327
### OpenShiftConfig
328
329
```java { .api }
330
public class OpenShiftConfig extends Config {
331
public static final Long DEFAULT_BUILD_TIMEOUT = 5 * 60 * 1000L;
332
public static final String KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY = "kubernetes.oapi.version";
333
public static final String OPENSHIFT_URL_SYSTEM_PROPERTY = "openshift.url";
334
public static final String OPENSHIFT_BUILD_TIMEOUT_SYSTEM_PROPERTY = "openshift.build.timeout";
335
336
public OpenShiftConfig();
337
public OpenShiftConfig(Config kubernetesConfig);
338
public OpenShiftConfig(Config kubernetesConfig, String openShiftUrl);
339
340
public static OpenShiftConfig wrap(Config config);
341
342
public String getOapiVersion();
343
public void setOapiVersion(String oapiVersion);
344
345
public String getOpenShiftUrl();
346
public void setOpenShiftUrl(String openShiftUrl);
347
348
public long getBuildTimeout();
349
public void setBuildTimeout(long buildTimeout);
350
351
public boolean isDisableApiGroupCheck();
352
public void setDisableApiGroupCheck(boolean disableApiGroupCheck);
353
}
354
```
355
356
### OpenShiftConfigBuilder
357
358
```java { .api }
359
public class OpenShiftConfigBuilder extends ConfigBuilder<OpenShiftConfigBuilder, OpenShiftConfig> {
360
public OpenShiftConfigBuilder withOapiVersion(String oapiVersion);
361
public OpenShiftConfigBuilder withOpenShiftUrl(String openShiftUrl);
362
public OpenShiftConfigBuilder withBuildTimeout(Long buildTimeout);
363
public OpenShiftConfigBuilder withDisableApiGroupCheck(Boolean disableApiGroupCheck);
364
365
// Inherited from ConfigBuilder
366
public OpenShiftConfigBuilder withMasterUrl(String masterUrl);
367
public OpenShiftConfigBuilder withApiVersion(String apiVersion);
368
public OpenShiftConfigBuilder withNamespace(String namespace);
369
public OpenShiftConfigBuilder withOauthToken(String oauthToken);
370
public OpenShiftConfigBuilder withUsername(String username);
371
public OpenShiftConfigBuilder withPassword(String password);
372
public OpenShiftConfigBuilder withTrustCerts(Boolean trustCerts);
373
public OpenShiftConfigBuilder withDisableHostnameVerification(Boolean disableHostnameVerification);
374
public OpenShiftConfigBuilder withCaCertFile(String caCertFile);
375
public OpenShiftConfigBuilder withCaCertData(String caCertData);
376
public OpenShiftConfigBuilder withClientCertFile(String clientCertFile);
377
public OpenShiftConfigBuilder withClientCertData(String clientCertData);
378
public OpenShiftConfigBuilder withClientKeyFile(String clientKeyFile);
379
public OpenShiftConfigBuilder withClientKeyData(String clientKeyData);
380
public OpenShiftConfigBuilder withConnectionTimeout(Integer connectionTimeout);
381
public OpenShiftConfigBuilder withRequestTimeout(Integer requestTimeout);
382
public OpenShiftConfigBuilder withWatchReconnectInterval(Integer watchReconnectInterval);
383
public OpenShiftConfigBuilder withWatchReconnectLimit(Integer watchReconnectLimit);
384
public OpenShiftConfigBuilder withHttpProxy(String httpProxy);
385
public OpenShiftConfigBuilder withHttpsProxy(String httpsProxy);
386
public OpenShiftConfigBuilder withNoProxy(String[] noProxy);
387
public OpenShiftConfigBuilder withProxyUsername(String proxyUsername);
388
public OpenShiftConfigBuilder withProxyPassword(String proxyPassword);
389
390
public OpenShiftConfig build();
391
}
392
```
393
394
### OpenShiftClientBuilder
395
396
```java { .api }
397
public class OpenShiftClientBuilder extends KubernetesClientBuilder {
398
public OpenShiftClientBuilder withConfig(Config config);
399
public OpenShiftClient build();
400
}
401
```