Skip to content

Commit

Permalink
Added an option to allow pom.xml to specify podman --platform argumen…
Browse files Browse the repository at this point in the history
…t such that resulting image can be built for desired target platform.
  • Loading branch information
rajha-korithrien authored and Max Planck committed Jul 7, 2023
1 parent 62c6da4 commit 1c2f581
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/goals/build.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ Note: You can also override the default value of layers by setting the BUILDAH_L

**See**: https://docs.podman.io/en/latest/markdown/podman-build.1.html

|platform
|Specifies the platform to be passed to Podman to control the operating system, architecture and variants of the resulting image.

**Default value is**: `null` (not specified and Podman will build a container that targets the system doing the container build)

**Example value for linux x86_64 is**: linux/amd64

**Example value for linux aarch64 is**: linux/arm64

**See (--platform)**: https://docs.podman.io/en/latest/markdown/podman-build.1.html
|noCache
|Do not use existing cached images for the container build. Build from the start with a new set of cached layers.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class PodmanBuildCommand extends AbstractPodmanCommand {
private static final String PULL_ALWAYS_CMD = "--pull-always";
private static final String NO_CACHE_CMD = "--no-cache";
private static final String BUILD_ARG_CMD = "--build-arg";
private static final String PLATFORM_CMD = "--platform";
private static final String SUBCOMMAND = "build";

private PodmanBuildCommand(Log log, PodmanConfiguration podmanConfig, CommandExecutorDelegate delegate) {
Expand Down Expand Up @@ -137,6 +138,17 @@ public Builder setPullAllways(Boolean pullAlways) {
return this;
}

/**
* Sets the platform for the resulting image rather using the default of the build system
*
* @param platform A valid combination of GO OS and GO ARCH for example linux/arm64 (https://golang.org/doc/install/source#environment)
* @return This builder instance
*/
public Builder setPlatform(String platform){
command.withOption(PLATFORM_CMD, platform);
return this;
}

public Builder addBuildArgs(Map<String, String> args) {
Map<String, String> allBuildArgs = new HashMap<>(args);
allBuildArgs.putAll(getBuildArgsFromSystem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ public abstract class AbstractImageBuildConfiguration {
@Parameter
protected Boolean layers;

/**
* Specify the resulting image platform by being passed to the "--platform" option of podman.
* <p>
*
* @see "https://docs.podman.io/en/latest/markdown/podman-build.1.html"
*/
@Parameter
protected String platform;

/**
* Will be set when this class is validated using the #initAndValidate() method
*/
Expand Down Expand Up @@ -322,6 +331,15 @@ public Boolean getLayers() {
return layers;
}

/**
* Returns the platform that Podman should be told to build for.
*
* @return When set, a string in the format of GO OS/GO ARCH (because GO is the language of Podman) for example: linux/arm64
*/
public Optional<String> getPlatform(){
return Optional.ofNullable(platform);
}

/**
* Returns a boolean indicating whether this configuration is valid
*
Expand Down Expand Up @@ -379,6 +397,15 @@ public void setLayers(Boolean layers) {
this.layers = layers;
}

/**
* Used to set the platform to be passed to Podman when building the image.
*
* @param platform String in the format of GO OS/GO ARCH (because GO is the language of Podman) for example: linux/arm64
*/
public void setPlatform(String platform){
this.platform = platform;
}

/**
* Sets the noCache option. Allows configuring whether caching should be used
* to cache images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public List<String> build(SingleImageConfiguration image) throws MojoExecutionEx
builder = builder.setPullAllways(pullAlwaysOptional.get());
}

Optional<String> platform = image.getBuild().getPlatform();
if (platform.isPresent()) {
builder = builder.setPlatform(platform.get());
}

builder.addBuildArgs(image.getBuild().getArgs());

return builder.build().execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public TestSingleImageConfigurationBuilder setLayers(boolean layers) {
return this;
}

public TestSingleImageConfigurationBuilder setPlatform(String platform){
image.getBuild().setPlatform(platform);
return this;
}

public TestSingleImageConfigurationBuilder setNoCache(boolean noCache) {
image.getBuild().setNoCache(noCache);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,31 @@ public void testBuildNoLayers() throws MojoExecutionException {
+ image.getBuild().getTargetContainerFile() + " --no-cache=false --layers=false .", delegate.getCommandAsString());
}

@Test
public void testBuildPlatform() throws MojoExecutionException {
when(mavenProject.getBuild()).thenReturn(build);
when(build.getDirectory()).thenReturn("target");

PodmanConfiguration podmanConfig = new TestPodmanConfigurationBuilder().setTlsVerify(TRUE).initAndValidate(mavenProject, log).build();
SingleImageConfiguration image = new TestSingleImageConfigurationBuilder("test_image")
.setFormat(OCI)
.setLayers(false)
.setContainerfileDir("src/test/resources")
.setPlatform("linux/arm64")
.initAndValidate(mavenProject, log, true)
.build();

String sampleImageHash = "this_would_normally_be_an_image_hash";
InterceptorCommandExecutorDelegate delegate = new InterceptorCommandExecutorDelegate(Collections.singletonList(sampleImageHash));
podmanExecutorService = new PodmanExecutorService(log, podmanConfig, delegate);

podmanExecutorService.build(image);

Assertions.assertEquals("podman build --tls-verify=true --format=oci --file="
+ image.getBuild().getTargetContainerFile() + " --no-cache=false --layers=false --platform=linux/arm64 .", delegate.getCommandAsString());

}

@Test
public void testBuildPullAlways() throws MojoExecutionException {
when(mavenProject.getBuild()).thenReturn(build);
Expand Down

0 comments on commit 1c2f581

Please sign in to comment.