Skip to content
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

Adding the from.platforms Jib config parameter to jib-maven-plugin and to the jib-gradle-plugin #2568

Merged
merged 21 commits into from
Jul 10, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,13 +30,32 @@
public class BaseImageParameters {

private final AuthParameters auth;

@Nullable private String image;
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
@Nullable private String credHelper;
private final PlatformParametersSpec platformParametersSpec;
private final ListProperty<PlatformParameters> 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);
}

chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
@Nested
@Optional
public ListProperty<PlatformParameters> getPlatforms() {
return platforms;
}

public void platforms(Action<? super PlatformParametersSpec> action) {
action.execute(platformParametersSpec);
}

@Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,9 @@ public Path getImageJsonOutputPath() {
public List<? extends ExtensionConfiguration> getPluginExtensions() {
return jibExtension.getPluginExtensions().get();
}

@Override
public List<? extends PlatformConfiguration> getPlatforms() {
return jibExtension.getFrom().getPlatforms().get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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
louismurerwa marked this conversation as resolved.
Show resolved Hide resolved
public Optional<String> getOsName() {
return Optional.ofNullable(os);
}

public void setOs(String os) {
this.os = os;
}

@Input
@Nullable
public String getArchitecture() {
return architecture;
}

@Internal
@Override
louismurerwa marked this conversation as resolved.
Show resolved Hide resolved
public Optional<String> getArchitectureName() {
return Optional.ofNullable(architecture);
}

public void setArchitecture(String architecture) {
this.architecture = architecture;
}
}
Original file line number Diff line number Diff line change
@@ -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<PlatformParameters> platforms;

@Inject
public PlatformParametersSpec(
ObjectFactory objectFactory, ListProperty<PlatformParameters> 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<? super PlatformParameters> action) {
PlatformParameters platform = objectFactory.newInstance(PlatformParameters.class);
action.execute(platform);
platforms.add(platform);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,6 +128,22 @@ Optional<String> getMode() {
}
}

/** Configuration for {@code platform} parameter. */
louismurerwa marked this conversation as resolved.
Show resolved Hide resolved
public static class PlatformParameters implements PlatformConfiguration {
@Nullable @Parameter private String os;
@Nullable @Parameter private String architecture;

@Override
public Optional<String> getOsName() {
return Optional.ofNullable(os);
}

@Override
public Optional<String> getArchitectureName() {
return Optional.ofNullable(architecture);
}
}

/** Configuration for {@code from} parameter. */
public static class FromConfiguration {

Expand All @@ -135,6 +152,16 @@ public static class FromConfiguration {
@Nullable @Parameter private String credHelper;

@Parameter private FromAuthConfiguration auth = new FromAuthConfiguration();

@Parameter private List<PlatformParameters> 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. */
Expand Down Expand Up @@ -330,6 +357,15 @@ protected void checkJibVersion() throws MojoExecutionException {
MojoCommon.checkJibVersion(descriptor);
}

/**
* Gets the specified platforms.
*
* @return the specified platforms
*/
List<PlatformParameters> getPlatforms() {
return from.platforms;
}

/**
* Gets the base image reference.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,9 @@ public Path getImageJsonOutputPath() {
public List<? extends ExtensionConfiguration> getPluginExtensions() {
return jibPluginConfiguration.getPluginExtensions();
}

@Override
public List<? extends PlatformConfiguration> getPlatforms() {
return jibPluginConfiguration.getPlatforms();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ static interface ExtensionConfiguration {
Optional<Object> getExtraConfiguration();
}

static interface PlatformConfiguration {

Optional<String> getOsName();

Optional<String> getArchitectureName();
}

Optional<String> getFromImage();

Optional<String> getToImage();
Expand Down Expand Up @@ -107,4 +114,6 @@ static interface ExtensionConfiguration {
Path getImageJsonOutputPath();

List<? extends ExtensionConfiguration> getPluginExtensions();

List<? extends PlatformConfiguration> getPlatforms();
}