Skip to content

Commit

Permalink
correct buildx tags
Browse files Browse the repository at this point in the history
closes #1566
closes #1567
  • Loading branch information
chonton authored and rohanKanojia committed Jun 11, 2022
1 parent adf91b4 commit f42c906
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
4 changes: 4 additions & 0 deletions it/buildx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<platform>linux/arm64</platform>
</platforms>
</buildx>
<tags>
<tag>${project.version}</tag>
<tag>latest</tag>
</tags>
</build>
<run>
<log>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.fabric8.maven.docker.config.BuildXConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.ProjectPaths;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -119,7 +120,7 @@ private void buildX(List<String> buildX, String builderName, BuildDirs buildDirs
cmdLine.add(String.join(",", platforms));
buildConfiguration.getTags().forEach(t -> {
cmdLine.add("--tag");
cmdLine.add(t);
cmdLine.add(new ImageName(imageConfig.getName(), t).getFullName());
}
);
cmdLine.add("--tag");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -77,6 +79,10 @@ class RegistryServiceTest {
@Mock
private BuildXService.Exec exec;

private static String getOsDependentBuild(Path buildPath, String docker) {
return buildPath.resolve(docker).toString().replace('/', File.separatorChar);
}

@BeforeEach
void setup() {
BuildXService buildXService = new BuildXService(docker, dockerAssemblyManager, logger, exec);
Expand All @@ -92,7 +98,7 @@ void setup() {
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
@ValueSource(booleans = { false, true })
void pullImagePullPolicyAlways(boolean hasImage) throws Exception {
givenAnImage();
givenAnImageConfiguration("myregistry.com/user/app:1.0.1");
Expand All @@ -103,7 +109,7 @@ void pullImagePullPolicyAlways(boolean hasImage) throws Exception {
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
@ValueSource(booleans = { false, true })
void pullImageAutopullAlways(boolean hasImage) throws Exception {
givenAnImage();
givenAnImageConfiguration("myregistry.com/user/app:1.0.1");
Expand Down Expand Up @@ -262,34 +268,46 @@ void pushImage() throws DockerAccessException {

@Test
void pushBuildXImage() throws MojoExecutionException {
givenBuildxImageConfiguration("user/test:1.0.1", null, null);
givenBuildxImageConfiguration("user/test:1.0.1", null, null, null);
givenCredentials("skroob", "12345");

whenPushImage();

thenBuildxImageHasBeenPushed(null, null);
thenBuildxImageHasBeenPushed(null, null, false);
thenNoExceptionThrown();
}

@Test
void pushBuildXImageWithDockerfile() throws MojoExecutionException {
givenBuildxImageConfiguration("user/test:1.0.1", null, projectBaseDir.toPath().resolve("src/docker/Dockerfile").toString());
String dockerFile = projectBaseDir.toPath().resolve("src/docker/Dockerfile").toString();
givenBuildxImageConfiguration("user/test:1.0.1", null, dockerFile, null);
givenCredentials("skroob", "12345");

whenPushImage();

thenBuildxImageHasBeenPushed(null, "Dockerfile");
thenBuildxImageHasBeenPushed(null, "Dockerfile", false);
thenNoExceptionThrown();
}

@Test
void pushBuildXImageProvidedBuilder() throws MojoExecutionException {
givenBuildxImageConfiguration("user/test:1.0.1", "provided-builder", null);
givenBuildxImageConfiguration("user/test:1.0.1", "provided-builder", null, null);
givenCredentials("King_Roland_of_Druidia", "12345");

whenPushImage();

thenBuildxImageHasBeenPushed("provided-builder", null, false);
thenNoExceptionThrown();
}

@Test
void pushBuildXImageTag() throws MojoExecutionException {
givenBuildxImageConfiguration("user/test:1.0.1", null, null, "perri-air");
givenCredentials("King_Roland_of_Druidia", "12345");

whenPushImage();

thenBuildxImageHasBeenPushed("provided-builder", null);
thenBuildxImageHasBeenPushed(null, null, true);
thenNoExceptionThrown();
}

Expand Down Expand Up @@ -332,41 +350,45 @@ private void thenImageHasBeenPushed() throws DockerAccessException {
Mockito.verify(docker).pushImage(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyInt());
}

private void thenBuildxImageHasBeenPushed(String providedBuilder, String relativeDockerfile) throws MojoExecutionException {
private void thenBuildxImageHasBeenPushed(String providedBuilder, String relativeDockerfile, boolean tag) throws MojoExecutionException {
Path buildPath = projectBaseDir.toPath().resolve("target/docker").resolve("user/test/1.0.1");
String config = getOsDependentBuild(buildPath, "docker");
String cacheDir = getOsDependentBuild(buildPath, "cache");
String buildDir = getOsDependentBuild(buildPath, "build");
String builderName = providedBuilder!=null ? providedBuilder : "dmp_user_test_1.0.1";
String builderName = providedBuilder != null ? providedBuilder : "dmp_user_test_1.0.1";

if (providedBuilder == null) {
Mockito.verify(exec).process(Arrays.asList("docker", "--config", config, "buildx"),
"create", "--driver", "docker-container", "--name", builderName);
}

List<String> args = new ArrayList<>();
args.addAll(Arrays.asList(
"docker", "--config", config, "buildx",
"build", "--progress=plain", "--builder", builderName,
"--platform", "linux/amd64,linux/arm64"));
if (tag) {
args.addAll(Arrays.asList("--tag", "user/test:perri-air"));
}
args.addAll(Arrays.asList("--tag", "user/test:1.0.1", "--push",
"--cache-to=type=local,dest=" + cacheDir, "--cache-from=type=local,src=" + cacheDir));

String[] cmds;
if (relativeDockerfile != null) {
Path dockerBuild = buildPath.resolve("tmp/docker-build");
cmds = new String[] { "--file=" + dockerBuild.resolve(relativeDockerfile), dockerBuild.toString() };
} else {
cmds = new String[] { buildDir };
}
Mockito.verify(exec).process(Arrays.asList("docker", "--config", config, "buildx",
"build", "--progress=plain", "--builder", builderName,
"--platform", "linux/amd64,linux/arm64",
"--tag", "user/test:1.0.1", "--push",
"--cache-to=type=local,dest=" + cacheDir, "--cache-from=type=local,src=" + cacheDir), cmds);

Mockito.verify(exec).process(args, cmds);

if (providedBuilder == null) {
Mockito.verify(exec).process(Arrays.asList("docker", "--config", config, "buildx"),
"rm", builderName);
}
}

private static String getOsDependentBuild(Path buildPath, String docker) {
return buildPath.resolve(docker).toString().replace('/', File.separatorChar);
}

private void thenImageHasBeenTagged() throws DockerAccessException {
Mockito.verify(docker).tag(new ImageName(imageName).getFullName(registry), imageName, false);
}
Expand Down Expand Up @@ -406,12 +428,12 @@ private void whenAutoPullImage() {

private void whenPushImage() {
try {
ProjectPaths projectPaths= new ProjectPaths(projectBaseDir, "target/docker");
ProjectPaths projectPaths = new ProjectPaths(projectBaseDir, "target/docker");

RegistryService.RegistryConfig registryConfig =
new RegistryService.RegistryConfig.Builder()
.authConfigFactory(authConfigFactory)
.authConfig(authConfig).build();
new RegistryService.RegistryConfig.Builder()
.authConfigFactory(authConfigFactory)
.authConfig(authConfig).build();
registryService.pushImages(projectPaths, Collections.singleton(imageConfiguration), 1, registryConfig, false);
} catch (Exception e) {
this.actualException = e;
Expand Down Expand Up @@ -447,19 +469,20 @@ private void givenAnImage(String imageName) {
}

private void givenAnImageConfiguration(String imageName) {
givenImageNameAndBuildX(imageName, null, null);
givenImageNameAndBuildX(imageName, null, null, null);
}

private void givenBuildxImageConfiguration(String imageName, String builderName, String dockerFile) {
private void givenBuildxImageConfiguration(String imageName, String builderName, String dockerFile, String tag) {
BuildXConfiguration buildx = new BuildXConfiguration.Builder()
.platforms(Arrays.asList("linux/amd64", "linux/arm64"))
.builderName(builderName)
.build();
givenImageNameAndBuildX(imageName, buildx, dockerFile);
givenImageNameAndBuildX(imageName, buildx, dockerFile, tag);
}

private void givenImageNameAndBuildX(String imageName, BuildXConfiguration buildx, String dockerFile) {
BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder().buildx(buildx).dockerFile(dockerFile).build();
private void givenImageNameAndBuildX(String imageName, BuildXConfiguration buildx, String dockerFile, String tag) {
List<String> tags = tag != null ? Collections.singletonList(tag) : null;
BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder().buildx(buildx).tags(tags).dockerFile(dockerFile).build();
buildImageConfiguration.initAndValidate(logger);
imageConfiguration = new ImageConfiguration.Builder().name(imageName).buildConfig(buildImageConfiguration).build();
}
Expand Down

0 comments on commit f42c906

Please sign in to comment.