Maven / Fabric8 - build an OpenShift container image against a remote OpenShift installation

902 views Asked by At

With Docker I need only provide DOCKER_HOST and appropriate credentials in order for the fabric8 plugin to be able to build my container image. As long as a Docker Registry is available - there is no need to install Docker-related infrastructure on the actual host which does the image build (e.g. my Bamboo agent).

With OpenShift I cannot seem to find a way to do this. It seems instead that I must run the maven goals on a host on which OpenShift has been installed, which must necessarily be a RedHat Linux host.

This seems far more complicated than it should be. Am I missing something here?

Is there a way to reference a remote OpenShift environment from the fabric8-maven-plugin when in openshift mode?

Thanks, Robin.

1

There are 1 answers

5
Rohan Kumar On

I'm from Fabric8 team. You don't need to run goals from host on which OpenShift has been install. Plugin reads your ~/.kube/config for information related to your OpenShift cluster and uses your API token in order to interact with OpenShift REST API.

Fabric8 Maven Plugin has been migrated to Eclipse JKube. Although Fabric8 Maven Plugin also works just fine with remote OpenShift Clusters. It's the same Fabric8 Maven Plugin but with a different name and targeted OpenShift Plugin. I would give this answer with Eclipse JKube's OpenShift Maven Plugin. You can have it in your pom.xml like this:

<dependency>
  <groupId>org.eclipse.jkube</groupId>
  <artifactId>openshift-maven-plugin</artifactId>
  <version>1.0.0</version>
  <type>maven-plugin</type>
</dependency>

Here is a demo using a simple Random generator application: https://github.com/rohanKanojia/eclipse-jkube-demo-project

  1. Log into your OpenShift Cluster with credentials provided:
~ : $ oc login https://your-cluster.openshift.com--token=your-token
Logged into "https://your-cluster.openshift.com:443" as "rohanKanojia" using the token provided.

You have one project on this server: "rokumar"

Using project "rokumar".
  1. Go to your project and Issue OpenShift Maven Plugin goals:
eclipse-jkube-demo-project : $ mvn package oc:build oc:resource oc:apply
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< meetup:random-generator >-----------------------
[INFO] Building random-generator 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ random-generator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ random-generator ---

...

[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ random-generator ---
[INFO] Building jar: /home/rohaan/work/repos/eclipse-jkube-demo-project/target/random-generator-0.0.1.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.3.RELEASE:repackage (repackage) @ random-generator ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- openshift-maven-plugin:1.0.1-SNAPSHOT:build (default-cli) @ random-generator ---
[INFO] oc: Using OpenShift build with strategy S2I
[INFO] oc: Running in OpenShift mode
[INFO] oc: Running generator spring-boot
[INFO] oc: spring-boot: Using Docker image quay.io/jkube/jkube-java-binary-s2i:0.0.8 as base / builder
[INFO] oc: [random-generator:0.0.1] "spring-boot": Created docker source tar /home/rohaan/work/repos/eclipse-jkube-demo-project/target/docker/random-generator/0.0.1/tmp/docker-build.tar
[INFO] oc: Adding to Secret pullsecret-jkube
[INFO] oc: Using Secret pullsecret-jkube
[INFO] oc: Creating BuildServiceConfig random-generator-s2i for Source build
[INFO] oc: Adding to ImageStream random-generator
[INFO] oc: Starting Build random-generator-s2i
[INFO] oc: Waiting for build random-generator-s2i-1 to complete...
[INFO] oc: Using quay.io/jkube/jkube-java-binary-s2i:0.0.8 as the s2i builder image
[INFO] oc: INFO S2I source build with plain binaries detected
[INFO] oc: INFO S2I binary build from fabric8-maven-plugin detected
[INFO] oc: INFO Copying binaries from /tmp/src/deployments to /deployments ...
[INFO] oc: random-generator-0.0.1.jar
[INFO] oc: INFO Copying deployments from deployments to /deployments...
[INFO] oc: '/tmp/src/deployments/random-generator-0.0.1.jar' -> '/deployments/random-generator-0.0.1.jar'
[INFO] oc: INFO Cleaning up source directory (/tmp/src)
[INFO] oc: 
[INFO] oc: Pushing image 172.30.39.149:5000/rokumar/random-generator:0.0.1 ...
[INFO] oc: Pushed 0/4 layers, 19% complete
[INFO] oc: Pushed 1/4 layers, 25% complete
[INFO] oc: Push successful
[INFO] oc: Build random-generator-s2i-1 in status Complete
[INFO] oc: Found tag on ImageStream random-generator tag: sha256:51756e1ff9b639c939f3c0c9246ab4ed924faa269bc9cbf05035c859d19fa50c
[INFO] oc: ImageStream random-generator written to /home/rohaan/work/repos/eclipse-jkube-demo-project/target/random-generator-is.yml
[INFO] 
[INFO] --- openshift-maven-plugin:1.0.1-SNAPSHOT:resource (default-cli) @ random-generator ---
[INFO] oc: Using docker image name of namespace: rokumar
[INFO] oc: Running generator spring-boot
[INFO] oc: spring-boot: Using Docker image quay.io/jkube/jkube-java-binary-s2i:0.0.8 as base / builder
[INFO] oc: jkube-controller: Adding a default DeploymentConfig
[INFO] oc: jkube-service: Adding a default service 'random-generator' with ports [8080]
[INFO] oc: jkube-healthcheck-spring-boot: Adding readiness probe on port 8080, path='/actuator/health', scheme='HTTP', with initial delay 10 seconds
[INFO] oc: jkube-healthcheck-spring-boot: Adding liveness probe on port 8080, path='/actuator/health', scheme='HTTP', with initial delay 180 seconds
[INFO] oc: jkube-revision-history: Adding revision history limit to 2
[INFO] oc: validating /home/rohaan/work/repos/eclipse-jkube-demo-project/target/classes/META-INF/jkube/openshift/random-generator-service.yml resource
[INFO] oc: validating /home/rohaan/work/repos/eclipse-jkube-demo-project/target/classes/META-INF/jkube/openshift/random-generator-route.yml resource
[INFO] oc: validating /home/rohaan/work/repos/eclipse-jkube-demo-project/target/classes/META-INF/jkube/openshift/random-generator-deploymentconfig.yml resource
[INFO] 
[INFO] --- openshift-maven-plugin:1.0.1-SNAPSHOT:apply (default-cli) @ random-generator ---
[INFO] oc: Using OpenShift at https://api.rh-idev.openshift.com:443/ in namespace rokumar with manifest /home/rohaan/work/repos/eclipse-jkube-demo-project/target/classes/META-INF/jkube/openshift.yml 
[INFO] oc: OpenShift platform detected
[INFO] oc: Using project: rokumar
[INFO] oc: Creating a Service from openshift.yml namespace rokumar name random-generator
[INFO] oc: Created Service: target/jkube/applyJson/rokumar/service-random-generator.json
[INFO] oc: Creating a DeploymentConfig from openshift.yml namespace rokumar name random-generator
[INFO] oc: Created DeploymentConfig: target/jkube/applyJson/rokumar/deploymentconfig-random-generator.json
[INFO] oc: Creating Route rokumar:random-generator host: null
[INFO] oc: HINT: Use the command `oc get pods -w` to watch your pods start up
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:02 min
[INFO] Finished at: 2020-09-29T10:57:34+05:30
[INFO] ------------------------------------------------------------------------
  1. Access your application deployed into OpenShift
eclipse-jkube-demo-project : $ oc get routes
NAME                      HOST/PORT                                                        PATH      SERVICES                  PORT      TERMINATION   WILDCARD
random-generator          random-generator-rokumar.b6ff.rh-idev.openshiftapps.com                    random-generator          8080                    None
weather-web-application   weather-web-application-rokumar.b6ff.rh-idev.openshiftapps.com             weather-web-application   8080                    None
eclipse-jkube-demo-project : $ curl random-generator-rokumar.b6ff.rh-idev.openshiftapps.com/random | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    45    0    45    0     0     56      0 --:--:-- --:--:-- --:--:--    56
{
  "id": "d79ca3fd-4505-43d1-9920-a39ac15a3b49"
}

For more information, you can checkout Eclipse JKube Quickstarts