Skip to content

Commit

Permalink
Fix oraclelcoud fn image (#1055)
Browse files Browse the repository at this point in the history
* fix oraclecloud function image

* Do not use Jib for Oracle Cloud functions

* fix oraclecloud functions

* fix oraclecloud functions

* fix oraclecloud functions

* fix oraclecloud functions

* Update micronaut-maven-jib-integration/src/main/java/io/micronaut/maven/jib/JibMicronautExtension.java

Co-authored-by: Álvaro Sánchez-Mariscal <alvaro.sanchez-mariscal@oracle.com>

---------

Co-authored-by: Álvaro Sánchez-Mariscal <alvaro.sanchez-mariscal@oracle.com>
  • Loading branch information
n0tl3ss and alvarosanchez committed Apr 4, 2024
1 parent dede40d commit 21770e1
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY dependency/* /home/app/libs/
COPY graalvm-reachability-metadat[a] /home/app/graalvm-reachability-metadata
COPY nativ[e]/generated /home/app/
COPY *.args /home/app/graalvm-native-image.args
RUN native-image @/home/app/graalvm-native-image.args --report-unsupported-elements-at-runtime -H:Class=com.fnproject.fn.runtime.EntryPoint -H:Name=application -cp "/home/app/libs/*:/home/app/classes/"
RUN native-image @/home/app/graalvm-native-image.args --report-unsupported-elements-at-runtime -H:PageSize=64k -H:Class=com.fnproject.fn.runtime.EntryPoint -H:Name=application -cp "/home/app/libs/*:/home/app/classes/"

FROM fnproject/fn-java-fdk:jre17-latest AS fnfdk

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
FROM fnproject/fn-java-fdk:jre17-latest
ARG BASE_JAVA_IMAGE=eclipse-temurin:21-jre

FROM fnproject/fn-java-fdk:jre17-latest AS fnfdk

FROM ${BASE_JAVA_IMAGE}
COPY --from=fnfdk /function /function
WORKDIR /function
COPY classes /function/app/
COPY dependency/* /function/app/
COPY classes /function/app/classes
COPY dependency/* /function/app/libs/
CMD ["io.micronaut.oraclecloud.function.http.HttpFunction::handleRequest"]
ENTRYPOINT ["java", "-XX:-UsePerfData", "-XX:+UseSerialGC", "-Xshare:auto", "-Djava.awt.headless=true", "-Djava.library.path=/function/runtime/lib", "-cp", "/function/app/classes:/function/app/libs/*:/function/app/resources:/function/runtime/*", "com.fnproject.fn.runtime.EntryPoint"]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
File dockerfile = new File("$basedir/target", "Dockerfile")
File expectedDockerfile = new File(basedir, "Dockerfile")
String expectedDockerfileText = expectedDockerfile.text

String expectedDockerfileText = expectedDockerfile.text.replace("eclipse-temurin:21-jre", "eclipse-temurin:${System.getProperty("java.specification.version")}-jre")

assert dockerfile.text == expectedDockerfileText

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains("fnproject/fn-java-fdk:jre17-latest")
assert log.text.contains("Built image to Docker daemon as alvarosanchez/dockerfile-docker-oracle-function:0.1")
assert log.text.contains("Container entrypoint set to [java, -XX:-UsePerfData, -XX:+UseSerialGC, -Xshare:auto, -Djava.awt.headless=true, -Djava.library.path=/function/runtime/lib, -cp, /function/app/classes:/function/app/libs/*:/function/app/resources:/function/runtime/*, com.fnproject.fn.runtime.EntryPoint]")
assert log.text.contains("Container program arguments set to [io.micronaut.oraclecloud.function.http.HttpFunction::handleRequest]")
assert log.text.contains("Successfully tagged alvarosanchez/dockerfile-docker-oracle-function:0.1")
assert log.text.contains("ENTRYPOINT [\"java\", \"-XX:-UsePerfData\", \"-XX:+UseSerialGC\", \"-Xshare:auto\", \"-Djava.awt.headless=true\", \"-Djava.library.path=/function/runtime/lib\", \"-cp\", \"/function/app/classes:/function/app/libs/*:/function/app/resources:/function/runtime/*\", \"com.fnproject.fn.runtime.EntryPoint\"]")
assert log.text.contains("CMD [\"io.micronaut.oraclecloud.function.http.HttpFunction::handleRequest\"]")
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ public static List<String> buildProjectFnEntrypoint() {

public static String determineProjectFnVersion(String javaVersion) {
int majorVersion = Integer.parseInt(javaVersion.split("\\.")[0]);
if (majorVersion >= 17) {
return "jre17-latest";
if (majorVersion <= 21 && majorVersion > 17) {
return "21-jre";
} else if (majorVersion == 17) {
return "17-jre";
} else {
return LATEST_TAG;
}
Expand All @@ -131,7 +133,6 @@ public static String determineProjectFnVersion(String javaVersion) {
public static String determineBaseImage(String jdkVersion, DockerBuildStrategy buildStrategy) {
int javaVersion = Integer.parseInt(jdkVersion);
return switch (buildStrategy) {
case ORACLE_FUNCTION -> "fnproject/fn-java-fdk:" + determineProjectFnVersion(System.getProperty("java.version"));
case LAMBDA -> "public.ecr.aws/lambda/java:" + javaVersion;
default -> javaVersion == 17 ? DEFAULT_JAVA17_BASE_IMAGE : DEFAULT_JAVA21_BASE_IMAGE;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class JibMicronautExtensionTest {

@ParameterizedTest
@CsvSource({
"17.0.1, jre17-latest",
"17.0.4.1, jre17-latest",
"19.0.1, jre17-latest"
"17.0.1, 17-jre",
"17.0.4.1, 17-jre",
"19.0.1, 21-jre",
"21.0.1, 21-jre"
})
void testDetermineJavaVersion(String javaVersion, String expectedFnVersion) {
String fnVersion = JibMicronautExtension.determineProjectFnVersion(javaVersion);
Expand Down Expand Up @@ -81,11 +82,11 @@ void testRemapLayer() {
@ParameterizedTest
@CsvSource({
"DEFAULT, 17, eclipse-temurin:17-jre",
"ORACLE_FUNCTION, 17, fnproject/fn-java-fdk:jre17-latest",
"ORACLE_FUNCTION, 17, eclipse-temurin:17-jre",
"LAMBDA, 17, public.ecr.aws/lambda/java:17",

"DEFAULT, 21, eclipse-temurin:21-jre",
"ORACLE_FUNCTION, 21, fnproject/fn-java-fdk:jre17-latest",
"ORACLE_FUNCTION, 21, eclipse-temurin:21-jre",
"LAMBDA, 21, public.ecr.aws/lambda/java:21"
})
void testDetermineBaseImage(String dockerBuildStrategy, String jdkVersion, String expectedImage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.github.dockerjava.api.command.BuildImageCmd;
import com.google.cloud.tools.jib.plugins.common.PropertyNames;
import io.micronaut.maven.core.DockerBuildStrategy;
import io.micronaut.maven.core.MicronautRuntime;
import io.micronaut.maven.services.ApplicationConfigurationService;
import io.micronaut.maven.services.DockerService;
import io.micronaut.maven.jib.JibConfigurationService;
Expand All @@ -34,6 +36,8 @@
import java.nio.file.LinkOption;
import java.nio.file.StandardCopyOption;

import static io.micronaut.maven.DockerfileMojo.DOCKERFILE_ORACLE_CLOUD;

/**
* <p>Allows using a provided Dockerfile.</p>
* <p><strong>WARNING</strong>: this goal is not intended to be executed directly. Instead, specify the packaging type
Expand All @@ -60,30 +64,56 @@ public DockerMojo(MavenProject mavenProject, JibConfigurationService jibConfigur

@Override
public void execute() throws MojoExecutionException {
File dockerfile = new File(mavenProject.getBasedir(), DockerfileMojo.DOCKERFILE);
if (dockerfile.exists()) {
try {
getLog().info("Using provided Dockerfile: " + dockerfile.getAbsolutePath());
mavenProject.getProperties().put(PropertyNames.SKIP, "true");
var providedDockerfile = new File(mavenProject.getBasedir(), DockerfileMojo.DOCKERFILE);
if (shouldBuildWithDockerfile(providedDockerfile)) {
var dockerfile = determineDockerfile(providedDockerfile);
buildDockerfile(dockerfile);
} else if (jibConfigurationService.getFromImage().isEmpty()) {
mavenProject.getProperties().setProperty(PropertyNames.FROM_IMAGE, getBaseImage());
}
}

copyDependencies();
private File determineDockerfile(File providedDockerfile) throws MojoExecutionException {
if (providedDockerfile.exists()) {
return providedDockerfile;
} else {
try {
return dockerService.loadDockerfileAsResource(DOCKERFILE_ORACLE_CLOUD);
} catch (IOException e) {
throw new MojoExecutionException("Error loading Dockerfile", e);
}
}
}

String targetDir = mavenProject.getBuild().getDirectory();
File targetDockerfile = new File(targetDir, dockerfile.getName());
Files.copy(dockerfile.toPath(), targetDockerfile.toPath(), LinkOption.NOFOLLOW_LINKS,
StandardCopyOption.REPLACE_EXISTING);
private boolean shouldBuildWithDockerfile(File providedDockerfile) {
var runtime = MicronautRuntime.valueOf(micronautRuntime.toUpperCase());
return providedDockerfile.exists() || runtime.getBuildStrategy() == DockerBuildStrategy.ORACLE_FUNCTION;
}

BuildImageCmd buildImageCmd = dockerService.buildImageCmd()
.withDockerfile(targetDockerfile)
.withTags(getTags())
.withBaseDirectory(new File(targetDir));
getNetworkMode().ifPresent(buildImageCmd::withNetworkMode);
dockerService.buildImage(buildImageCmd);
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
private void buildDockerfile(File dockerfile) throws MojoExecutionException {
try {
var runtime = MicronautRuntime.valueOf(micronautRuntime.toUpperCase());
if (runtime.getBuildStrategy() == DockerBuildStrategy.ORACLE_FUNCTION) {
DockerfileMojo.processOracleFunctionDockerfile(dockerfile);
}
} else if (jibConfigurationService.getFromImage().isEmpty()) {
mavenProject.getProperties().setProperty(PropertyNames.FROM_IMAGE, getBaseImage());
getLog().info("Using Dockerfile: " + dockerfile.getAbsolutePath());
mavenProject.getProperties().put(PropertyNames.SKIP, "true");

copyDependencies();

String targetDir = mavenProject.getBuild().getDirectory();
File targetDockerfile = new File(targetDir, dockerfile.getName());
Files.copy(dockerfile.toPath(), targetDockerfile.toPath(), LinkOption.NOFOLLOW_LINKS,
StandardCopyOption.REPLACE_EXISTING);

BuildImageCmd buildImageCmd = dockerService.buildImageCmd()
.withDockerfile(targetDockerfile)
.withTags(getTags())
.withBaseDirectory(new File(targetDir));
getNetworkMode().ifPresent(buildImageCmd::withNetworkMode);
dockerService.buildImage(buildImageCmd);
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class DockerNativeMojo extends AbstractDockerMojo {
public static final String MICRONAUT_VERSION = "micronaut.version";
public static final String ARGS_FILE_PROPERTY_NAME = "graalvm.native-image.args-file";
static final int AWS_LAMBDA_MAX_ALLOWED_VERSION = 21;
static final int ORACLE_FUNCTION_MAX_ALLOWED_VERSION = 17;
static final int ORACLE_FUNCTION_MAX_ALLOWED_VERSION = 21;
static final int MAX_ALLOWED_VERSION = 21;
private MicronautRuntime runtime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private Optional<File> buildCracDockerfile(MicronautRuntime runtime) throws IOEx
return Optional.ofNullable(dockerfile);
}

private void processOracleFunctionDockerfile(File dockerfile) throws IOException {
static void processOracleFunctionDockerfile(File dockerfile) throws IOException {
if (dockerfile != null) {
List<String> allLines = Files.readAllLines(dockerfile.toPath());
String projectFnVersion = JibMicronautExtension.determineProjectFnVersion(System.getProperty("java.version"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ COPY dependency/* /home/app/libs/
COPY graalvm-reachability-metadat[a] /home/app/graalvm-reachability-metadata
COPY nativ[e]/generated /home/app/
COPY *.args /home/app/graalvm-native-image.args
RUN native-image @/home/app/graalvm-native-image.args --report-unsupported-elements-at-runtime -H:Class=com.fnproject.fn.runtime.EntryPoint -H:Name=application -cp "/home/app/libs/*:/home/app/classes/"
RUN native-image @/home/app/graalvm-native-image.args --report-unsupported-elements-at-runtime -H:PageSize=64k -H:Class=com.fnproject.fn.runtime.EntryPoint -H:Name=application -cp "/home/app/libs/*:/home/app/classes/"

FROM fnproject/fn-java-fdk:jre17-latest AS fnfdk

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
FROM fnproject/fn-java-fdk:
ARG BASE_JAVA_IMAGE=eclipse-temurin:

FROM fnproject/fn-java-fdk:jre17-latest AS fnfdk

FROM ${BASE_JAVA_IMAGE}
COPY --from=fnfdk /function /function
WORKDIR /function
COPY classes /function/app/
COPY dependency/* /function/app/
COPY classes /function/app/classes
COPY dependency/* /function/app/libs/
CMD ["io.micronaut.oraclecloud.function.http.HttpFunction::handleRequest"]

0 comments on commit 21770e1

Please sign in to comment.