Skip to content

Commit

Permalink
Add OS configuration for CRaC JDK (#926)
Browse files Browse the repository at this point in the history
The Azul API has started to differentiate between linux-musl and linux-glibc.

Without us passing the option to use linux-glibc we get 2 JDKs returned, and the musl one doesn't work for building images on OSX with DockerDesktop.

This PR allows configuration of the OS when making the request to the Azul API
  • Loading branch information
timyates authored Jan 30, 2024
1 parent 433e193 commit 880f493
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
import java.io.IOException;
Expand Down Expand Up @@ -61,6 +62,9 @@ public abstract class CRaCCheckpointDockerfile extends Dockerfile {
@Input
public abstract Property<String> getArch();

@Input
public abstract Property<String> getOs();

@Input
public abstract Property<JavaLanguageVersion> getJavaVersion();

Expand Down Expand Up @@ -180,7 +184,7 @@ static void setupResources(CRaCCheckpointDockerfile task) {

String errorMessage = "No CRaC OpenJDK found for Java version " + javaVersion + " and architecture " + arch;

String url = "https://api.azul.com/metadata/v1/zulu/packages/?java_version=" + javaVersion + "&arch=" + arch + "&crac_supported=true&java_package_type=jdk&latest=true&release_status=ga&certifications=tck&page=1&page_size=100";
String url = getUrl(javaVersion, task.getOs().get(), arch);
task.runCommand("release_id=$(curl -s \"" + url + "\" -H \"accept: application/json\" | jq -r '.[0] | .package_uuid') \\\n" +
" && if [ \"$release_id\" = \"null\" ]; then \\\n" +
" echo \"" + errorMessage + "\"; \\\n" +
Expand Down Expand Up @@ -209,4 +213,20 @@ static void setupResources(CRaCCheckpointDockerfile task) {
task.copyFile("scripts/checkpoint.sh", workDir + "/checkpoint.sh");
task.copyFile("scripts/warmup.sh", workDir + "/warmup.sh");
}

@NotNull
static String getUrl(String javaVersion, String os, String arch) {
return """
https://api.azul.com/metadata/v1/zulu/packages/\
?java_version=%s\
&os=%s\
&arch=%s\
&crac_supported=true\
&java_package_type=jdk\
&latest=true\
&release_status=ga\
&certifications=tck\
&page=1\
&page_size=100""".formatted(javaVersion, os, arch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public interface CRaCConfiguration {
*/
Property<String> getArch();

/**
* Filters the jdk result by the operating system the package is targeting. Defaults to {@value MicronautCRaCPlugin#DEFAULT_OS}.
* @return the operating system
*/
Property<String> getOs();

/**
* The java version to use for building the CRaC enabled images. Currently only '17' is supported.
* @return the java version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class MicronautCRaCPlugin implements Plugin<Project> {
public static final String CRAC_DEFAULT_BASE_IMAGE_PLATFORM = "linux/amd64";
public static final String ARM_ARCH = "aarch64";
public static final String X86_64_ARCH = "amd64";
public static final String DEFAULT_OS = "linux-glibc";
public static final String CRAC_DEFAULT_READINESS_COMMAND = "curl --output /dev/null --silent --head http://localhost:8080";
private static final String CRAC_TASK_GROUP = "CRaC";
public static final String BUILD_DOCKER_DIRECTORY = "docker/";
Expand Down Expand Up @@ -78,6 +79,9 @@ private CRaCConfiguration createCRaCConfiguration(Project project) {
String osArch = System.getProperty("os.arch");
crac.getArch().convention(ARM_ARCH.equals(osArch) ? ARM_ARCH : X86_64_ARCH);

// Default to linux-glibc
crac.getOs().convention(DEFAULT_OS);

// Default to Java 17
crac.getJavaVersion().convention(JavaLanguageVersion.of(17));

Expand Down Expand Up @@ -150,6 +154,7 @@ private CheckpointTasksOfNote configureCheckpointDockerBuild(Project project,
task.getBaseImage().set(configuration.getBaseImage());
task.getPlatform().set(configuration.getPlatform());
task.getArch().set(configuration.getArch());
task.getOs().set(configuration.getOs());
task.getJavaVersion().set(configuration.getJavaVersion());
task.setupDockerfileInstructions();
task.getLayers().convention(buildLayersTask.flatMap(BuildLayersTask::getLayers));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micronaut.gradle.crac

import groovy.json.JsonSlurper
import spock.lang.IgnoreIf

@IgnoreIf({ os.windows })
Expand Down Expand Up @@ -133,27 +134,38 @@ class CracCustomizationSpec extends BaseCracGradleBuildSpec {

when:
def result = build('dockerfileCrac', 'checkpointDockerfile', '-s')

then:
result.output.contains("BUILD SUCCESSFUL")
fileTextContents("build/docker/main/Dockerfile.CRaCCheckpoint").contains("https://api.azul.com/metadata/v1/zulu/packages/?java_version=$javaVersion&arch=$expectedArch&crac_supported=true&java_package_type=jdk&latest=true&release_status=ga&certifications=tck&page=1&page_size=100")
fileTextContents("build/docker/main/Dockerfile.CRaCCheckpoint").contains("https://api.azul.com/metadata/v1/zulu/packages/?java_version=$javaVersion&os=$MicronautCRaCPlugin.DEFAULT_OS&arch=$expectedArch&crac_supported=true&java_package_type=jdk&latest=true&release_status=ga&certifications=tck&page=1&page_size=100")
}

void "default CRaC URL returns a single JDK"() {
when:
def expectedArch = System.properties['os.arch'] == MicronautCRaCPlugin.ARM_ARCH ? MicronautCRaCPlugin.ARM_ARCH : MicronautCRaCPlugin.X86_64_ARCH
def json = new URL(CRaCCheckpointDockerfile.getUrl("17", MicronautCRaCPlugin.DEFAULT_OS, expectedArch)).text
def result = new JsonSlurper().parseText(json)

then:
result.size() == 1
}

void "Azul CRaC JDK and arch can be changed"() {
void "Azul CRaC JDK os and arch can be changed"() {
given:
def javaVersion = "21"
def configuredOs = "configured-os"
settingsFile << "rootProject.name = 'hello-world'"
buildFile << getBuildFileBlockWithMicronautConfig(getMicronautConfigBlock("""crac {
javaVersion.set(JavaLanguageVersion.of($javaVersion))
arch.set('$MicronautCRaCPlugin.ARM_ARCH')
os.set('$configuredOs')
}"""))

when:
def result = build('dockerfileCrac', 'checkpointDockerfile', '-s')

then:
result.output.contains("BUILD SUCCESSFUL")
fileTextContents("build/docker/main/Dockerfile.CRaCCheckpoint").contains("https://api.azul.com/metadata/v1/zulu/packages/?java_version=$javaVersion&arch=$MicronautCRaCPlugin.ARM_ARCH&crac_supported=true&java_package_type=jdk&latest=true&release_status=ga&certifications=tck&page=1&page_size=100")
fileTextContents("build/docker/main/Dockerfile.CRaCCheckpoint").contains("https://api.azul.com/metadata/v1/zulu/packages/?java_version=$javaVersion&os=$configuredOs&arch=$MicronautCRaCPlugin.ARM_ARCH&crac_supported=true&java_package_type=jdk&latest=true&release_status=ga&certifications=tck&page=1&page_size=100")
}

void "Weird java versions cause an error"() {
Expand Down
8 changes: 7 additions & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ Micronaut CRaC is a module which adds support for https://openjdk.org/projects/c

It is capable of generating a docker image containing a CRaC enabled JDK and a pre-warmed, checkpointed application via a new task `dockerBuildCrac`.

IMPORTANT: Currently, CRaC support has only been tested on Ubuntu 18.04, 20.04 and 22.04. It also requires an x86 machine architecture.
IMPORTANT: Currently, CRaC support has only been tested on Ubuntu 18.04, 20.04 and 22.04.

When executed, this task will:

Expand Down Expand Up @@ -1774,6 +1774,9 @@ micronaut {
// (currently only 'aarch64' or 'amd64' are supported)
arch = "aarch64"
// The OS of the Azul CRaC JDK to use. Defaults to linux-glibc for the default base image.
os = "linux-glibc"
// The version of the Azul CRaC JDK to use in the image (currently only 17 is supported)
javaVersion = JavaLanguageVersion.of(17)
Expand Down Expand Up @@ -1809,6 +1812,9 @@ micronaut {
// (currently only 'aarch64' or 'amd64' are supported)
arch.set("aarch64")
// The OS of the Azul CRaC JDK to use. Defaults to linux-glibc for the default base image.
os.set("linux-glibc")
// The version of the Azul CRaC JDK to use in the image (currently only 17 is supported)
javaVersion.set(JavaLanguageVersion.of(17))
Expand Down

0 comments on commit 880f493

Please sign in to comment.