Cloud Native 11 min read

Using Fabric8 OpenShift Client DSL to Manage OpenShift Resources

This guide demonstrates how to use the Fabric8 OpenShift client DSL in Java to initialize an OpenShift client, create and manage resources such as DeploymentConfig, Route, BuildConfig, Project, ImageStream, and various policies, and perform listing and deletion operations with code examples.

FunTester
FunTester
FunTester
Using Fabric8 OpenShift Client DSL to Manage OpenShift Resources

The OpenShift client (oc) is Red Hat's command‑line tool for interacting with OpenShift clusters, supporting full lifecycle management of containerized applications.

1. Initialize OpenShift client

Both default and custom configurations can be used:

try (OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class)) {
    // use client
}

Config kubeConfig = new ConfigBuilder()
    .withMasterUrl("https://api.example.com:6443")
    .withOauthToken("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
    .build();
try (OpenShiftClient client = new KubernetesClientBuilder().withConfig(kubeConfig).build().adapt(OpenShiftClient.class)) {
    // use client
}

2. DeploymentConfig

Load from YAML, get, create, list and delete DeploymentConfig resources:

DeploymentConfig dc = client.deploymentConfigs()
    .inNamespace("FunTester")
    .load(new FileInputStream("FunTester-deploymentconfig.yml")).item();

DeploymentConfig dcCreated = client.deploymentConfigs()
    .inNamespace("FunTester")
    .resource(new DeploymentConfigBuilder()
        .withNewMetadata().withName("deploymentconfig1").endMetadata()
        .withNewSpec().withReplicas(2)
        .withNewTemplate()
            .withNewMetadata().addToLabels("app","database").endMetadata()
            .withNewSpec()
                .addNewContainer().withName("mysql").withImage("openshift/mysql-55-centos7").endContainer()
            .endSpec()
        .endTemplate()
    .endSpec().build())
    .create();

DeploymentConfigList dcList = client.deploymentConfigs().inNamespace("FunTester").list();
client.deploymentConfigs().inNamespace("FunTester").withName("deploymentconfig1").delete();

3. BuildConfig

Load from YAML and create a BuildConfig:

BuildConfig bc = client.buildConfigs()
    .inNamespace("FunTester")
    .load(new FileInputStream("test-buildconfig.yml")).item();

BuildConfig bcCreated = client.buildConfigs()
    .inNamespace("FunTester")
    .resource(new BuildConfigBuilder()
        .withNewMetadata().withName("bc1").endMetadata()
        .withNewSpec()
            .addNewTrigger().withType("GitHub").withNewGithub().withSecret("secret101").endGithub().endTrigger()
            .withNewSource().withType("Git").withNewGit().withUri("https://github.com/openshift/ruby-hello-world").endGit().endSource()
            .withNewStrategy().withType("Source").withNewSourceStrategy().withNewFrom().withKind("ImageStreamTag").withName("origin-ruby-sample:latest").endFrom().endSourceStrategy().endStrategy()
            .withNewOutput().withNewTo().withKind("ImageStreamTag").withName("origin-ruby-sample:latest").endTo().endOutput()
        .endSpec().build())
    .create();

BuildConfigList bcList = client.buildConfigs().inNamespace("FunTester").list();

4. Route

Load from YAML and create a Route that maps traffic to a Service:

Route route = client.routes()
    .inNamespace("FunTester")
    .load(new FileInputStream("test-route.yml")).item();

Route routeCreated = client.routes()
    .inNamespace("FunTester")
    .resource(new RouteBuilder()
        .withNewMetadata().withName("route1").endMetadata()
        .withNewSpec()
            .withHost("www.FunTester.com")
            .withNewTo().withKind("Service").withName("service-name1").endTo()
        .endSpec().build())
    .create();

5. Project

ProjectRequest request = client.projectrequests().create(new ProjectRequestBuilder()
    .withNewMetadata().withName("thisisatest").endMetadata()
    .withDescription("Fabric8")
    .withDisplayName("Fabric8")
    .build());
ProjectList projectList = client.projects().list();

6. ImageStream

ImageStream is = client.imageStreams()
    .load(new FileInputStream("test-imagestream.yml")).item();
ImageStream isCreated = client.imageStreams()
    .inNamespace("FunTester")
    .resource(new ImageStreamBuilder()
        .withNewMetadata().withName("FunTester-camel-cdi").endMetadata()
        .withNewSpec()
            .addNewTag().withName("latest").endTag()
            .withDockerImageRepository("fabric8/FunTester-camel-cdi")
        .endSpec().build())
    .create();

7. CatalogSource

CatalogSource cs = new CatalogSourceBuilder()
    .withNewMetadata().withName("foo").endMetadata()
    .withNewSpec()
        .withSourceType("Foo")
        .withImage("nginx:latest")
        .withDisplayName("Foo Bar")
        .withPublisher("Fabric8")
    .endSpec().build();
client.operatorHub().catalogSources().inNamespace("FunTester").resource(cs).create();

8. PrometheusRule

PrometheusRule pr = new PrometheusRuleBuilder()
    .withNewMetadata().withName("foo").endMetadata()
    .withNewSpec()
        .addNewGroup().withName("./FunTester-rules")
            .addNewRule().withAlert("FunTesterAlert").withNewExpr().withStrVal("vector(1)").endExpr().endRule()
        .endGroup()
    .endSpec().build();
client.monitoring().prometheusRules().inNamespace("FunTester").resource(pr).create();

9. ServiceMonitor

ServiceMonitor sm = new ServiceMonitorBuilder()
    .withNewMetadata().withName("foo").addToLabels("prometheus","frontend").endMetadata()
    .withNewSpec()
        .withNewNamespaceSelector().withAny(true).endNamespaceSelector()
        .withNewSelector().addToMatchLabels("prometheus","frontend").endSelector()
        .addNewEndpoint().withPort("http-metric").withInterval("15s").endEndpoint()
    .endSpec().build();
client.monitoring().serviceMonitors().inNamespace("FunTester").resource(sm).create();

10. ClusterResourceQuota

Map
hard = new HashMap<>();
hard.put("pods", new Quantity("10"));
hard.put("secrets", new Quantity("20"));
ClusterResourceQuota crq = new ClusterResourceQuotaBuilder()
    .withNewMetadata().withName("foo").endMetadata()
    .withNewSpec()
        .withNewSelector().addToAnnotations("openshift.io/requester","foo-user").endSelector()
        .withQuota(new ResourceQuotaSpecBuilder().withHard(hard).build())
    .endSpec().build();
client.quotas().clusterResourceQuotas().resource(crq).create();

11. EgressNetworkPolicy

EgressNetworkPolicy enp = new EgressNetworkPolicyBuilder()
    .withNewMetadata().withName("foo").withNamespace("FunTester").endMetadata()
    .withNewSpec()
        .addNewEgress().withType("Allow").withNewTo().withCidrSelector("1.2.3.0/24").endTo().endEgress()
        .addNewEgress().withType("Allow").withNewTo().withDnsName("www.foo.com").endTo().endEgress()
    .endSpec().build();
client.egressNetworkPolicies().inNamespace("FunTester").resource(enp).create();

12. Tekton client

try (TektonClient client = new KubernetesClientBuilder().build().adapt(TektonClient.class)) {
    // use client
}
PipelineRun pr = new PipelineRunBuilder()
    .withNewMetadata().withName("demo-run-1").endMetadata()
    .withNewSpec()
        .withNewPipelineRef().withName("demo-pipeline").endPipelineRef()
        .addNewParam().withName("greeting").withNewValue("Hello World!").endParam()
    .endSpec().build();
client.v1().pipelineRuns().inNamespace("FunTester").resource(pr).create();

13. Knative client

try (KnativeClient client = new KubernetesClientBuilder().build().adapt(KnativeClient.class)) {
    // use client
}
Service svc = new ServiceBuilder()
    .withNewMetadata().withName("helloworld-go").endMetadata()
    .withNewSpec()
        .withNewTemplate()
            .withNewSpec()
                .addToContainers(new ContainerBuilder()
                    .withImage("gcr.io/knative-samples/helloworld-go")
                    .addNewEnv().withName("TARGET").withValue("Go Sample V1").endEnv()
                    .build())
            .endSpec()
        .endTemplate()
    .endSpec().build();
client.services().inNamespace("FunTester").resource(svc).serverSideApply();

14. Logging configuration

Set the log level in simplelogger.properties :

org.slf4j.simpleLogger.defaultLogLevel=trace

Full example

The article concludes with a complete Java program ( OpenShiftClientFunTester.java ) that puts together initialization, creation of a DeploymentConfig and a Route, listing of resources, and their deletion, showing the expected console output.

JavaCloud NativekubernetesOpenShiftClient DSLFabric8
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.