Skip to content

Commit

Permalink
change buildAndLoadNativePlatform to buildAndLoadSinglePlatform
Browse files Browse the repository at this point in the history
Signed-off-by: Marsette Vona <[email protected]>
  • Loading branch information
martyvona authored and rohanKanojia committed Apr 18, 2023
1 parent aa49dd4 commit 47bc529
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
24 changes: 15 additions & 9 deletions src/main/asciidoc/inc/build/_buildx.adoc
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@

[[build-buildx]]

Buildx is enabled when there is a non-empty `<platform>` element inside the `<buildx>` configuration. Only the native platform
is built and saved in the local image cache by the `build` goal. All specified platforms are built and pushed into the remote
repository by the `push` goal. This behavior is to prevent non-native images from tainting the local image cache.
Buildx is enabled when there is a non-empty `<platform>` element inside the `<buildx>` configuration.

The local image cache cannot hold multi-architecture images nor can it have two platform specific images of the same name. The
recommended `<buildx>` configuration is to specify all supported platforms, including the native platform, in the `<platforms>`
element. This allows local integration testing of the build image from the local cache. During install or deploy phase, the
build machine will build and push all images to the registry. Any downstream consumers, regardless of native architecture, will
be able to use the multi-architecture image.
The local image cache cannot hold multi-architecture images nor can it have two platform specific images of the same name.
Thus the `build` goal will build and save a single-architecture image to the local image cache if possible:

* If the `<platform>` element contains a single platform, that image will be built.
* If the `<platform>` element contains more than one platform including the native platform, the native platform be used.
* If the `<platform>` element contains more than one platform not including the native platform, no image will be built.
These rules only apply to the image built and loaded into the local image cache with the `build` goal. They do not apply to the `push` goal which will always build and push either a single-architecture or multi-architecture image with whatever platforms are specified in the `<platform>` element.

The recommended `<buildx>` configuration is to specify all supported platforms, including the native platform, in the
`<platform>` element. This allows local integration testing of the build image from the local cache. During install or deploy
phase, the build machine will build and push a multi-architecture image containing all specified platforms to the registry.
Any downstream consumers, regardless of native architecture, will be able to use the multi-architecture image.

The `<buildx>` element within `<build>` defines how to build multi-architecture images.

Expand Down Expand Up @@ -75,4 +81,4 @@ You can now override the built platforms using a command line define:
[source,bash]
----
mvn clean deploy -Ddocker.platforms=linux/amd64,linux/arm64
----
----
12 changes: 7 additions & 5 deletions src/main/java/io/fabric8/maven/docker/service/BuildXService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BuildXService(DockerAccess dockerAccess, DockerAssemblyManager dockerAsse
}

public void build(ProjectPaths projectPaths, ImageConfiguration imageConfig, String configuredRegistry, AuthConfig authConfig, File buildArchive) throws MojoExecutionException {
useBuilder(projectPaths, imageConfig, configuredRegistry, authConfig, buildArchive, this::buildAndLoadNativePlatform);
useBuilder(projectPaths, imageConfig, configuredRegistry, authConfig, buildArchive, this::buildAndLoadSinglePlatform);
}

public void push(ProjectPaths projectPaths, ImageConfiguration imageConfig, String configuredRegistry, AuthConfig authConfig) throws MojoExecutionException {
Expand Down Expand Up @@ -92,14 +92,16 @@ private void removeConfigJson(Path configJson) {
}
}

private void buildAndLoadNativePlatform(List<String> buildX, String builderName, BuildDirs buildDirs, ImageConfiguration imageConfig, String configuredRegistry, File buildArchive) throws MojoExecutionException {
private void buildAndLoadSinglePlatform(List<String> buildX, String builderName, BuildDirs buildDirs, ImageConfiguration imageConfig, String configuredRegistry, File buildArchive) throws MojoExecutionException {
List<String> platforms = imageConfig.getBuildConfiguration().getBuildX().getPlatforms();
// build and load the native image by re-building, image should be cached and build should be quick
// build and load the single-platform image by re-building, image should be cached and build should be quick
String nativePlatform = dockerAccess.getNativePlatform();
if (platforms.contains(nativePlatform)) {
if (platforms.size() == 1) {
buildX(buildX, builderName, buildDirs, imageConfig, configuredRegistry, platforms, buildArchive, "--load");
} else if (platforms.contains(nativePlatform)) {
buildX(buildX, builderName, buildDirs, imageConfig, configuredRegistry, Collections.singletonList(nativePlatform), buildArchive, "--load");
} else {
logger.info("Native platform not specified, no image built");
logger.info("More than one platform specified not including native %s, no image built", nativePlatform);
}
}

Expand Down

0 comments on commit 47bc529

Please sign in to comment.