Skip to content

Expose jib.from.platforms as property. #3526

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

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
2 changes: 2 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.

### Added

- [`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))

### Changed

- 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))
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 @@ -213,7 +213,7 @@ Property | Type | Default | Description
`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).
`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) | _Incubating feature_: Configures platforms of base images to select from a manifest list.
`platforms` | [`platforms`](#platforms-closure) | See [`platforms`](#platforms-closure) | Configures platforms of base images to select from a manifest list.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

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

import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator;
import com.google.cloud.tools.jib.plugins.common.PropertyNames;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.Action;
Expand Down Expand Up @@ -53,6 +56,16 @@ public BaseImageParameters(ObjectFactory objectFactory) {
@Nested
@Optional
public ListProperty<PlatformParameters> getPlatforms() {
String property = System.getProperty(PropertyNames.FROM_PLATFORMS);
if (property != null) {
List<PlatformParameters> parsed =
ConfigurationPropertyValidator.parseListProperty(property).stream()
.map(PlatformParameters::of)
.collect(Collectors.toList());
if (!parsed.equals(platforms.get())) {
platforms.set(parsed);
}
}
return platforms;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@
package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;

/** Configuration of a platform. */
public class PlatformParameters implements PlatformConfiguration {

static PlatformParameters of(String osArchitecture) {
Matcher matcher = Pattern.compile("([^/ ]+)/([^/ ]+)").matcher(osArchitecture);
if (!matcher.matches()) {
throw new IllegalArgumentException("Platform must be of form os/architecture.");
}
PlatformParameters platformParameters = new PlatformParameters();
platformParameters.os = matcher.group(1);
platformParameters.architecture = matcher.group(2);
return platformParameters;
}

@Nullable private String os;
@Nullable private String architecture;

Expand Down Expand Up @@ -58,4 +73,22 @@ public Optional<String> getArchitectureName() {
public void setArchitecture(String architecture) {
this.architecture = architecture;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof PlatformParameters)) {
return false;
}
PlatformParameters otherPlatform = (PlatformParameters) other;
return Objects.equals(architecture, otherPlatform.getArchitecture())
&& Objects.equals(os, otherPlatform.getOs());
}

@Override
public int hashCode() {
return Objects.hash(architecture, os);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ public void testFrom() {
from.auth(auth -> auth.setUsername("some username"));
from.auth(auth -> auth.setPassword("some password"));
from.platforms(
platformSpec -> {
platformSpec.platform(
platform -> {
platform.setArchitecture("arm");
platform.setOs("windows");
});
});
platformSpec ->
platformSpec.platform(
platform -> {
platform.setArchitecture("arm");
platform.setOs("windows");
}));
});
assertThat(testJibExtension.getFrom().getImage()).isEqualTo("some image");
assertThat(testJibExtension.getFrom().getCredHelper()).isEqualTo("some cred helper");
Expand Down Expand Up @@ -262,11 +261,10 @@ public void testExtraDirectories_stringListForPaths() {
@Test
public void testExtraDirectories_fileListForPaths() {
testJibExtension.extraDirectories(
extraDirectories -> {
extraDirectories.setPaths(
Arrays.asList(
Paths.get("test", "path").toFile(), Paths.get("another", "path").toFile()));
});
extraDirectories ->
extraDirectories.setPaths(
Arrays.asList(
Paths.get("test", "path").toFile(), Paths.get("another", "path").toFile())));

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

System.setProperty("jib.from.platforms", "linux/amd64,darwin/arm64");
List<PlatformParameters> platforms = testJibExtension.getFrom().getPlatforms().get();
assertThat(platforms).hasSize(2);
assertThat(platforms.get(0).getOs()).isEqualTo("linux");
assertThat(platforms.get(0).getArchitecture()).isEqualTo("amd64");
assertThat(platforms.get(1).getOs()).isEqualTo("darwin");
assertThat(platforms.get(1).getArchitecture()).isEqualTo("arm64");

System.setProperty("jib.to.image", "toImage");
assertThat(testJibExtension.getTo().getImage()).isEqualTo("toImage");
System.setProperty("jib.to.tags", "tag1,tag2,tag3");
Expand Down Expand Up @@ -412,6 +418,12 @@ public void testProperties() {
.inOrder();
}

@Test
public void testSystemPropertiesWithInvalidPlatform() {
System.setProperty("jib.from.platforms", "linux /amd64");
assertThrows(IllegalArgumentException.class, testJibExtension.getFrom()::getPlatforms);
}

@Test
public void testPropertiesOutputPaths() {
System.setProperties(new Properties());
Expand Down Expand Up @@ -441,10 +453,7 @@ public void testPropertiesOutputPaths() {

private TargetImageParameters generateTargetImageParametersWithTags(String... tags) {
HashSet<String> set = new HashSet<>(Arrays.asList(tags));
testJibExtension.to(
to -> {
to.setTags(set);
});
testJibExtension.to(to -> to.setTags(set));
return testJibExtension.getTo();
}
}
2 changes: 2 additions & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.

### Added

- [`<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))

### Changed

- 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))
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 @@ -263,7 +263,7 @@ Property | Type | Default | Description
`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).
`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) | _Incubating feature_: Configures platforms of base images to select from a manifest list.
`platforms` | list | See [`platform`](#platform-object) | Configures platforms of base images to select from a manifest list.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.inject.Inject;
Expand Down Expand Up @@ -134,6 +136,17 @@ Optional<String> getMode() {
/** Configuration for {@code platform} parameter. */
public static class PlatformParameters implements PlatformConfiguration {

private static PlatformParameters of(String osArchitecture) {
Matcher matcher = Pattern.compile("([^/ ]+)/([^/ ]+)").matcher(osArchitecture);
if (!matcher.matches()) {
throw new IllegalArgumentException("Platform must be of form os/architecture.");
}
PlatformParameters platformParameters = new PlatformParameters();
platformParameters.os = matcher.group(1);
platformParameters.architecture = matcher.group(2);
return platformParameters;
}

@Nullable @Parameter private String os;
@Nullable @Parameter private String architecture;

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

/** Constructor for defaults. */
public FromConfiguration() {
PlatformParameters platform = new PlatformParameters();
platform.os = "linux";
platform.architecture = "amd64";
platforms = Collections.singletonList(platform);
platforms = Collections.singletonList(PlatformParameters.of("linux/amd64"));
}
}

Expand Down Expand Up @@ -354,6 +364,12 @@ protected void checkJibVersion() throws MojoExecutionException {
* @return the specified platforms
*/
List<PlatformParameters> getPlatforms() {
String property = getProperty(PropertyNames.FROM_PLATFORMS);
if (property != null) {
return ConfigurationPropertyValidator.parseListProperty(property).stream()
.map(PlatformParameters::of)
.collect(Collectors.toList());
}
return from.platforms;
}

Expand Down
Loading