Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom entrypoints in Jib extension #10071

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -90,7 +90,8 @@ public void buildFromJar(ContainerImageConfig containerImageConfig, JibConfig ji
JibContainerBuilder jibContainerBuilder;
String packageType = packageConfig.type;
if (packageType.equalsIgnoreCase(PackageConfig.LEGACY)
|| packageType.equalsIgnoreCase(PackageConfig.JAR)) {
|| packageType.equalsIgnoreCase(PackageConfig.JAR)
|| packageType.equalsIgnoreCase(PackageConfig.UBER_JAR)) {
jibContainerBuilder = createContainerBuilderFromLegacyJar(jibConfig,
sourceJar, outputTarget, mainClass, containerImageLabels);
} else if (packageType.equalsIgnoreCase(PackageConfig.FAST_JAR)) {
Expand Down Expand Up @@ -254,11 +255,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 +300,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 +318,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 +337,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