From 74290e4aa0571241411792849ead7843989cd3a7 Mon Sep 17 00:00:00 2001 From: Yubao Liu Date: Tue, 20 Sep 2022 23:48:43 +0800 Subject: [PATCH] Support "INHERIT" for quarkus.jib.jvm-entrypoint and quarkus.jib.native-entrypoint jib-maven-plugin and jib-gradle-plugin both support this option to inherit entrypoint from base image, so make Quarkus learn it. Fixed https://github.com/quarkusio/quarkus/issues/28071 --- .../image/jib/deployment/JibConfig.java | 8 +- .../image/jib/deployment/JibProcessor.java | 14 +++ .../invoker.properties | 1 + .../it/container-build-jib-inherit/pom.xml | 115 ++++++++++++++++++ .../src/main/java/org/acme/Hello.java | 16 +++ .../src/main/resources/application.properties | 8 ++ .../container-build-jib-inherit/verify.groovy | 37 ++++++ 7 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/invoker.properties create mode 100644 integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/pom.xml create mode 100644 integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/java/org/acme/Hello.java create mode 100644 integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/resources/application.properties create mode 100644 integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/verify.groovy diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java index 876665d5938a8..c7ce9311e43e8 100644 --- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java +++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java @@ -54,6 +54,8 @@ public class JibConfig { * If this is set, then it will be used as the entry point of the container image. * There are a few things to be aware of when creating an entry point * * * When this is not set, a proper default entrypoint will be constructed. @@ -77,10 +79,12 @@ public class JibConfig { * If this is set, then it will be used as the entry point of the container image. * There are a few things to be aware of when creating an entry point * * * When this is not set, a proper default entrypoint will be constructed. diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java index 6b0ebe0ddb21f..b2a716c6a2dd8 100644 --- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java +++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java @@ -562,6 +562,8 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag .setEnvironment(getEnvironmentVariables(jibConfig)) .setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels)); + mayInheritEntrypoint(jibContainerBuilder, entrypoint, jibConfig.jvmArguments); + if (jibConfig.useCurrentTimestamp) { jibContainerBuilder.setCreationTime(now); } @@ -596,6 +598,15 @@ public JibContainerBuilder addLayer(JibContainerBuilder jibContainerBuilder, Lis return jibContainerBuilder.addFileEntriesLayer(layerConfigurationBuilder.build()); } + private void mayInheritEntrypoint(JibContainerBuilder jibContainerBuilder, List entrypoint, + List arguments) { + if (entrypoint.size() == 1 && "INHERIT".equals(entrypoint.get(0))) { + jibContainerBuilder + .setEntrypoint((List) null) + .setProgramArguments(arguments); + } + } + private List determineEffectiveJvmArguments(JibConfig jibConfig, Optional appCDSResult) { List effectiveJvmArguments = new ArrayList<>(jibConfig.jvmArguments); jibConfig.jvmAdditionalArguments.ifPresent(effectiveJvmArguments::addAll); @@ -666,6 +677,7 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(String baseJvmIm if (jibConfig.jvmEntrypoint.isPresent()) { jibContainerBuilder.setEntrypoint(jibConfig.jvmEntrypoint.get()); + mayInheritEntrypoint(jibContainerBuilder, jibConfig.jvmEntrypoint.get(), jibConfig.jvmArguments); } return jibContainerBuilder; @@ -702,6 +714,8 @@ private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig .setEnvironment(getEnvironmentVariables(jibConfig)) .setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels)); + mayInheritEntrypoint(jibContainerBuilder, entrypoint, jibConfig.nativeArguments.orElse(null)); + if (jibConfig.useCurrentTimestamp) { jibContainerBuilder.setCreationTime(Instant.now()); } diff --git a/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/invoker.properties b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/invoker.properties new file mode 100644 index 0000000000000..5a1e6fd59b49c --- /dev/null +++ b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/invoker.properties @@ -0,0 +1 @@ +invoker.goals=clean package -Dquarkus.container-image.build=true diff --git a/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/pom.xml b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/pom.xml new file mode 100644 index 0000000000000..e3e1993602bb0 --- /dev/null +++ b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/pom.xml @@ -0,0 +1,115 @@ + + + 4.0.0 + org.acme + container-build-jib-inherit + 0.1-SNAPSHOT + + UTF-8 + 3.0.0-M7 + 11 + UTF-8 + 11 + + + + + io.quarkus + quarkus-bom + @project.version@ + pom + import + + + + + + io.quarkus + quarkus-resteasy-reactive + + + io.quarkus + quarkus-container-image-jib + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + + + + io.quarkus + quarkus-maven-plugin + @project.version@ + + + + build + + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + native + + + native + + + + native + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${native.surefire.skip} + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + + + diff --git a/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/java/org/acme/Hello.java b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/java/org/acme/Hello.java new file mode 100644 index 0000000000000..ad80766a17747 --- /dev/null +++ b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/java/org/acme/Hello.java @@ -0,0 +1,16 @@ +package org.acme; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/hello") +public class Hello { + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + return "hello"; + } +} \ No newline at end of file diff --git a/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/resources/application.properties b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/resources/application.properties new file mode 100644 index 0000000000000..22994823ba77b --- /dev/null +++ b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/src/main/resources/application.properties @@ -0,0 +1,8 @@ +# Configuration file +# key = value +quarkus.package.type=fast-jar +quarkus.jib.jvm-arguments=java,-Djava.util.logging.manager=org.jboss.logmanager.LogManager,-jar,quarkus-run.jar +quarkus.jib.jvm-entrypoint=INHERIT +quarkus.jib.native-arguments=./application +quarkus.jib.native-entrypoint=INHERIT +quarkus.jib.working-directory=/app diff --git a/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/verify.groovy b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/verify.groovy new file mode 100644 index 0000000000000..9997de49e3f8c --- /dev/null +++ b/integration-tests/container-image/maven-invoker-way/src/it/container-build-jib-inherit/verify.groovy @@ -0,0 +1,37 @@ +import io.quarkus.deployment.util.ExecUtil + +import java.util.concurrent.ThreadLocalRandom + +try { + ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'") +} catch (Exception ignored) { + return +} + +String image = "${System.getProperty("user.name")}/container-build-jib-inherit:0.1-SNAPSHOT" +assert ExecUtil.exec("docker", "images", image) + +String containerName = "container-build-jib-inherit-" + ThreadLocalRandom.current().nextInt(10000) +int maxTimesToCheck = 10 +int i = 0 +int hostPort = 12345 +assert ExecUtil.exec("docker", "run", "-d", "-p", "$hostPort:8080", "--name", containerName, image) + +while (true) { + try { + def response = "http://localhost:$hostPort/hello".toURL().text + assert response == "hello" + break + } catch (IOException e) { + try { + Thread.sleep(2000) + } catch (InterruptedException ignored) { + } + if ((i++) >= maxTimesToCheck) { + throw new RuntimeException("Unable to determine if container is running", e) + } + } +} +assert ExecUtil.exec("docker", "stop", containerName) +assert ExecUtil.exec("docker", "rm", containerName) +assert ExecUtil.exec("docker", "rmi", image)