diff --git a/doc/changelog.md b/doc/changelog.md index 8b095f7bb..d6b657b0b 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -1,5 +1,9 @@ # ChangeLog * **0.42-SNAPSHOT** : + - Support `docker run --platform` ([1641](https://github.com/fabric8io/docker-maven-plugin/issues/1641)) @chonton + - Update buildx documentation to amplify the build behavior ([1646](https://github.com/fabric8io/docker-maven-plugin/pull/1646)) @chonton + - Default to native platform when creating container ([1645](https://github.com/fabric8io/docker-maven-plugin/pull/1645)) @chonton + - JIB ignores the `DOCKER_REGISTRY` environment variable ([1617](https://github.com/fabric8io/docker-maven-plugin/issues/1617)) @rohanKanojia * **0.41.0** (2023-02-06): - multi-arch build should use provided repository ([1597](https://github.com/fabric8io/docker-maven-plugin/issues/1597)) @merikan diff --git a/src/main/java/io/fabric8/maven/docker/service/JibBuildService.java b/src/main/java/io/fabric8/maven/docker/service/JibBuildService.java index b09834204..bb33ddcd7 100644 --- a/src/main/java/io/fabric8/maven/docker/service/JibBuildService.java +++ b/src/main/java/io/fabric8/maven/docker/service/JibBuildService.java @@ -33,7 +33,6 @@ public class JibBuildService { private static final List DEFAULT_DOCKER_REGISTRIES = Arrays.asList( "docker.io", "index.docker.io", "registry.hub.docker.com" ); - private static final String PUSH_REGISTRY = ".docker.push.registry"; private static final String ARCHIVE_FILE_NAME = "docker-build"; private final Logger log; private final ServiceHub serviceHub; @@ -51,10 +50,10 @@ public void build(String jibImageFormat, ImageConfiguration imageConfig, Registr if (imageConfig.getBuildConfiguration().isDockerFileMode()) { throw new MojoExecutionException("Dockerfile mode is not supported with JIB build strategy"); } - prependRegistry(imageConfig, mojoParameters.getProject().getProperties().getProperty(PUSH_REGISTRY)); + prepareImageConfiguration(imageConfig, registryConfig); BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), mojoParameters); final Credential pullRegistryCredential = getRegistryCredentials( - registryConfig, false, imageConfig, log); + registryConfig, false, imageConfig); final JibContainerBuilder containerBuilder = containerFromImageConfiguration(jibImageFormat, imageConfig, pullRegistryCredential); File dockerTarArchive = getAssemblyTarArchive(imageConfig, serviceHub, mojoParameters, log); @@ -85,11 +84,11 @@ public void build(String jibImageFormat, ImageConfiguration imageConfig, Registr public void push(Collection imageConfigs, int retries, RegistryService.RegistryConfig registryConfig, boolean skipTag) throws MojoExecutionException { try { for (ImageConfiguration imageConfiguration : imageConfigs) { - prependRegistry(imageConfiguration, registryConfig.getRegistry()); + prepareImageConfiguration(imageConfiguration, registryConfig); log.info("This push refers to: %s", imageConfiguration.getName()); JibServiceUtil.jibPush( imageConfiguration, - getRegistryCredentials(registryConfig, true, imageConfiguration, log), + getRegistryCredentials(registryConfig, true, imageConfiguration), getBuildTarArchive(imageConfiguration, mojoParameters), log ); @@ -117,21 +116,14 @@ static File getAssemblyTarArchive(ImageConfiguration imageConfig, ServiceHub ser } static Credential getRegistryCredentials( - RegistryService.RegistryConfig registryConfig, boolean isPush, ImageConfiguration imageConfiguration, Logger log) + RegistryService.RegistryConfig registryConfig, boolean isPush, ImageConfiguration imageConfiguration) throws MojoExecutionException { String registry; if (isPush) { - registry = EnvUtil.firstRegistryOf( - new ImageName(imageConfiguration.getName()).getRegistry(), - imageConfiguration.getRegistry(), - registryConfig.getRegistry() - ); + registry = getConfiguredPushRegistry(registryConfig, imageConfiguration); } else { - registry = EnvUtil.firstRegistryOf( - new ImageName(getBaseImage(imageConfiguration)).getRegistry(), - registryConfig.getRegistry() - ); + registry = getConfiguredPullRegistry(registryConfig, imageConfiguration); } if (registry == null || DEFAULT_DOCKER_REGISTRIES.contains(registry)) { registry = DOCKER_LOGIN_DEFAULT_REGISTRY; // Let's assume docker is default registry. @@ -150,4 +142,25 @@ static File getBuildTarArchive(ImageConfiguration imageConfiguration, MojoParame BuildDirs buildDirs = new BuildDirs(imageConfiguration.getName(), mojoParameters); return new File(buildDirs.getTemporaryRootDirectory(), ARCHIVE_FILE_NAME + "." + ArchiveCompression.none.getFileSuffix()); } + + static ImageConfiguration prepareImageConfiguration(ImageConfiguration imageConfig, RegistryService.RegistryConfig registryConfig) { + String configuredRegistry = getConfiguredPushRegistry(registryConfig, imageConfig); + prependRegistry(imageConfig, configuredRegistry); + return imageConfig; + } + + private static String getConfiguredPushRegistry(RegistryService.RegistryConfig registryConfig, ImageConfiguration imageConfiguration) { + return EnvUtil.firstRegistryOf( + new ImageName(imageConfiguration.getName()).getRegistry(), + imageConfiguration.getRegistry(), + registryConfig.getRegistry() + ); + } + + private static String getConfiguredPullRegistry(RegistryService.RegistryConfig registryConfig, ImageConfiguration imageConfiguration) { + return EnvUtil.firstRegistryOf( + new ImageName(getBaseImage(imageConfiguration)).getRegistry(), + registryConfig.getRegistry() + ); + } } \ No newline at end of file diff --git a/src/main/java/io/fabric8/maven/docker/util/JibServiceUtil.java b/src/main/java/io/fabric8/maven/docker/util/JibServiceUtil.java index 1c8e37593..28d8179ff 100644 --- a/src/main/java/io/fabric8/maven/docker/util/JibServiceUtil.java +++ b/src/main/java/io/fabric8/maven/docker/util/JibServiceUtil.java @@ -204,7 +204,7 @@ static Set getAllImageTags(List tags, String imageName) { } static ImageFormat getImageFormat(String jibImageFormat) { - if (jibImageFormat != null && jibImageFormat.toLowerCase().equalsIgnoreCase("oci")) { + if (jibImageFormat != null && jibImageFormat.equalsIgnoreCase("oci")) { return ImageFormat.OCI; } return ImageFormat.Docker; diff --git a/src/test/java/io/fabric8/maven/docker/service/JibBuildServiceTest.java b/src/test/java/io/fabric8/maven/docker/service/JibBuildServiceTest.java index 1b5178c8f..63b220701 100644 --- a/src/test/java/io/fabric8/maven/docker/service/JibBuildServiceTest.java +++ b/src/test/java/io/fabric8/maven/docker/service/JibBuildServiceTest.java @@ -6,11 +6,10 @@ import java.nio.file.Path; import java.util.Collections; import java.util.List; -import java.util.Properties; import com.google.cloud.tools.jib.api.Credential; -import com.google.cloud.tools.jib.api.TarImage; +import io.fabric8.maven.docker.util.EnvUtil; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; @@ -33,6 +32,10 @@ import io.fabric8.maven.docker.util.Logger; import io.fabric8.maven.docker.util.MojoParameters; +import static io.fabric8.maven.docker.service.JibBuildService.prepareImageConfiguration; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mockStatic; + @ExtendWith(MockitoExtension.class) class JibBuildServiceTest { @@ -70,11 +73,11 @@ void testGetRegistryCredentialsForPush() throws MojoExecutionException { mockAuthConfigFactory(true, registryConfig); // When Credential credential = JibBuildService.getRegistryCredentials( - registryConfig, true, imageConfiguration, logger); + registryConfig, true, imageConfiguration); // Then Assertions.assertNotNull(credential); - Assertions.assertEquals("testuserpush", credential.getUsername()); - Assertions.assertEquals("testpass", credential.getPassword()); + assertEquals("testuserpush", credential.getUsername()); + assertEquals("testpass", credential.getPassword()); } @Test @@ -90,11 +93,11 @@ void testGetRegistryCredentialsForPull() throws MojoExecutionException { mockAuthConfigFactory(false, registryConfig); // When Credential credential = JibBuildService.getRegistryCredentials( - registryConfig, false, imageConfiguration, logger); + registryConfig, false, imageConfiguration); // Then Assertions.assertNotNull(credential); - Assertions.assertEquals("testuserpull", credential.getUsername()); - Assertions.assertEquals("testpass", credential.getPassword()); + assertEquals("testuserpull", credential.getUsername()); + assertEquals("testpass", credential.getPassword()); } @Test @@ -109,7 +112,7 @@ void testGetBuildTarArchive() throws IOException { // Then Assertions.assertNotNull(tarArchive); - Assertions.assertEquals(new File("/target/test/testimage/0.0.1/tmp/docker-build.tar").getPath(), + assertEquals(new File("/target/test/testimage/0.0.1/tmp/docker-build.tar").getPath(), tarArchive.getAbsolutePath().substring(projectBaseDir.getAbsolutePath().length())); } @@ -126,7 +129,7 @@ void testGetAssemblyTarArchive() throws IOException, MojoExecutionException { // Then Assertions.assertNotNull(tarArchive); - Assertions.assertEquals(new File("/target/test/testimage/0.0.1/tmp/docker-build.tar").getPath(), + assertEquals(new File("/target/test/testimage/0.0.1/tmp/docker-build.tar").getPath(), tarArchive.getAbsolutePath().substring(projectBaseDir.toString().length())); } @@ -138,12 +141,12 @@ void testPrependRegistry() { JibBuildService.prependRegistry(imageConfiguration, "quay.io"); // Then Assertions.assertNotNull(imageConfiguration); - Assertions.assertEquals("quay.io/test/testimage:0.0.1", imageConfiguration.getName()); + assertEquals("quay.io/test/testimage:0.0.1", imageConfiguration.getName()); } @Test void testPushWithNoConfigurations() { - try (MockedStatic jibServiceUtilMock = Mockito.mockStatic(JibServiceUtil.class)) { + try (MockedStatic jibServiceUtilMock = mockStatic(JibServiceUtil.class)) { // Given jibServiceUtilMock .when(() -> JibServiceUtil.jibPush(Mockito.any(ImageConfiguration.class), Mockito.any(Credential.class), Mockito.any(File.class), Mockito.any(Logger.class))) @@ -159,7 +162,6 @@ void testPushWithNoConfigurations() { @Test void testBuildCallsBuildContainer(@TempDir Path tmpDir) throws Exception { // ARRANGE - Mockito.when(project.getProperties()).thenReturn(new Properties()); setupServiceHubExpectations(tmpDir.toFile()); setupDockerAssemblyExpectations(tmpDir); final RegistryService.RegistryConfig registryConfig = new RegistryService.RegistryConfig.Builder() @@ -170,7 +172,7 @@ void testBuildCallsBuildContainer(@TempDir Path tmpDir) throws Exception { JibBuildService jibBuildService = new JibBuildService(serviceHub, params, logger); ImageConfiguration imageConfiguration = getImageConfiguration(); - try (MockedStatic jibServiceUtilMock = Mockito.mockStatic(JibServiceUtil.class)) { + try (MockedStatic jibServiceUtilMock = mockStatic(JibServiceUtil.class)) { jibServiceUtilMock.when(() -> JibServiceUtil.getBaseImage(imageConfiguration)).thenCallRealMethod(); // ACT jibBuildService.build("docker", imageConfiguration, registryConfig); @@ -183,7 +185,7 @@ void testBuildCallsBuildContainer(@TempDir Path tmpDir) throws Exception { @Test @Disabled("Cannot intercept JibServiceUtil.pushImage() to prevent actual image creation") void testPushWithConfiguration(@TempDir Path tmpDir) throws Exception { - try (MockedStatic jibServiceUtilMock = Mockito.mockStatic(JibServiceUtil.class)) { + try (MockedStatic jibServiceUtilMock = mockStatic(JibServiceUtil.class)) { // Given setupServiceHubExpectations(tmpDir.toFile()); setupDockerAssemblyExpectations(tmpDir); @@ -204,6 +206,24 @@ void testPushWithConfiguration(@TempDir Path tmpDir) throws Exception { } } + @Test + void prepareImageConfiguration_whenDockerRegistryPropertySet_thenImageConfigurationShouldHaveRegistry() { + try (MockedStatic envUtilMockedStatic = mockStatic(EnvUtil.class)) { + // Given + envUtilMockedStatic.when(() -> EnvUtil.firstRegistryOf(null, null, null)).thenReturn("registry-from-docker-registry-env.io"); + ImageConfiguration imageConfiguration = new ImageConfiguration.Builder() + .name("test/foo:bar") + .build(); + RegistryService.RegistryConfig registryConfig = new RegistryService.RegistryConfig(); + + // When + ImageConfiguration result = prepareImageConfiguration(imageConfiguration, registryConfig); + + // Then + assertEquals("registry-from-docker-registry-env.io/test/foo:bar", result.getName()); + } + } + private ImageConfiguration getImageConfiguration() { return new ImageConfiguration.Builder() .name("test/testimage:0.0.1")