Skip to content

Commit 47bc529

Browse files
martyvonarohanKanojia
authored andcommitted
change buildAndLoadNativePlatform to buildAndLoadSinglePlatform
Signed-off-by: Marsette Vona <martyvona@gmail.com>
1 parent aa49dd4 commit 47bc529

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Diff for: src/main/asciidoc/inc/build/_buildx.adoc

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11

22
[[build-buildx]]
33

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

8-
The local image cache cannot hold multi-architecture images nor can it have two platform specific images of the same name. The
9-
recommended `<buildx>` configuration is to specify all supported platforms, including the native platform, in the `<platforms>`
10-
element. This allows local integration testing of the build image from the local cache. During install or deploy phase, the
11-
build machine will build and push all images to the registry. Any downstream consumers, regardless of native architecture, will
12-
be able to use the multi-architecture image.
6+
The local image cache cannot hold multi-architecture images nor can it have two platform specific images of the same name.
7+
Thus the `build` goal will build and save a single-architecture image to the local image cache if possible:
8+
9+
* If the `<platform>` element contains a single platform, that image will be built.
10+
* If the `<platform>` element contains more than one platform including the native platform, the native platform be used.
11+
* If the `<platform>` element contains more than one platform not including the native platform, no image will be built.
12+
13+
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.
14+
15+
The recommended `<buildx>` configuration is to specify all supported platforms, including the native platform, in the
16+
`<platform>` element. This allows local integration testing of the build image from the local cache. During install or deploy
17+
phase, the build machine will build and push a multi-architecture image containing all specified platforms to the registry.
18+
Any downstream consumers, regardless of native architecture, will be able to use the multi-architecture image.
1319

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

@@ -75,4 +81,4 @@ You can now override the built platforms using a command line define:
7581
[source,bash]
7682
----
7783
mvn clean deploy -Ddocker.platforms=linux/amd64,linux/arm64
78-
----
84+
----

Diff for: src/main/java/io/fabric8/maven/docker/service/BuildXService.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public BuildXService(DockerAccess dockerAccess, DockerAssemblyManager dockerAsse
4949
}
5050

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

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

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

0 commit comments

Comments
 (0)