Skip to content

Commit

Permalink
feat: add support for dockerd Builder-Version header
Browse files Browse the repository at this point in the history
* 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: #1711
  • Loading branch information
rroesch1 committed Jun 19, 2024
1 parent ac5e09a commit 70d4d15
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 70d4d15

Please sign in to comment.