From 70d4d15e2d094bfdffeae522c85905109333db88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20R=C3=B6sch?= Date: Wed, 19 Jun 2024 15:34:48 +0200 Subject: [PATCH] feat: add support for dockerd Builder-Version header * in docker 23 dockerd introduced the 'Builder-Version' header to expose the recommended version of the builder backend * it is up to the client to follow this recommendation * dockerd uses the deprecated 'classic builder' if clients do not set the 'version' build option explicitly * d-m-p should always set the 'version' option as recommended by the dockerd Refs: fabric8io/docker-maven-plugin#1711 --- .../fabric8/maven/docker/access/BuildOptions.java | 7 +++++++ .../fabric8/maven/docker/access/DockerAccess.java | 7 +++++++ .../access/hc/DockerAccessWithHcClient.java | 15 +++++++++++++++ .../maven/docker/service/BuildService.java | 1 + 4 files changed, 30 insertions(+) diff --git a/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java b/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java index 439c0bf3b..e0886dae9 100644 --- a/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java +++ b/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java @@ -43,6 +43,13 @@ public BuildOptions addOption(String key, String value) { return this; } + public BuildOptions builderVersion(String version) { + if (version != null) { + options.put("version", version); + } + return this; + } + public BuildOptions dockerfile(String name) { if (name != null) { options.put("dockerfile", name); diff --git a/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java b/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java index 8fc199275..da421ce23 100644 --- a/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java +++ b/src/main/java/io/fabric8/maven/docker/access/DockerAccess.java @@ -42,6 +42,13 @@ public interface DockerAccess { */ String getServerApiVersion() throws DockerAccessException; + /** + * Get the builder version recommended by the daemon (see 'Builder-Version' response header of '/_ping' API) + * + * @return the recommended builder version or 'null' if dockerd doesn't recommend any version + */ + String getDefaultBuilderVersion(); + /** * Get the native platform * @return The platform os/arch diff --git a/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java b/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java index 369fe94d2..e79893840 100644 --- a/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java +++ b/src/main/java/io/fabric8/maven/docker/access/hc/DockerAccessWithHcClient.java @@ -19,6 +19,7 @@ import io.fabric8.maven.docker.access.CreateImageOptions; import org.apache.commons.io.IOUtils; +import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.HttpResponseException; import org.apache.http.client.ResponseHandler; @@ -111,6 +112,7 @@ public class DockerAccessWithHcClient implements DockerAccess { private final ApacheHttpClientDelegate delegate; private final String apiVersion; + private final String defaultBuilderVersion; private final String nativePlatform; private final UrlBuilder urlBuilder; @@ -149,6 +151,14 @@ public DockerAccessWithHcClient(@Nonnull String baseUrl, this.nativePlatform = info.get("Os").getAsString() + "/" + info.get("Arch").getAsString(); this.urlBuilder = new UrlBuilder(baseUrl, "v" + apiVersion); this.log = log; + String pingUrl = baseUrl + "/_ping"; + defaultBuilderVersion = delegate.get(pingUrl, response -> { + Header[] builderVersionHeaders = response.getHeaders("Builder-Version"); + if (null != builderVersionHeaders && 0 < builderVersionHeaders.length) { + return builderVersionHeaders[0].getValue(); + } + return null; + }, HTTP_OK); } static String stripTrailingSlash(String url) { @@ -165,6 +175,11 @@ public String getServerApiVersion() { return apiVersion; } + @Override + public String getDefaultBuilderVersion() { + return defaultBuilderVersion; + } + @Override public void startExecContainer(String containerId, LogOutputSpec outputSpec) throws DockerAccessException { String url = urlBuilder.startExecContainer(containerId); diff --git a/src/main/java/io/fabric8/maven/docker/service/BuildService.java b/src/main/java/io/fabric8/maven/docker/service/BuildService.java index a099b69fb..4dcfd46bb 100644 --- a/src/main/java/io/fabric8/maven/docker/service/BuildService.java +++ b/src/main/java/io/fabric8/maven/docker/service/BuildService.java @@ -175,6 +175,7 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, // auto is now supported by docker, consider switching? BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()) + .builderVersion(buildConfig.getBuildOptions().getOrDefault("version", docker.getDefaultBuilderVersion())) .dockerfile(buildConfig.getDockerfileName()) .forceRemove(cleanupMode.isRemove()) .noCache(noCache)