diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BaseImageParameters.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BaseImageParameters.java index c1afc93ec7..3b182b06f6 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BaseImageParameters.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BaseImageParameters.java @@ -21,6 +21,7 @@ import javax.inject.Inject; import org.gradle.api.Action; import org.gradle.api.model.ObjectFactory; +import org.gradle.api.provider.ListProperty; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Nested; import org.gradle.api.tasks.Optional; @@ -29,13 +30,32 @@ public class BaseImageParameters { private final AuthParameters auth; - @Nullable private String image; @Nullable private String credHelper; + private final PlatformParametersSpec platformParametersSpec; + private final ListProperty platforms; @Inject public BaseImageParameters(ObjectFactory objectFactory) { auth = objectFactory.newInstance(AuthParameters.class, "from.auth"); + platforms = objectFactory.listProperty(PlatformParameters.class).empty(); + platformParametersSpec = + objectFactory.newInstance(PlatformParametersSpec.class, objectFactory, platforms); + + PlatformParameters platform = new PlatformParameters(); + platform.setOs("linux"); + platform.setArchitecture("amd64"); + platforms.add(platform); + } + + @Nested + @Optional + public ListProperty getPlatforms() { + return platforms; + } + + public void platforms(Action action) { + action.execute(platformParametersSpec); } @Input diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java index a4d15f8449..6b87545ef9 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java @@ -215,4 +215,9 @@ public Path getImageJsonOutputPath() { public List getPluginExtensions() { return jibExtension.getPluginExtensions().get(); } + + @Override + public List getPlatforms() { + return jibExtension.getFrom().getPlatforms().get(); + } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibExtension.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibExtension.java index 6b85f6aa01..282b7adc0d 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibExtension.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibExtension.java @@ -37,6 +37,12 @@ * from { * image = 'gcr.io/my-gcp-project/my-base-image' * credHelper = 'gcr' + * platforms { + * platform { + * os = 'linux' + * architecture = 'amd64' + * } + * } * } * to { * image = 'gcr.io/gcp-project/my-app:built-with-jib' diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParameters.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParameters.java new file mode 100644 index 0000000000..204d0c20ad --- /dev/null +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParameters.java @@ -0,0 +1,61 @@ +/* + * Copyright 2020 Google LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.tools.jib.gradle; + +import com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration; +import java.util.Optional; +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 { + @Nullable private String os; + @Nullable private String architecture; + + @Input + @Nullable + public String getOs() { + return os; + } + + @Internal + @Override + public Optional getOsName() { + return Optional.ofNullable(os); + } + + public void setOs(String os) { + this.os = os; + } + + @Input + @Nullable + public String getArchitecture() { + return architecture; + } + + @Internal + @Override + public Optional getArchitectureName() { + return Optional.ofNullable(architecture); + } + + public void setArchitecture(String architecture) { + this.architecture = architecture; + } +} diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParametersSpec.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParametersSpec.java new file mode 100644 index 0000000000..681118a0c4 --- /dev/null +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PlatformParametersSpec.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 Google LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.tools.jib.gradle; + +import javax.inject.Inject; +import org.gradle.api.Action; +import org.gradle.api.model.ObjectFactory; +import org.gradle.api.provider.ListProperty; + +/** Allows to add {@link PlatformParameters} objects to the list property of the same type. */ +public class PlatformParametersSpec { + + private final ObjectFactory objectFactory; + private final ListProperty platforms; + + @Inject + public PlatformParametersSpec( + ObjectFactory objectFactory, ListProperty platforms) { + this.platforms = platforms; + this.objectFactory = objectFactory; + } + + /** + * Adds a new platform configuration to the platforms list. + * + * @param action closure representing a platform configuration + */ + public void platform(Action action) { + PlatformParameters platform = objectFactory.newInstance(PlatformParameters.class); + action.execute(platform); + platforms.add(platform); + } +} diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java index 131da5a2a4..53653d7804 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java @@ -20,6 +20,7 @@ import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator; import com.google.cloud.tools.jib.plugins.common.PropertyNames; import com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtensionConfiguration; +import com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; @@ -127,6 +128,22 @@ Optional getMode() { } } + /** Configuration for {@code platform} parameter. */ + public static class PlatformParameters implements PlatformConfiguration { + @Nullable @Parameter private String os; + @Nullable @Parameter private String architecture; + + @Override + public Optional getOsName() { + return Optional.ofNullable(os); + } + + @Override + public Optional getArchitectureName() { + return Optional.ofNullable(architecture); + } + } + /** Configuration for {@code from} parameter. */ public static class FromConfiguration { @@ -135,6 +152,16 @@ public static class FromConfiguration { @Nullable @Parameter private String credHelper; @Parameter private FromAuthConfiguration auth = new FromAuthConfiguration(); + + @Parameter private List platforms; + + /** Constructor for defaults. */ + public FromConfiguration() { + PlatformParameters platform = new PlatformParameters(); + platform.os = "linux"; + platform.architecture = "amd64"; + platforms = Collections.singletonList(platform); + } } /** Configuration for {@code to} parameter, where image is required. */ @@ -330,6 +357,15 @@ protected void checkJibVersion() throws MojoExecutionException { MojoCommon.checkJibVersion(descriptor); } + /** + * Gets the specified platforms. + * + * @return the specified platforms + */ + List getPlatforms() { + return from.platforms; + } + /** * Gets the base image reference. * diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenRawConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenRawConfiguration.java index 8493675fae..6b53006f4b 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenRawConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/MavenRawConfiguration.java @@ -217,4 +217,9 @@ public Path getImageJsonOutputPath() { public List getPluginExtensions() { return jibPluginConfiguration.getPluginExtensions(); } + + @Override + public List getPlatforms() { + return jibPluginConfiguration.getPlatforms(); + } } diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java index a20ae1d707..4ac8dad7e6 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java @@ -69,6 +69,9 @@ public Log getLog() { @Test public void testDefaults() { + Assert.assertEquals("linux", testPluginConfiguration.getPlatforms().get(0).getOsName().get()); + Assert.assertEquals( + "amd64", testPluginConfiguration.getPlatforms().get(0).getArchitectureName().get()); Assert.assertEquals("", testPluginConfiguration.getAppRoot()); Assert.assertNull(testPluginConfiguration.getWorkingDirectory()); Assert.assertTrue(testPluginConfiguration.getExtraClasspath().isEmpty()); diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java index 7e1d720c48..d980f0849f 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java @@ -40,6 +40,13 @@ static interface ExtensionConfiguration { Optional getExtraConfiguration(); } + static interface PlatformConfiguration { + + Optional getOsName(); + + Optional getArchitectureName(); + } + Optional getFromImage(); Optional getToImage(); @@ -107,4 +114,6 @@ static interface ExtensionConfiguration { Path getImageJsonOutputPath(); List getPluginExtensions(); + + List getPlatforms(); }