Skip to content

Commit

Permalink
Add defaut base image for Java 21
Browse files Browse the repository at this point in the history
  • Loading branch information
rquinio committed Nov 19, 2023
1 parent 440bd48 commit cbdce67
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/google-cloud-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Any Java container can be used for building, not only the `gcr.io/cloud-builders

```yaml
steps:
- name: 'docker.io/library/eclipse-temurin:17'
- name: 'docker.io/library/eclipse-temurin:21'
entrypoint: './gradlew'
args: ['--console=plain', '--no-daemon', ':server:jib', '-Djib.to.image=gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA']
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ private static String getDefaultBaseImage(ArtifactProcessor processor) {
if (processor.getJavaVersion() <= 11) {
return "eclipse-temurin:11-jre";
}
return "eclipse-temurin:17-jre";
if (processor.getJavaVersion() <= 17) {
return "eclipse-temurin:17-jre";
}
return "eclipse-temurin:21-jre";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class JarFilesTest {
"11, eclipse-temurin:11-jre",
"13, eclipse-temurin:17-jre",
"17, eclipse-temurin:17-jre",
"21, eclipse-temurin:21-jre",
})
public void testToJibContainer_defaultBaseImage(int javaVersion, String expectedBaseImage)
throws IOException, InvalidImageReferenceException {
Expand Down
2 changes: 1 addition & 1 deletion jib-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Field | Type | Default | Description

Property | Type | Default | Description
--- | --- | --- | ---
`image` | `String` | `eclipse-temurin:{8,11,17}-jre` (or `jetty` for WAR) | The image reference for the base image. The source type can be specified using a [special type prefix](#setting-the-base-image).
`image` | `String` | `eclipse-temurin:{8,11,17,21}-jre` (or `jetty` for WAR) | The image reference for the base image. The source type can be specified using a [special type prefix](#setting-the-base-image).
`auth` | [`auth`](#auth-closure) | *None* | Specifies credentials directly (alternative to `credHelper`).
`credHelper` | `String` | *None* | Specifies a credential helper that can authenticate pulling the base image. This parameter can either be configured as an absolute path to the credential helper executable or as a credential helper suffix (following `docker-credential-`).
`platforms` | [`platforms`](#platforms-closure) | See [`platforms`](#platforms-closure) | Configures platforms of base images to select from a manifest list.
Expand Down
2 changes: 1 addition & 1 deletion jib-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Field | Type | Default | Description

Property | Type | Default | Description
--- | --- | --- | ---
`image` | string | `eclipse-temurin:{8,11,17}-jre` (or `jetty` for WAR) | The image reference for the base image. The source type can be specified using a [special type prefix](#setting-the-base-image).
`image` | string | `eclipse-temurin:{8,11,17,21}-jre` (or `jetty` for WAR) | The image reference for the base image. The source type can be specified using a [special type prefix](#setting-the-base-image).
`auth` | [`auth`](#auth-object) | *None* | Specifies credentials directly (alternative to `credHelper`).
`credHelper` | string | *None* | Specifies a credential helper that can authenticate pulling the base image. This parameter can either be configured as an absolute path to the credential helper executable or as a credential helper suffix (following `docker-credential-`).
`platforms` | list | See [`platform`](#platform-object) | Configures platforms of base images to select from a manifest list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ static JavaContainerBuilder getJavaContainerBuilderWithBaseImage(
if (isKnownJava17Image(prefixRemoved) && javaVersion > 17) {
throw new IncompatibleBaseImageJavaVersionException(17, javaVersion);
}
if (isKnownJava21Image(prefixRemoved) && javaVersion > 21) {
throw new IncompatibleBaseImageJavaVersionException(21, javaVersion);
}

ImageReference baseImageReference = ImageReference.parse(prefixRemoved);
if (baseImageConfig.startsWith(Jib.DOCKER_DAEMON_IMAGE_PREFIX)) {
Expand Down Expand Up @@ -772,8 +775,10 @@ static String getDefaultBaseImage(ProjectProperties projectProperties)
return "eclipse-temurin:11-jre";
} else if (javaVersion <= 17) {
return "eclipse-temurin:17-jre";
} else if (javaVersion <= 21) {
return "eclipse-temurin:21-jre";
}
throw new IncompatibleBaseImageJavaVersionException(17, javaVersion);
throw new IncompatibleBaseImageJavaVersionException(21, javaVersion);
}

/**
Expand Down Expand Up @@ -1097,4 +1102,14 @@ private static boolean isKnownJava11Image(String imageReference) {
private static boolean isKnownJava17Image(String imageReference) {
return imageReference.startsWith("eclipse-temurin:17");
}

/**
* Checks if the given image is a known Java 21 image. May return false negative.
*
* @param imageReference the image reference
* @return {@code true} if the image is a known Java 21 image
*/
private static boolean isKnownJava21Image(String imageReference) {
return imageReference.startsWith("eclipse-temurin:21");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ public void testGetDefaultBaseImage_warProject()
"9, eclipse-temurin:11-jre",
"11, eclipse-temurin:11-jre",
"13, eclipse-temurin:17-jre",
"17, eclipse-temurin:17-jre"
"17, eclipse-temurin:17-jre",
"21, eclipse-temurin:21-jre"
})
public void testGetDefaultBaseImage_defaultJavaBaseImage(
int javaVersion, String expectedBaseImage) throws IncompatibleBaseImageJavaVersionException {
Expand All @@ -913,16 +914,16 @@ public void testGetDefaultBaseImage_defaultJavaBaseImage(
}

@Test
public void testGetDefaultBaseImage_projectHigherThanJava17() {
when(projectProperties.getMajorJavaVersion()).thenReturn(20);
public void testGetDefaultBaseImage_projectHigherThanJava21() {
when(projectProperties.getMajorJavaVersion()).thenReturn(22);

IncompatibleBaseImageJavaVersionException exception =
assertThrows(
IncompatibleBaseImageJavaVersionException.class,
() -> PluginConfigurationProcessor.getDefaultBaseImage(projectProperties));

assertThat(exception.getBaseImageMajorJavaVersion()).isEqualTo(17);
assertThat(exception.getProjectMajorJavaVersion()).isEqualTo(20);
assertThat(exception.getBaseImageMajorJavaVersion()).isEqualTo(21);
assertThat(exception.getProjectMajorJavaVersion()).isEqualTo(22);
}

@Test
Expand Down Expand Up @@ -980,7 +981,9 @@ public void testGetJavaContainerBuilderWithBaseImage_registryWithPrefix()
"eclipse-temurin:11, 11, 15",
"eclipse-temurin:11-jre, 11, 15",
"eclipse-temurin:17, 17, 19",
"eclipse-temurin:17-jre, 17, 19"
"eclipse-temurin:17-jre, 17, 19",
"eclipse-temurin:21, 21, 22",
"eclipse-temurin:21-jre, 21, 22"
})
public void testGetJavaContainerBuilderWithBaseImage_incompatibleJavaBaseImage(
String baseImage, int baseImageJavaVersion, int appJavaVersion) {
Expand Down Expand Up @@ -1010,17 +1013,17 @@ public void testGetJavaContainerBuilderWithBaseImage_java12BaseImage()
}

@Test
public void testGetJavaContainerBuilderWithBaseImage_java19NoBaseImage() {
when(projectProperties.getMajorJavaVersion()).thenReturn(19);
public void testGetJavaContainerBuilderWithBaseImage_java22NoBaseImage() {
when(projectProperties.getMajorJavaVersion()).thenReturn(22);
when(rawConfiguration.getFromImage()).thenReturn(Optional.empty());
IncompatibleBaseImageJavaVersionException exception =
assertThrows(
IncompatibleBaseImageJavaVersionException.class,
() ->
PluginConfigurationProcessor.getJavaContainerBuilderWithBaseImage(
rawConfiguration, projectProperties, inferredAuthProvider));
assertThat(exception.getBaseImageMajorJavaVersion()).isEqualTo(17);
assertThat(exception.getProjectMajorJavaVersion()).isEqualTo(19);
assertThat(exception.getBaseImageMajorJavaVersion()).isEqualTo(21);
assertThat(exception.getProjectMajorJavaVersion()).isEqualTo(22);
}

@Test
Expand Down

0 comments on commit cbdce67

Please sign in to comment.