Skip to content

Commit

Permalink
Merge pull request #28097 from Dieken/jib-entrypoint-inherit
Browse files Browse the repository at this point in the history
Support "INHERIT" for quarkus.jib.jvm-entrypoint and quarkus.jib.native-entrypoint
  • Loading branch information
geoand authored Sep 22, 2022
2 parents 22c6aca + 74290e4 commit d45c048
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <ul>
* <li>Entrypoint "INHERIT" means to inherit entrypoint from base image, {@code jvmArguments} field is used for
* arguments</li>
* <li>A valid entrypoint is jar package specific (see {@code quarkus.package.type})</li>
* <li>A valid entrypoint depends on the location of both the launching scripts and the application jar file. To that
* end it's helpful to remember that when {@code fast-jar} packaging is used (the default), all necessary application
Expand All @@ -62,7 +64,7 @@ public class JibConfig {
* jars
* are unpacked under the {@code /app} directory
* and that directory is used as the working directory.</li>
* <li>Even if the {@code jvmArguments} field is set, it is ignored completely</li>
* <li>Even if the {@code jvmArguments} field is set, it is ignored completely unless entrypoint is "INHERIT"</li>
* </ul>
*
* When this is not set, a proper default entrypoint will be constructed.
Expand All @@ -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
* <ul>
* <li>Entrypoint "INHERIT" means to inherit entrypoint from base image, {@code nativeArguments} field is used for
* arguments</li>
* <li>A valid entrypoint depends on the location of both the launching scripts and the native binary file. To that end
* it's helpful to remember that the native application is added to the {@code /work} directory and that and the same
* directory is also used as the working directory</li>
* <li>Even if the {@code nativeArguments} field is set, it is ignored completely</li>
* <li>Even if the {@code nativeArguments} field is set, it is ignored completely unless entrypoint is "INHERIT"</li>
* </ul>
*
* When this is not set, a proper default entrypoint will be constructed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -596,6 +598,15 @@ public JibContainerBuilder addLayer(JibContainerBuilder jibContainerBuilder, Lis
return jibContainerBuilder.addFileEntriesLayer(layerConfigurationBuilder.build());
}

private void mayInheritEntrypoint(JibContainerBuilder jibContainerBuilder, List<String> entrypoint,
List<String> arguments) {
if (entrypoint.size() == 1 && "INHERIT".equals(entrypoint.get(0))) {
jibContainerBuilder
.setEntrypoint((List<String>) null)
.setProgramArguments(arguments);
}
}

private List<String> determineEffectiveJvmArguments(JibConfig jibConfig, Optional<AppCDSResultBuildItem> appCDSResult) {
List<String> effectiveJvmArguments = new ArrayList<>(jibConfig.jvmArguments);
jibConfig.jvmAdditionalArguments.ifPresent(effectiveJvmArguments::addAll);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean package -Dquarkus.container-image.build=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>container-build-jib-inherit</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bom</artifactId>
<version>@project.version@</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-jib</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d45c048

Please sign in to comment.