You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/12 15:45:51 UTC

[camel] branch main updated: OpenShift build improvement (#7607)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 007c4b6f7c0 OpenShift build improvement (#7607)
007c4b6f7c0 is described below

commit 007c4b6f7c054a01cf1426ea4ed32c7c0339ba71
Author: Marat Gubaidullin <ma...@gmail.com>
AuthorDate: Thu May 12 11:45:39 2022 -0400

    OpenShift build improvement (#7607)
---
 .../camel/dsl/jbang/core/commands/Deploy.java      |  37 ++---
 .../camel/dsl/jbang/core/commands/Image.java       | 178 ++++++++++-----------
 2 files changed, 102 insertions(+), 113 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java
index 3b1bd598ecd..d0a34a7826a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Deploy.java
@@ -42,7 +42,7 @@ public class Deploy implements Callable<Integer> {
     private String name;
     @CommandLine.Option(names = { "--version" }, description = "Application version (label)")
     private String version = "latest";
-    @CommandLine.Option(names = { "--image" }, description = "Deployment container image name", required = true)
+    @CommandLine.Option(names = { "--image" }, description = "Deployment container image name")
     private String image;
     @CommandLine.Option(names = { "--container-port" }, description = "Container port", defaultValue = "8080")
     private int containerPort;
@@ -63,23 +63,8 @@ public class Deploy implements Callable<Integer> {
 
     @Override
     public Integer call() throws Exception {
-        if (minikube) {
-            System.out.println("Generating Deployment...");
-            Deployment deployment = KubernetesHelper.createDeployment(namespace, name, image, version, containerPort, replicas);
-            System.out.println("Generating Service...");
-            Service service
-                    = KubernetesHelper.createService(namespace, name, version, servicePort, containerPort, minikube, nodePort);
-
-            try (KubernetesClient client = new DefaultKubernetesClient()) {
-                System.out.println("Creating Deployment in " + (minikube ? "Minikube" : "Kubernetes"));
-                client.apps().deployments().inNamespace(namespace).createOrReplace(deployment);
-                client.services().inNamespace(namespace).delete(service);
-                System.out.println("Creating Service in " + (minikube ? "Minikube" : "Kubernetes"));
-                client.services().inNamespace(namespace).createOrReplace(service);
-            } catch (Exception ex) {
-                System.out.println("ERROR: " + ex.getMessage());
-            }
-        } else if (openshift) {
+        if (openshift) {
+            image = image == null ? namespace + "/" + name + ":" + version : image;
             if (!image.startsWith("image-registry.openshift-image-registry.svc:5000") && image.split("/").length != 3) {
                 image = "image-registry.openshift-image-registry.svc:5000/" + image;
             }
@@ -110,6 +95,22 @@ public class Deploy implements Callable<Integer> {
                     System.out.println("ERROR " + ex.getMessage());
                 }
             }
+        } else {
+            System.out.println("Generating Deployment...");
+            Deployment deployment = KubernetesHelper.createDeployment(namespace, name, image, version, containerPort, replicas);
+            System.out.println("Generating Service...");
+            Service service
+                    = KubernetesHelper.createService(namespace, name, version, servicePort, containerPort, minikube, nodePort);
+
+            try (KubernetesClient client = new DefaultKubernetesClient()) {
+                System.out.println("Creating Deployment in " + (minikube ? "Minikube" : "Kubernetes"));
+                client.apps().deployments().inNamespace(namespace).createOrReplace(deployment);
+                client.services().inNamespace(namespace).delete(service);
+                System.out.println("Creating Service in " + (minikube ? "Minikube" : "Kubernetes"));
+                client.services().inNamespace(namespace).createOrReplace(service);
+            } catch (Exception ex) {
+                System.out.println("ERROR: " + ex.getMessage());
+            }
         }
         return 0;
     }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
index 1652a7e5919..8708f6bfbdb 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Image.java
@@ -33,7 +33,6 @@ import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
 import com.google.cloud.tools.jib.api.Jib;
 import com.google.cloud.tools.jib.api.LogEvent;
 import com.google.cloud.tools.jib.api.RegistryImage;
-import com.google.cloud.tools.jib.api.TarImage;
 import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
 import io.fabric8.kubernetes.client.dsl.LogWatch;
 import io.fabric8.openshift.api.model.Build;
@@ -56,22 +55,18 @@ public class Image implements Callable<Integer> {
     private String from;
     @CommandLine.Option(names = { "-j", "--jar" }, description = "Jar file", defaultValue = "camel-runner.jar")
     private String jar;
-    @CommandLine.Option(names = { "-t", "--tag" }, description = "Image tag")
-    private String tag;
     @CommandLine.Option(names = { "--push" }, description = "Push to the registry")
     private boolean push;
     @CommandLine.Option(names = { "-r", "--registry" }, description = "Registry image reference")
     private String registry;
     @CommandLine.Option(names = { "-u", "--username" }, description = "Registry username")
     private String username;
-    @CommandLine.Option(names = { "-p", "--password" }, description = "Registry password")
+    @CommandLine.Option(names = { "-p", "--password" }, description = "Registry password (token)")
     private String password;
-    @CommandLine.Option(names = { "--tar" }, description = "Create tar")
-    private boolean tar;
-    @CommandLine.Option(names = { "--tar-name" }, description = "Tar filename")
-    private String tarname;
-    @CommandLine.Option(names = { "--openshift" }, description = "Target is openshift")
+    @CommandLine.Option(names = { "--openshift" }, description = "Build in OpenShift")
     private boolean openshift;
+    @CommandLine.Option(names = { "--build-config" }, description = "Build in OpenShift using BuildConfig")
+    private boolean buildConfig;
     @CommandLine.Option(names = { "--server" }, description = "Master URL")
     private String server;
     @CommandLine.Option(names = { "--token" }, description = "Token")
@@ -82,117 +77,110 @@ public class Image implements Callable<Integer> {
     private String name;
     @CommandLine.Option(names = { "--version" }, description = "Application version (label)", required = true)
     private String version;
-    @CommandLine.Option(names = { "--source-image" }, description = "Source image name (for OpenShift buildConfig)",
+    @CommandLine.Option(names = { "--source-image" }, description = "Source image name (for OpenShift BuildConfig)",
                         defaultValue = "java:openjdk-11-ubi8")
     private String sourceImage;
 
     @Override
     public Integer call() throws Exception {
         File jarFile = Paths.get(jar).toFile();
-        if (openshift) {
-            System.out.println("Generating resources...");
-            OpenShiftConfig config
-                    = new OpenShiftConfigBuilder().withMasterUrl(server).withOauthToken(token).withNamespace(namespace)
-                            .withTrustCerts(true).build();
-            try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
-                ImageStream imageStream = KubernetesHelper.createImageStream(namespace, name, version);
-                BuildConfig buildConfig
-                        = KubernetesHelper.createBuildConfig(namespace, name, version, jarFile.getName(), sourceImage);
-                System.out.println("Creating ImageStream...");
-                client.imageStreams().createOrReplace(imageStream);
-                System.out.println("Creating BuildConfig...");
-                client.buildConfigs().createOrReplace(buildConfig);
-                System.out.println("Creating Build...");
-                Build build = client.buildConfigs()
-                        .inNamespace(namespace)
-                        .withName(buildConfig.getMetadata().getName())
-                        .instantiateBinary()
-                        .asFile(jarFile.getName())
-                        .withTimeout(5, TimeUnit.MINUTES)
-                        .fromFile(jarFile);
-                while (isNew(build) || isPending(build) || isRunning(build)) {
-                    final String buildName = build.getMetadata().getName();
-                    Build updated = client.builds().withName(buildName).get();
-                    if (updated == null) {
-                        throw new IllegalStateException("Build:" + build.getMetadata().getName() + " is no longer present!");
-                    } else if (updated.getStatus() == null) {
-                        throw new IllegalStateException("Build:" + build.getMetadata().getName() + " has no status!");
-                    } else if (isNew(updated) || isPending(updated) || isRunning(updated)) {
-                        build = updated;
-                        try (LogWatch w
-                                = client.builds().withName(buildName).withPrettyOutput().watchLog();
-                             Reader reader = new InputStreamReader(w.getOutput())) {
-                            display(reader);
-                        } catch (IOException e) {
-                            // This may happen if the LogWatch is closed while we are still reading.
-                            // We shouldn't let the build fail, so let's log a warning and display last few lines of the log
-                            System.out.println("Log stream closed, redisplaying last " + LOG_TAIL_SIZE + " entries:");
-                            try {
-                                display(client.builds().withName(buildName).tailingLines(LOG_TAIL_SIZE)
-                                        .getLogReader());
-                            } catch (IOException ex) {
-                                // Let's ignore this.
-                            }
-                        }
-                    } else if (isComplete(updated)) {
-                        break;
-                    } else if (isCancelled(updated)) {
-                        throw new IllegalStateException("Build:" + buildName + " cancelled!");
-                    } else if (isFailed(updated)) {
-                        throw new IllegalStateException(
-                                "Build:" + buildName + " failed! " + updated.getStatus().getMessage());
-                    } else if (isError(updated)) {
-                        throw new IllegalStateException(
-                                "Build:" + buildName + " encountered error! " + updated.getStatus().getMessage());
-                    }
-                }
-            }
+        if (openshift && buildConfig) {
+            buildInOpenshiftWithBuildConfig(jarFile);
         } else {
+            String host = openshift ? getOpenshiftRegistryHost() : registry;
+            String imageReference = host + "/" + namespace + "/" + name + ":" + version;
             Jib.from(from)
                     .addLayer(Arrays.asList(Paths.get(jar)), "/deployments/")
                     .setWorkingDirectory(AbsoluteUnixPath.get("/deployments"))
                     .setEntrypoint("java", "-jar", jarFile.getName())
-                    .containerize(push ? getRegistry() : (tar ? getTarImage() : getDockerImage()));
+                    .containerize(push ? getRegistry(imageReference) : getDockerImage());
         }
         return 0;
     }
 
-    private Containerizer getDockerImage() throws InvalidImageReferenceException {
-        return Containerizer.to(DockerDaemonImage.named(tag)).addEventHandler(LogEvent.class, getEventConsumer());
+    private String getOpenshiftRegistryHost() {
+        OpenShiftConfig config
+                = new OpenShiftConfigBuilder().withMasterUrl(server).withOauthToken(token).withNamespace(namespace)
+                        .withTrustCerts(true).build();
+        try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
+            String host = client.routes().inNamespace("openshift-image-registry").list().getItems().get(0).getSpec().getHost();
+            return host + "/" + namespace + "/" + name + ":" + version;
+        }
     }
 
-    private Containerizer getTarImage() throws InvalidImageReferenceException {
-        if (tarname == null) {
-            String filename = jar.contains(File.separator) ? jar.substring(jar.lastIndexOf(File.separator) + 1) : jar;
-            tarname = filename.substring(0, filename.lastIndexOf('.')) + ".tar";
+    private void buildInOpenshiftWithBuildConfig(File jarFile) throws InvalidImageReferenceException {
+        System.out.println("Generating resources...");
+        OpenShiftConfig config
+                = new OpenShiftConfigBuilder().withMasterUrl(server).withOauthToken(token).withNamespace(namespace)
+                        .withTrustCerts(true).build();
+        try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
+            ImageStream imageStream = KubernetesHelper.createImageStream(namespace, name, version);
+            BuildConfig buildConfig
+                    = KubernetesHelper.createBuildConfig(namespace, name, version, jarFile.getName(), sourceImage);
+            System.out.println("Creating ImageStream...");
+            client.imageStreams().createOrReplace(imageStream);
+            System.out.println("Creating BuildConfig...");
+            client.buildConfigs().createOrReplace(buildConfig);
+            System.out.println("Creating Build...");
+            Build build = client.buildConfigs()
+                    .inNamespace(namespace)
+                    .withName(buildConfig.getMetadata().getName())
+                    .instantiateBinary()
+                    .asFile(jarFile.getName())
+                    .withTimeout(5, TimeUnit.MINUTES)
+                    .fromFile(jarFile);
+            while (isNew(build) || isPending(build) || isRunning(build)) {
+                final String buildName = build.getMetadata().getName();
+                Build updated = client.builds().withName(buildName).get();
+                if (updated == null) {
+                    throw new IllegalStateException("Build:" + build.getMetadata().getName() + " is no longer present!");
+                } else if (updated.getStatus() == null) {
+                    throw new IllegalStateException("Build:" + build.getMetadata().getName() + " has no status!");
+                } else if (isNew(updated) || isPending(updated) || isRunning(updated)) {
+                    build = updated;
+                    try (LogWatch w
+                            = client.builds().withName(buildName).withPrettyOutput().watchLog();
+                         Reader reader = new InputStreamReader(w.getOutput())) {
+                        display(reader);
+                    } catch (IOException e) {
+                        // This may happen if the LogWatch is closed while we are still reading.
+                        // We shouldn't let the build fail, so let's log a warning and display last few lines of the log
+                        System.out.println("Log stream closed, redisplaying last " + LOG_TAIL_SIZE + " entries:");
+                        try {
+                            display(client.builds().withName(buildName).tailingLines(LOG_TAIL_SIZE)
+                                    .getLogReader());
+                        } catch (IOException ex) {
+                            // Let's ignore this.
+                        }
+                    }
+                } else if (isComplete(updated)) {
+                    break;
+                } else if (isCancelled(updated)) {
+                    throw new IllegalStateException("Build:" + buildName + " cancelled!");
+                } else if (isFailed(updated)) {
+                    throw new IllegalStateException(
+                            "Build:" + buildName + " failed! " + updated.getStatus().getMessage());
+                } else if (isError(updated)) {
+                    throw new IllegalStateException(
+                            "Build:" + buildName + " encountered error! " + updated.getStatus().getMessage());
+                }
+            }
         }
-        return Containerizer.to(TarImage.at(Paths.get(tarname)).named(tag)).addEventHandler(LogEvent.class, getEventConsumer());
     }
 
-    private Containerizer getRegistry() throws InvalidImageReferenceException {
+    private Containerizer getDockerImage() throws InvalidImageReferenceException {
+        String image = namespace + "/" + name + ":" + version;
+        return Containerizer.to(DockerDaemonImage.named(image)).addEventHandler(LogEvent.class, getEventConsumer());
+    }
+
+    private Containerizer getRegistry(String imageReference) throws InvalidImageReferenceException {
         return Containerizer.to(
-                RegistryImage.named(registry).addCredential(username, password))
-                .withAdditionalTag(tag)
+                RegistryImage.named(imageReference).addCredential(username, password))
                 .addEventHandler(LogEvent.class, getEventConsumer());
     }
 
     private Consumer<LogEvent> getEventConsumer() {
-        return event -> {
-            switch (event.getLevel()) {
-                case ERROR:
-                    System.out.println("ERROR: " + event.getMessage());
-                    break;
-                case WARN:
-                    System.out.println("WARN: " + event.getMessage());
-                    break;
-                case DEBUG:
-                    System.out.println("DEBUG: " + event.getMessage());
-                    break;
-                default:
-                    System.out.println(event.getMessage());
-                    break;
-            }
-        };
+        return event -> System.out.println(event.getLevel() + ": " + event.getMessage());
     }
 
     private static void display(Reader logReader) throws IOException {