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

#1804 support buildx use of default builder #1805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 23 additions & 19 deletions src/main/java/io/fabric8/maven/docker/service/BuildXService.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,27 +245,31 @@ protected void createDirectory(Path cachePath) {
protected String createBuilder(Path configPath, List<String> buildX, ImageConfiguration imageConfig, BuildDirs buildDirs) throws MojoExecutionException {
BuildXConfiguration buildXConfiguration = imageConfig.getBuildConfiguration().getBuildX();
String builderName = Optional.ofNullable(buildXConfiguration.getBuilderName()).orElse("maven");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend turning ignore whitespace on to better highlight the change

String nodeName = buildXConfiguration.getNodeName();
Path builderPath = configPath.resolve(Paths.get("buildx", "instances", builderName));
if(Files.notExists(builderPath)) {
List<String> cmds = new ArrayList<>(buildX);
append(cmds, "create", "--driver", "docker-container", "--name", builderName);
if (nodeName != null) {
append(cmds, "--node", nodeName);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker does a nice job of providing meaningful error messages if you try to use the default builder with multiple architectures, etc. For instance:

[INFO] DOCKER> ERROR: Multi-platform build is not supported for the docker driver.
[INFO] DOCKER> Switch to a different driver, or turn on the containerd image store, and try again.
[INFO] DOCKER> Learn more at https://docs.docker.com/go/build-multi-platform/
[ERROR] DOCKER> Error status (1) when building

if ("default".equals(builderName)) {
logger.info("Using default builder with buildx - only single platforms will be supported");
} else {
String nodeName = buildXConfiguration.getNodeName();
Path builderPath = configPath.resolve(Paths.get("buildx", "instances", builderName));
if(Files.notExists(builderPath)) {
List<String> cmds = new ArrayList<>(buildX);
append(cmds, "create", "--driver", "docker-container", "--name", builderName);
if (nodeName != null) {
append(cmds, "--node", nodeName);
}

if (buildXConfiguration.getDriverOpts() != null && !buildXConfiguration.getDriverOpts().isEmpty()) {
buildXConfiguration.getDriverOpts().forEach((key, value) -> append(cmds, "--driver-opt", key + '=' + value));
}
if (buildXConfiguration.getDriverOpts() != null && !buildXConfiguration.getDriverOpts().isEmpty()) {
buildXConfiguration.getDriverOpts().forEach((key, value) -> append(cmds, "--driver-opt", key + '=' + value));
}

String buildConfig = buildXConfiguration.getConfigFile();
if(buildConfig != null) {
append(cmds, "--config",
buildDirs.getProjectPath(EnvUtil.resolveHomeReference(buildConfig)).toString());
}
int rc = exec.process(cmds);
if (rc != 0) {
throw new MojoExecutionException("Error status (" + rc + ") while creating builder " + builderName);
String buildConfig = buildXConfiguration.getConfigFile();
if (buildConfig != null) {
append(cmds, "--config",
buildDirs.getProjectPath(EnvUtil.resolveHomeReference(buildConfig)).toString());
}
int rc = exec.process(cmds);
if (rc != 0) {
throw new MojoExecutionException("Error status (" + rc + ") while creating builder " + builderName);
}
}
}
return builderName;
Expand Down
54 changes: 41 additions & 13 deletions src/test/java/io/fabric8/maven/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void noSkipWhenNotPom() throws IOException, MojoExecutionException {
}

@Test
void buildUsingBuildx() throws IOException, MojoExecutionException {
void buildUsingBuildxWithDockerContainerDriver() throws IOException, MojoExecutionException {
givenBuildXService();

givenMavenProject(buildMojo);
Expand All @@ -98,6 +98,20 @@ void buildUsingBuildx() throws IOException, MojoExecutionException {
thenBuildxRun(null, null, true, null );
}

@Test
void buildUsingBuildxWithDefaultDriver() throws IOException, MojoExecutionException {
givenBuildXService();

givenMavenProject(buildMojo);
ImageConfiguration imageConfiguration = singleBuildXImageWithDefaultBuilderName(null);
givenResolvedImages(buildMojo, Collections.singletonList(imageConfiguration));
givenPackaging("jar");

whenMojoExecutes();

thenBuildxRun(null, null, true, null, Collections.emptyList(), false);
}

@Test
void buildUsingConfiguredBuildx() throws IOException, MojoExecutionException {
givenBuildXService();
Expand Down Expand Up @@ -269,7 +283,7 @@ void buildWithTagByBuildx(boolean skipTag) throws IOException, MojoExecutionExce
List<String> fullTags = skipTag ? Collections.emptyList() : tags.stream()
.map(tag -> new ImageName(imageConfiguration.getName(), tag).getFullName())
.collect(Collectors.toList());
thenBuildxRun(null, null, true, null, fullTags);
thenBuildxRun(null, null, true, null, fullTags, true);
}

@ParameterizedTest
Expand Down Expand Up @@ -348,30 +362,39 @@ private void verifyBuild(int wantedNumberOfInvocations) throws DockerAccessExcep

private void thenBuildxRun(String relativeConfigFile, String contextDir, boolean nativePlatformIncluded,
String attestation) throws MojoExecutionException {
thenBuildxRun(relativeConfigFile, contextDir, nativePlatformIncluded, attestation, Collections.emptyList());
thenBuildxRun(relativeConfigFile, contextDir, nativePlatformIncluded, attestation, Collections.emptyList(), true);
}

private void thenBuildxRun(String relativeConfigFile, String contextDir,
boolean nativePlatformIncluded, String attestation, List<String> tags)
boolean nativePlatformIncluded, String attestation, List<String> tags, boolean useMavenBuilder)
throws MojoExecutionException {
Path buildPath = projectBaseDirectory.toPath().resolve("target/docker/example/latest");
String config = getOsDependentBuild(buildPath, "docker");
String configFile =
relativeConfigFile != null ? getOsDependentBuild(projectBaseDirectory.toPath(), relativeConfigFile) :
null;

List<String> cmds =
BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
"create", "--driver", "docker-container", "--name", "maven", "--node", "maven0");
if (configFile != null) {
BuildXService.append(cmds, "--config", configFile.replace('/', File.separatorChar));
if (useMavenBuilder) {
List<String> cmds =
BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx", "create",
"--driver", "docker-container", "--name", "maven", "--node", "maven0");

if (configFile != null) {
cmds = BuildXService.append(cmds, "--config", configFile.replace('/', File.separatorChar));
}
Mockito.verify(exec).process(cmds);
}
Mockito.verify(exec).process(cmds);

if (nativePlatformIncluded) {
List<String> buildXLine = BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
"build", "--progress=plain", "--builder", "maven",
"--platform", NATIVE_PLATFORM, "--tag", "example:latest");
List<String> buildXLineInitial = BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
"build", "--progress=plain", "--builder");
if (useMavenBuilder) {
buildXLineInitial.add("maven");
} else {
buildXLineInitial.add("default");
}

List<String> buildXLine = BuildXService.append(buildXLineInitial,"--platform", NATIVE_PLATFORM, "--tag", "example:latest");

tags.forEach(tag -> {
buildXLine.add("--tag");
Expand Down Expand Up @@ -446,6 +469,11 @@ private ImageConfiguration singleBuildXImageWithAttestations(Boolean sbom, Strin
.build(), null);
}

private ImageConfiguration singleBuildXImageWithDefaultBuilderName(String configFile) {
return singleImageConfiguration(getBuildXPlatforms(NATIVE_PLATFORM).configFile(configFile)
.builderName("default").build(), null);
}

protected ImageConfiguration singleImageWithAuthRegistry(String dockerFile) {
BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder()
.dockerFile(dockerFile)
Expand Down