Java client for Kubernetes and OpenShift providing access to full REST APIs via a fluent DSL
—
This document covers OpenShift client configuration, authentication methods, and client creation patterns.
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
import io.fabric8.openshift.client.NamespacedOpenShiftClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.RequestConfig;Create a client using default configuration sources (kubeconfig, service accounts, environment variables):
// Recommended approach: Auto-configure from environment
OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);
// With try-with-resources for automatic cleanup
try (OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class)) {
// Use client
}// Method 1: Using OpenShiftConfig
OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken("your-oauth-token")
.withNamespace("my-project")
.withTrustCerts(true)
.build();
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);
// Method 2: Using standard Config
Config config = new ConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken("your-oauth-token")
.withNamespace("my-project")
.withTrustCerts(true)
.build();
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);Convert existing Kubernetes configuration to OpenShift configuration:
Config kubernetesConfig = new ConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken("token")
.build();
OpenShiftConfig openShiftConfig = new OpenShiftConfig(kubernetesConfig);
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(openShiftConfig)
.build()
.adapt(OpenShiftClient.class);OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken("your-oauth-token")
.build();OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withUsername("your-username")
.withPassword("your-password")
.build();
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);When running in a pod with a service account:
// Automatically uses service account token from mounted volume
OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withClientCertFile("/path/to/client.crt")
.withClientKeyFile("/path/to/client.key")
.withCaCertFile("/path/to/ca.crt")
.build();
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withConnectionTimeout(30000) // 30 seconds
.withRequestTimeout(60000) // 60 seconds
.withWatchReconnectInterval(1000) // 1 second
.withWatchReconnectLimit(10)
.build();OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withTrustCerts(true) // Trust self-signed certificates
.withDisableHostnameVerification(true)
.withCaCertFile("/path/to/ca.crt")
.build();OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOapiVersion("v1") // OpenShift API version
.withBuildTimeout(300000L) // 5 minutes for build operations
.withDisableApiGroupCheck(false) // Check API group availability
.build();OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withHttpProxy("http://proxy.example.com:8080")
.withHttpsProxy("https://proxy.example.com:8080")
.withNoProxy(new String[]{"localhost", "127.0.0.1", ".local"})
.withProxyUsername("proxy-user")
.withProxyPassword("proxy-pass")
.build();Create a client scoped to a specific namespace:
NamespacedOpenShiftClient namespacedClient = client.inNamespace("my-project");
// All operations will be scoped to "my-project" namespace
BuildList builds = namespacedClient.builds().list();
RouteList routes = namespacedClient.routes().list();// Work in different namespaces
client.inNamespace("project1").builds().list();
client.inNamespace("project2").deploymentConfigs().list();
// Remove namespace restrictions
client.inAnyNamespace().projects().list();RequestConfig requestConfig = new RequestConfig();
requestConfig.setConnectionTimeout(10000);
requestConfig.setRequestTimeout(30000);
NamespacedOpenShiftClient configuredClient = client.withRequestConfig(requestConfig);// Get OpenShift cluster URL
URL openshiftUrl = client.getOpenshiftUrl();
// Get version information
VersionInfo kubernetesVersion = client.getVersion();
VersionInfo openShiftV3Version = client.getOpenShiftV3Version();
String openShiftV4Version = client.getOpenShiftV4Version();
// Check API group support
boolean supportsConfig = client.supportsOpenShiftAPIGroup("config.openshift.io");// Get current user (equivalent to 'oc whoami')
User currentUser = client.currentUser();
String username = currentUser.getMetadata().getName();// Check if cluster supports OpenShift APIs
boolean isOpenShift = client.isSupported();
// Get client configuration
OpenShiftConfig config = (OpenShiftConfig) client.getConfiguration();import io.fabric8.openshift.client.*;
public class OpenShiftClientExample {
public void createConfiguredClient() {
OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken(System.getenv("OPENSHIFT_TOKEN"))
.withNamespace("my-application")
.withConnectionTimeout(30000)
.withRequestTimeout(60000)
.withTrustCerts(true)
.withBuildTimeout(600000L) // 10 minutes for builds
.build();
try (OpenShiftClient client = new OpenShiftClientBuilder()
.withConfig(config)
.build()) {
// Verify connection
System.out.println("Connected to: " + client.getOpenshiftUrl());
System.out.println("Current user: " + client.currentUser().getMetadata().getName());
// Use client for operations
performOperations(client);
}
}
private void performOperations(OpenShiftClient client) {
// Client operations here
}
}public class EnvironmentBasedClient {
public OpenShiftClient createClient() {
// Uses environment variables, service account, or kubeconfig automatically
return new OpenShiftClientBuilder().build();
}
public OpenShiftClient createCustomClient() {
OpenShiftConfigBuilder builder = new OpenShiftConfigBuilder();
// Override with environment variables if present
String masterUrl = System.getenv("OPENSHIFT_MASTER_URL");
if (masterUrl != null) {
builder.withMasterUrl(masterUrl);
}
String token = System.getenv("OPENSHIFT_TOKEN");
if (token != null) {
builder.withOauthToken(token);
}
String namespace = System.getenv("OPENSHIFT_NAMESPACE");
if (namespace != null) {
builder.withNamespace(namespace);
}
return new OpenShiftClientBuilder()
.withConfig(builder.build())
.build();
}
}public class OpenShiftConfig extends Config {
public static final Long DEFAULT_BUILD_TIMEOUT = 5 * 60 * 1000L;
public static final String KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY = "kubernetes.oapi.version";
public static final String OPENSHIFT_URL_SYSTEM_PROPERTY = "openshift.url";
public static final String OPENSHIFT_BUILD_TIMEOUT_SYSTEM_PROPERTY = "openshift.build.timeout";
public OpenShiftConfig();
public OpenShiftConfig(Config kubernetesConfig);
public OpenShiftConfig(Config kubernetesConfig, String openShiftUrl);
public static OpenShiftConfig wrap(Config config);
public String getOapiVersion();
public void setOapiVersion(String oapiVersion);
public String getOpenShiftUrl();
public void setOpenShiftUrl(String openShiftUrl);
public long getBuildTimeout();
public void setBuildTimeout(long buildTimeout);
public boolean isDisableApiGroupCheck();
public void setDisableApiGroupCheck(boolean disableApiGroupCheck);
}public class OpenShiftConfigBuilder extends ConfigBuilder<OpenShiftConfigBuilder, OpenShiftConfig> {
public OpenShiftConfigBuilder withOapiVersion(String oapiVersion);
public OpenShiftConfigBuilder withOpenShiftUrl(String openShiftUrl);
public OpenShiftConfigBuilder withBuildTimeout(Long buildTimeout);
public OpenShiftConfigBuilder withDisableApiGroupCheck(Boolean disableApiGroupCheck);
// Inherited from ConfigBuilder
public OpenShiftConfigBuilder withMasterUrl(String masterUrl);
public OpenShiftConfigBuilder withApiVersion(String apiVersion);
public OpenShiftConfigBuilder withNamespace(String namespace);
public OpenShiftConfigBuilder withOauthToken(String oauthToken);
public OpenShiftConfigBuilder withUsername(String username);
public OpenShiftConfigBuilder withPassword(String password);
public OpenShiftConfigBuilder withTrustCerts(Boolean trustCerts);
public OpenShiftConfigBuilder withDisableHostnameVerification(Boolean disableHostnameVerification);
public OpenShiftConfigBuilder withCaCertFile(String caCertFile);
public OpenShiftConfigBuilder withCaCertData(String caCertData);
public OpenShiftConfigBuilder withClientCertFile(String clientCertFile);
public OpenShiftConfigBuilder withClientCertData(String clientCertData);
public OpenShiftConfigBuilder withClientKeyFile(String clientKeyFile);
public OpenShiftConfigBuilder withClientKeyData(String clientKeyData);
public OpenShiftConfigBuilder withConnectionTimeout(Integer connectionTimeout);
public OpenShiftConfigBuilder withRequestTimeout(Integer requestTimeout);
public OpenShiftConfigBuilder withWatchReconnectInterval(Integer watchReconnectInterval);
public OpenShiftConfigBuilder withWatchReconnectLimit(Integer watchReconnectLimit);
public OpenShiftConfigBuilder withHttpProxy(String httpProxy);
public OpenShiftConfigBuilder withHttpsProxy(String httpsProxy);
public OpenShiftConfigBuilder withNoProxy(String[] noProxy);
public OpenShiftConfigBuilder withProxyUsername(String proxyUsername);
public OpenShiftConfigBuilder withProxyPassword(String proxyPassword);
public OpenShiftConfig build();
}public class OpenShiftClientBuilder extends KubernetesClientBuilder {
public OpenShiftClientBuilder withConfig(Config config);
public OpenShiftClient build();
}Install with Tessl CLI
npx tessl i tessl/maven-io-fabric8--kubernetes-client-project