Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for dockerd Builder-Version header #1794

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -172,9 +172,13 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params,

Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);

String builderVersion = null != buildConfig.getBuildOptions()
? buildConfig.getBuildOptions().getOrDefault("version", docker.getDefaultBuilderVersion()) // use configured version …
: docker.getDefaultBuilderVersion(); // … or use the default version
// auto is now supported by docker, consider switching?
BuildOptions opts =
new BuildOptions(buildConfig.getBuildOptions())
.builderVersion(builderVersion)
.dockerfile(buildConfig.getDockerfileName())
.forceRemove(cleanupMode.isRemove())
.noCache(noCache)
Expand Down
Loading