Skip to content

Commit

Permalink
Support custom entrypoints in Jib extension
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and johnaohara committed Jun 29, 2020
1 parent c844b50 commit 24c6e3c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@ public class JibConfig {
@ConfigItem(defaultValue = "-Dquarkus.http.host=0.0.0.0")
public List<String> nativeArguments;

/**
* 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>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, all necessary application jars are added to
* the {@code /work} directory and that the same
* directory is also used as the working directory. When {@code legacy} or {@code uber-jar} are used, the application 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>
* </ul>
*
* When this is not set, a proper default entrypoint will be constructed.
*
* As a final note, a very useful tool for inspecting container image layers that can greatly aid
* when debugging problems with endpoints is <a href="https://github.com/wagoodman/dive">dive</a>
*/
@ConfigItem
public Optional<List<String>> jvmEntrypoint;

/**
* 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>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>
* </ul>
*
* When this is not set, a proper default entrypoint will be constructed.
*
* As a final note, a very useful tool for inspecting container image layers that can greatly aid
* when debugging problems with endpoints is <a href="https://github.com/wagoodman/dive">dive</a>
*/
@ConfigItem
public Optional<List<String>> nativeEntrypoint;

/**
* Environment variables to add to the container image
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,17 @@ private JibContainerBuilder createContainerBuilderFromFastJar(JibConfig jibConfi
Path componentsPath = sourceJarBuildItem.getPath().getParent().getParent();

AbsoluteUnixPath workDirInContainer = AbsoluteUnixPath.get("/work");
List<String> entrypoint = new ArrayList<>(3 + jibConfig.jvmArguments.size());
entrypoint.add("java");
entrypoint.addAll(jibConfig.jvmArguments);
entrypoint.add("-jar");
entrypoint.add("quarkus-run.jar");

List<String> entrypoint;
if (jibConfig.jvmEntrypoint.isPresent()) {
entrypoint = jibConfig.jvmEntrypoint.get();
} else {
entrypoint = new ArrayList<>(3 + jibConfig.jvmArguments.size());
entrypoint.add("java");
entrypoint.addAll(jibConfig.jvmArguments);
entrypoint.add("-jar");
entrypoint.add(JarResultBuildStep.QUARKUS_RUN_JAR);
}

try {
return Jib.from(toRegistryImage(ImageReference.parse(jibConfig.baseJvmImage), jibConfig.baseRegistryUsername,
Expand Down Expand Up @@ -293,9 +299,15 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibCon
.from(toRegistryImage(ImageReference.parse(jibConfig.baseJvmImage), jibConfig.baseRegistryUsername,
jibConfig.baseRegistryPassword))
.addResources(classesDir, IS_CLASS_PREDICATE.negate())
.addClasses(classesDir, IS_CLASS_PREDICATE)
.addJvmFlags(jibConfig.jvmArguments)
.setMainClass(mainClassBuildItem.getClassName());
.addClasses(classesDir, IS_CLASS_PREDICATE);

// when there is no custom entry point, we just set everything up for a regular java run
if (!jibConfig.jvmEntrypoint.isPresent()) {
javaContainerBuilder
.addJvmFlags(jibConfig.jvmArguments)
.setMainClass(mainClassBuildItem.getClassName());
}

if (sourceJarBuildItem.getLibraryDir() != null) {
javaContainerBuilder
.addDependencies(
Expand All @@ -305,10 +317,16 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibCon
.collect(Collectors.toList()));
}

return javaContainerBuilder.toContainerBuilder()
JibContainerBuilder jibContainerBuilder = javaContainerBuilder.toContainerBuilder()
.setEnvironment(jibConfig.environmentVariables)
.setLabels(allLabels(jibConfig, containerImageLabels))
.setCreationTime(Instant.now());

if (jibConfig.jvmEntrypoint.isPresent()) {
jibContainerBuilder.setEntrypoint(jibConfig.jvmEntrypoint.get());
}

return jibContainerBuilder;
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InvalidImageReferenceException e) {
Expand All @@ -318,9 +336,15 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibCon

private JibContainerBuilder createContainerBuilderFromNative(ContainerImageConfig containerImageConfig, JibConfig jibConfig,
NativeImageBuildItem nativeImageBuildItem, List<ContainerImageLabelBuildItem> containerImageLabels) {
List<String> entrypoint = new ArrayList<>(jibConfig.nativeArguments.size() + 1);
entrypoint.add("./" + BINARY_NAME_IN_CONTAINER);
entrypoint.addAll(jibConfig.nativeArguments);

List<String> entrypoint;
if (jibConfig.nativeEntrypoint.isPresent()) {
entrypoint = jibConfig.nativeEntrypoint.get();
} else {
entrypoint = new ArrayList<>(jibConfig.nativeArguments.size() + 1);
entrypoint.add("./" + BINARY_NAME_IN_CONTAINER);
entrypoint.addAll(jibConfig.nativeArguments);
}
try {
AbsoluteUnixPath workDirInContainer = AbsoluteUnixPath.get("/work");
return Jib
Expand Down

0 comments on commit 24c6e3c

Please sign in to comment.