Skip to content

Commit 8dc607c

Browse files
Expose jib.from.platforms as property. (#3526)
* Expose jib.from.platforms as property. * Expose jib.from.platforms as property. * code review suggestions * Adding tests * add jib.from.platforms to the gradle plugin * Fix PlatformParameters comparsion / refactor test code * Fix null comparison Co-authored-by: Chanseok Oh <[email protected]>
1 parent 5a5fa5e commit 8dc607c

File tree

10 files changed

+261
-160
lines changed

10 files changed

+261
-160
lines changed

jib-gradle-plugin/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
55

66
### Added
77

8+
- [`jib.from.platforms`](https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin#from-closure) parameter for multi-architecture image building can now be configured through Maven and system properties (for example, `-Djib.from.platforms=linux/amd64,linux/arm64` on the command-line). ([#2742](https://github.com/GoogleContainerTools/jib/pull/2742))
9+
810
### Changed
911

1012
- Changed the default base image from the `adoptopenjdk` images to the [`eclipse-temurin`](https://hub.docker.com/_/eclipse-temurin) (for Java 8 and 11) and [`azul/zulu-openjdk`](https://hub.docker.com/r/azul/zulu-openjdk) (for Java 17) images on Docker Hub. Note that Temurin (by Adoptium) is the new name of AdoptOpenJDK. ([#3491](https://github.com/GoogleContainerTools/jib/pull/3491))

jib-gradle-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Property | Type | Default | Description
213213
`image` | `String` | `adoptopenjdk:{8,11}-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).
214214
`auth` | [`auth`](#auth-closure) | *None* | Specifies credentials directly (alternative to `credHelper`).
215215
`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-`).
216-
`platforms` | [`platforms`](#platforms-closure) | See [`platforms`](#platforms-closure) | _Incubating feature_: Configures platforms of base images to select from a manifest list.
216+
`platforms` | [`platforms`](#platforms-closure) | See [`platforms`](#platforms-closure) | Configures platforms of base images to select from a manifest list.
217217

218218
<a name="to-closure"></a>`to` is a closure with the following properties:
219219

jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BaseImageParameters.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package com.google.cloud.tools.jib.gradle;
1818

19+
import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator;
1920
import com.google.cloud.tools.jib.plugins.common.PropertyNames;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
2023
import javax.annotation.Nullable;
2124
import javax.inject.Inject;
2225
import org.gradle.api.Action;
@@ -53,6 +56,16 @@ public BaseImageParameters(ObjectFactory objectFactory) {
5356
@Nested
5457
@Optional
5558
public ListProperty<PlatformParameters> getPlatforms() {
59+
String property = System.getProperty(PropertyNames.FROM_PLATFORMS);
60+
if (property != null) {
61+
List<PlatformParameters> parsed =
62+
ConfigurationPropertyValidator.parseListProperty(property).stream()
63+
.map(PlatformParameters::of)
64+
.collect(Collectors.toList());
65+
if (!parsed.equals(platforms.get())) {
66+
platforms.set(parsed);
67+
}
68+
}
5669
return platforms;
5770
}
5871

jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParameters.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,28 @@
1717
package com.google.cloud.tools.jib.gradle;
1818

1919
import com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration;
20+
import java.util.Objects;
2021
import java.util.Optional;
22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
2124
import javax.annotation.Nullable;
2225
import org.gradle.api.tasks.Input;
2326
import org.gradle.api.tasks.Internal;
2427

2528
/** Configuration of a platform. */
2629
public class PlatformParameters implements PlatformConfiguration {
30+
31+
static PlatformParameters of(String osArchitecture) {
32+
Matcher matcher = Pattern.compile("([^/ ]+)/([^/ ]+)").matcher(osArchitecture);
33+
if (!matcher.matches()) {
34+
throw new IllegalArgumentException("Platform must be of form os/architecture.");
35+
}
36+
PlatformParameters platformParameters = new PlatformParameters();
37+
platformParameters.os = matcher.group(1);
38+
platformParameters.architecture = matcher.group(2);
39+
return platformParameters;
40+
}
41+
2742
@Nullable private String os;
2843
@Nullable private String architecture;
2944

@@ -58,4 +73,22 @@ public Optional<String> getArchitectureName() {
5873
public void setArchitecture(String architecture) {
5974
this.architecture = architecture;
6075
}
76+
77+
@Override
78+
public boolean equals(Object other) {
79+
if (this == other) {
80+
return true;
81+
}
82+
if (!(other instanceof PlatformParameters)) {
83+
return false;
84+
}
85+
PlatformParameters otherPlatform = (PlatformParameters) other;
86+
return Objects.equals(architecture, otherPlatform.getArchitecture())
87+
&& Objects.equals(os, otherPlatform.getOs());
88+
}
89+
90+
@Override
91+
public int hashCode() {
92+
return Objects.hash(architecture, os);
93+
}
6194
}

jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ public void testFrom() {
7272
from.auth(auth -> auth.setUsername("some username"));
7373
from.auth(auth -> auth.setPassword("some password"));
7474
from.platforms(
75-
platformSpec -> {
76-
platformSpec.platform(
77-
platform -> {
78-
platform.setArchitecture("arm");
79-
platform.setOs("windows");
80-
});
81-
});
75+
platformSpec ->
76+
platformSpec.platform(
77+
platform -> {
78+
platform.setArchitecture("arm");
79+
platform.setOs("windows");
80+
}));
8281
});
8382
assertThat(testJibExtension.getFrom().getImage()).isEqualTo("some image");
8483
assertThat(testJibExtension.getFrom().getCredHelper()).isEqualTo("some cred helper");
@@ -262,11 +261,10 @@ public void testExtraDirectories_stringListForPaths() {
262261
@Test
263262
public void testExtraDirectories_fileListForPaths() {
264263
testJibExtension.extraDirectories(
265-
extraDirectories -> {
266-
extraDirectories.setPaths(
267-
Arrays.asList(
268-
Paths.get("test", "path").toFile(), Paths.get("another", "path").toFile()));
269-
});
264+
extraDirectories ->
265+
extraDirectories.setPaths(
266+
Arrays.asList(
267+
Paths.get("test", "path").toFile(), Paths.get("another", "path").toFile())));
270268

271269
assertThat(testJibExtension.getExtraDirectories().getPaths()).hasSize(2);
272270
assertThat(testJibExtension.getExtraDirectories().getPaths().get(0).getFrom())
@@ -341,6 +339,14 @@ public void testProperties() {
341339
System.setProperty("jib.from.credHelper", "credHelper");
342340
assertThat(testJibExtension.getFrom().getCredHelper()).isEqualTo("credHelper");
343341

342+
System.setProperty("jib.from.platforms", "linux/amd64,darwin/arm64");
343+
List<PlatformParameters> platforms = testJibExtension.getFrom().getPlatforms().get();
344+
assertThat(platforms).hasSize(2);
345+
assertThat(platforms.get(0).getOs()).isEqualTo("linux");
346+
assertThat(platforms.get(0).getArchitecture()).isEqualTo("amd64");
347+
assertThat(platforms.get(1).getOs()).isEqualTo("darwin");
348+
assertThat(platforms.get(1).getArchitecture()).isEqualTo("arm64");
349+
344350
System.setProperty("jib.to.image", "toImage");
345351
assertThat(testJibExtension.getTo().getImage()).isEqualTo("toImage");
346352
System.setProperty("jib.to.tags", "tag1,tag2,tag3");
@@ -412,6 +418,12 @@ public void testProperties() {
412418
.inOrder();
413419
}
414420

421+
@Test
422+
public void testSystemPropertiesWithInvalidPlatform() {
423+
System.setProperty("jib.from.platforms", "linux /amd64");
424+
assertThrows(IllegalArgumentException.class, testJibExtension.getFrom()::getPlatforms);
425+
}
426+
415427
@Test
416428
public void testPropertiesOutputPaths() {
417429
System.setProperties(new Properties());
@@ -441,10 +453,7 @@ public void testPropertiesOutputPaths() {
441453

442454
private TargetImageParameters generateTargetImageParametersWithTags(String... tags) {
443455
HashSet<String> set = new HashSet<>(Arrays.asList(tags));
444-
testJibExtension.to(
445-
to -> {
446-
to.setTags(set);
447-
});
456+
testJibExtension.to(to -> to.setTags(set));
448457
return testJibExtension.getTo();
449458
}
450459
}

jib-maven-plugin/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
55

66
### Added
77

8+
- [`<from><platforms>`](https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#from-object) parameter for multi-architecture image building can now be configured through Maven and system properties (for example, `-Djib.from.platforms=linux/amd64,linux/arm64` on the command-line). ([#2742](https://github.com/GoogleContainerTools/jib/pull/2742))
9+
810
### Changed
911

1012
- Changed the default base image from the `adoptopenjdk` images to the [`eclipse-temurin`](https://hub.docker.com/_/eclipse-temurin) (for Java 8 and 11) and [`azul/zulu-openjdk`](https://hub.docker.com/r/azul/zulu-openjdk) (for Java 17) images on Docker Hub. Note that Temurin (by Adoptium) is the new name of AdoptOpenJDK. ([#3491](https://github.com/GoogleContainerTools/jib/pull/3491))

jib-maven-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Property | Type | Default | Description
263263
`image` | string | `adoptopenjdk:{8,11}-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).
264264
`auth` | [`auth`](#auth-object) | *None* | Specifies credentials directly (alternative to `credHelper`).
265265
`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-`).
266-
`platforms` | list | See [`platform`](#platform-object) | _Incubating feature_: Configures platforms of base images to select from a manifest list.
266+
`platforms` | list | See [`platform`](#platform-object) | Configures platforms of base images to select from a manifest list.
267267

268268
<a name="to-object"></a>`to` is an object with the following properties:
269269

jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.Map;
3737
import java.util.Optional;
3838
import java.util.Set;
39+
import java.util.regex.Matcher;
40+
import java.util.regex.Pattern;
3941
import java.util.stream.Collectors;
4042
import javax.annotation.Nullable;
4143
import javax.inject.Inject;
@@ -134,6 +136,17 @@ Optional<String> getMode() {
134136
/** Configuration for {@code platform} parameter. */
135137
public static class PlatformParameters implements PlatformConfiguration {
136138

139+
private static PlatformParameters of(String osArchitecture) {
140+
Matcher matcher = Pattern.compile("([^/ ]+)/([^/ ]+)").matcher(osArchitecture);
141+
if (!matcher.matches()) {
142+
throw new IllegalArgumentException("Platform must be of form os/architecture.");
143+
}
144+
PlatformParameters platformParameters = new PlatformParameters();
145+
platformParameters.os = matcher.group(1);
146+
platformParameters.architecture = matcher.group(2);
147+
return platformParameters;
148+
}
149+
137150
@Nullable @Parameter private String os;
138151
@Nullable @Parameter private String architecture;
139152

@@ -158,10 +171,7 @@ public static class FromConfiguration {
158171

159172
/** Constructor for defaults. */
160173
public FromConfiguration() {
161-
PlatformParameters platform = new PlatformParameters();
162-
platform.os = "linux";
163-
platform.architecture = "amd64";
164-
platforms = Collections.singletonList(platform);
174+
platforms = Collections.singletonList(PlatformParameters.of("linux/amd64"));
165175
}
166176
}
167177

@@ -354,6 +364,12 @@ protected void checkJibVersion() throws MojoExecutionException {
354364
* @return the specified platforms
355365
*/
356366
List<PlatformParameters> getPlatforms() {
367+
String property = getProperty(PropertyNames.FROM_PLATFORMS);
368+
if (property != null) {
369+
return ConfigurationPropertyValidator.parseListProperty(property).stream()
370+
.map(PlatformParameters::of)
371+
.collect(Collectors.toList());
372+
}
357373
return from.platforms;
358374
}
359375

0 commit comments

Comments
 (0)