Java client for Kubernetes and OpenShift providing access to full REST APIs via a fluent DSL
npx @tessl/cli install tessl/maven-io-fabric8--kubernetes-client-project@6.13.0The Fabric8 Kubernetes Client provides comprehensive access to both Kubernetes and OpenShift REST APIs through a fluent DSL. It supports both programmatic configuration and automatic configuration from multiple sources including kubeconfig files, service accounts, and environment variables.
pom.xml:<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>6.13.5</version>
</dependency>For OpenShift-specific features:
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>openshift-client</artifactId>
<version>6.13.5</version>
</dependency>Or with Gradle:
implementation 'io.fabric8:kubernetes-client:6.13.5'
// For OpenShift features:
implementation 'io.fabric8:openshift-client:6.13.5'import io.fabric8.kubernetes.client.KubernetesClient;
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;Resource model imports:
import io.fabric8.openshift.api.model.*;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.MixedOperation;import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
// Create OpenShift client using default configuration (kubeconfig, service account, etc.)
try (OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class)) {
// Get cluster information
System.out.println("OpenShift URL: " + client.getOpenshiftUrl());
System.out.println("Current user: " + client.currentUser().getMetadata().getName());
// List all projects
client.projects().list().getItems().forEach(project ->
System.out.println("Project: " + project.getMetadata().getName()));
// Work with builds in a specific namespace
client.inNamespace("my-project").builds().list().getItems().forEach(build ->
System.out.println("Build: " + build.getMetadata().getName()));
}Configure OpenShift client with custom settings, authentication, and connection options.
// Method 1: Using OpenShiftConfig with KubernetesClientBuilder
OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken("your-token")
.withNamespace("default")
.withTrustCerts(true)
.build();
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);
// Method 2: Using ConfigBuilder with KubernetesClientBuilder
Config config = new ConfigBuilder()
.withMasterUrl("https://api.openshift-cluster.example.com:6443")
.withOauthToken("your-token")
.build();
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);Client Configuration and Creation
Manage OpenShift builds and build configurations, including binary builds, webhook triggers, and log streaming.
Build build = client.builds()
.inNamespace("my-project")
.withName("my-app-1")
.get();
String buildLog = client.builds()
.inNamespace("my-project")
.withName("my-app-1")
.getLog();Work with OpenShift DeploymentConfigs including scaling, rollouts, and pod operations.
DeploymentConfig dc = client.deploymentConfigs()
.inNamespace("my-project")
.withName("my-app")
.scale(3);
List<Pod> pods = client.deploymentConfigs()
.inNamespace("my-project")
.withName("my-app")
.getPods();Manage container images, image streams, image stream tags, and image imports.
ImageStream imageStream = client.imageStreams()
.inNamespace("my-project")
.withName("my-app")
.get();
ImageStreamImport importResult = client.imageStreamImports()
.inNamespace("my-project")
.create(imageStreamImport);Configure OpenShift routes, network policies, and cluster networking.
Route route = client.routes()
.inNamespace("my-project")
.withName("my-app")
.get();
String routeUrl = "https://" + route.getSpec().getHost();Process OpenShift templates with parameters and instantiate applications.
Template template = client.templates()
.inNamespace("my-project")
.withName("my-template")
.withParameters("PARAM_NAME", "param-value")
.process();Create and manage OpenShift projects with role bindings and quotas.
Project project = client.projects()
.createProjectAndRoleBindings("new-project",
"Project description", "display-name", "admin-user");Manage users, groups, roles, role bindings, and security context constraints.
User currentUser = client.currentUser();
List<Role> roles = client.roles()
.inNamespace("my-project")
.list().getItems();Access specialized OpenShift API groups for configuration, operators, monitoring, and more.
// Access config API group (config.openshift.io)
ClusterVersion clusterVersion = client.config().clusterVersions()
.withName("version")
.get();
// Access monitoring API group (monitoring.coreos.com)
ServiceMonitor monitor = client.monitoring().serviceMonitors()
.inNamespace("my-project")
.withName("my-monitor")
.get();Manage OpenShift OAuth tokens, clients, and access tokens for authentication.
// OAuth access tokens
List<OAuthAccessToken> tokens = client.oAuthAccessTokens().list().getItems();
// OAuth clients
OAuthClient oauthClient = client.oAuthClients()
.withName("my-oauth-client")
.get();
// User OAuth access tokens
List<UserOAuthAccessToken> userTokens = client.userOAuthAccessTokens()
.list().getItems();Manage OpenShift identities, users, and user identity mappings.
// Get current user
User currentUser = client.currentUser();
// List users
List<User> users = client.users().list().getItems();
// List identities
List<Identity> identities = client.identities().list().getItems();
// Create user identity mapping
UserIdentityMapping mapping = client.userIdentityMappings()
.create(userIdentityMapping);Manage cluster-wide networking resources including cluster networks and host subnets.
// Cluster networks
List<ClusterNetwork> clusterNetworks = client.clusterNetworks()
.list().getItems();
// Host subnets
List<HostSubnet> hostSubnets = client.hostSubnets()
.list().getItems();
// Net namespaces
List<NetNamespace> netNamespaces = client.netNamespaces()
.list().getItems();Access OpenShift quota API group for advanced resource management.
// Access quota API group resources
client.quotas().clusterResourceQuotas()
.list().getItems();Manage OpenShift Machine Config Operator resources for cluster configuration.
// Access machine configuration API group
client.machineConfigurations().machineConfigs()
.list().getItems();
// Machine API group
client.machine().machines()
.inNamespace("openshift-machine-api")
.list().getItems();Get OpenShift cluster version and cluster information.
// Get OpenShift version
VersionInfo openshiftVersion = client.getOpenShiftV3Version();
String v4Version = client.getOpenShiftV4Version();
// Get cluster URL
URL openshiftUrl = client.getOpenshiftUrl();
// Check API group support
boolean supportsApiGroup = client.supportsOpenShiftAPIGroup("config.openshift.io");Perform access reviews and security policy validations.
SubjectAccessReviewResponse response = client.subjectAccessReviews()
.create(subjectAccessReview);
boolean canAccess = response.getAllowed();The OpenShift client extends the Kubernetes client with OpenShift-specific functionality:
public interface OpenShiftClient extends KubernetesClient, SupportTestingClient {
String BASE_API_GROUP = "openshift.io";
// Core OpenShift client methods
URL getOpenshiftUrl();
User currentUser();
boolean supportsOpenShiftAPIGroup(String apiGroup);
VersionInfo getOpenShiftV3Version();
String getOpenShiftV4Version();
// API Group DSLs
OpenShiftConfigAPIGroupDSL config();
OpenShiftConsoleAPIGroupDSL console();
OpenShiftClusterAutoscalingAPIGroupDSL clusterAutoscaling();
OpenShiftHiveAPIGroupDSL hive();
OpenShiftOperatorAPIGroupDSL operator();
OpenShiftOperatorHubAPIGroupDSL operatorHub();
OpenShiftMonitoringAPIGroupDSL monitoring();
MachineConfigurationAPIGroupDSL machineConfigurations();
OpenShiftMachineAPIGroupDSL machine();
OpenShiftQuotaAPIGroupDSL quotas();
OpenShiftTunedAPIGroupDSL tuned();
OpenShiftStorageVersionMigratorApiGroupDSL kubeStorageVersionMigrator();
OpenShiftWhereaboutsAPIGroupDSL whereabouts();
// Resource operations
MixedOperation<Build, BuildList, BuildResource> builds();
MixedOperation<BuildConfig, BuildConfigList, BuildConfigResource<BuildConfig, Void, Build>> buildConfigs();
MixedOperation<DeploymentConfig, DeploymentConfigList, DeployableScalableResource<DeploymentConfig>> deploymentConfigs();
MixedOperation<Route, RouteList, Resource<Route>> routes();
ParameterMixedOperation<Template, TemplateList, TemplateResource> templates();
MixedOperation<ImageStream, ImageStreamList, Resource<ImageStream>> imageStreams();
MixedOperation<ImageStreamTag, ImageStreamTagList, Resource<ImageStreamTag>> imageStreamTags();
// Project operations
ProjectOperation projects();
ProjectRequestOperation projectrequests();
// Security and RBAC
NonNamespaceOperation<SecurityContextConstraints, SecurityContextConstraintsList, Resource<SecurityContextConstraints>> securityContextConstraints();
MixedOperation<Role, RoleList, Resource<Role>> roles();
MixedOperation<RoleBinding, RoleBindingList, Resource<RoleBinding>> roleBindings();
NonNamespaceOperation<ClusterRole, ClusterRoleList, Resource<ClusterRole>> clusterRoles();
MixedOperation<ClusterRoleBinding, ClusterRoleBindingList, Resource<ClusterRoleBinding>> clusterRoleBindings();
// User and identity management
NonNamespaceOperation<User, UserList, Resource<User>> users();
NonNamespaceOperation<Group, GroupList, Resource<Group>> groups();
NonNamespaceOperation<Identity, IdentityList, Resource<Identity>> identities();
// OAuth operations
NonNamespaceOperation<OAuthAccessToken, OAuthAccessTokenList, Resource<OAuthAccessToken>> oAuthAccessTokens();
NonNamespaceOperation<OAuthClient, OAuthClientList, Resource<OAuthClient>> oAuthClients();
NonNamespaceOperation<UserOAuthAccessToken, UserOAuthAccessTokenList, Resource<UserOAuthAccessToken>> userOAuthAccessTokens();
// Network operations
NonNamespaceOperation<ClusterNetwork, ClusterNetworkList, Resource<ClusterNetwork>> clusterNetworks();
NonNamespaceOperation<HostSubnet, HostSubnetList, Resource<HostSubnet>> hostSubnets();
NonNamespaceOperation<NetNamespace, NetNamespaceList, Resource<NetNamespace>> netNamespaces();
MixedOperation<EgressNetworkPolicy, EgressNetworkPolicyList, Resource<EgressNetworkPolicy>> egressNetworkPolicies();
// Authorization reviews
InOutCreateable<SubjectAccessReview, SubjectAccessReviewResponse> subjectAccessReviews();
InOutCreateable<ResourceAccessReview, ResourceAccessReviewResponse> resourceAccessReviews();
NamespacedInOutCreateable<LocalSubjectAccessReview, SubjectAccessReviewResponse> localSubjectAccessReviews();
NamespacedInOutCreateable<LocalResourceAccessReview, ResourceAccessReviewResponse> localResourceAccessReviews();
}
public interface NamespacedOpenShiftClient extends OpenShiftClient, NamespacedKubernetesClient {
}
public class OpenShiftConfig extends Config {
public static final Long DEFAULT_BUILD_TIMEOUT = 5 * 60 * 1000L;
public String getOapiVersion();
public String getOpenShiftUrl();
public long getBuildTimeout();
public boolean isDisableApiGroupCheck();
}public class OpenShiftConfigBuilder {
public OpenShiftConfigBuilder withMasterUrl(String masterUrl);
public OpenShiftConfigBuilder withOauthToken(String token);
public OpenShiftConfigBuilder withUsername(String username);
public OpenShiftConfigBuilder withPassword(String password);
public OpenShiftConfigBuilder withNamespace(String namespace);
public OpenShiftConfigBuilder withTrustCerts(boolean trustCerts);
public OpenShiftConfigBuilder withBuildTimeout(long timeout);
public OpenShiftConfig build();
}// Recommended approach using KubernetesClientBuilder
OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);
// With custom configuration
OpenShiftClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()
.adapt(OpenShiftClient.class);
// Legacy approach (deprecated)
@Deprecated
OpenShiftClient client = new DefaultOpenShiftClient();
@Deprecated
OpenShiftClient client = new DefaultOpenShiftClient(config);