diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildService.java index e80bd83285..1efe34d86e 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildService.java @@ -78,22 +78,22 @@ public boolean isApplicable() { } @Override - public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeServiceException { + public void buildSingleImage(ImageConfiguration imageConfiguration) throws JKubeServiceException { try { kitLogger.info("[[B]]JIB[[B]] image build started"); - if (imageConfig.getBuildConfiguration().isDockerFileMode()) { + if (imageConfiguration.getBuildConfiguration().isDockerFileMode()) { throw new JKubeServiceException("Dockerfile mode is not supported with JIB build strategy"); } - prependRegistry(imageConfig, getPushRegistry(imageConfig, configuration.getRegistryConfig())); - BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), configuration); - String pullRegistry = getPullRegistry(imageConfig, configuration.getRegistryConfig()); + final ImageConfiguration imageConfigToBuild = prependPushRegistry(imageConfiguration, configuration.getRegistryConfig()); + final BuildDirs buildDirs = new BuildDirs(imageConfigToBuild.getName(), configuration); + final String pullRegistry = getPullRegistry(imageConfigToBuild, configuration.getRegistryConfig()); final Credential pullRegistryCredential = getRegistryCredentials( configuration.getRegistryConfig(), false, pullRegistry); - final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfig, pullRegistry, pullRegistryCredential); + final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfigToBuild, pullRegistry, pullRegistryCredential); final Map> layers = AssemblyManager.getInstance() .copyFilesToFinalTarballDirectory(configuration, buildDirs, - AssemblyManager.getAssemblyConfiguration(imageConfig.getBuildConfiguration(), configuration)); + AssemblyManager.getAssemblyConfiguration(imageConfigToBuild.getBuildConfiguration(), configuration)); JibServiceUtil.layers(buildDirs, layers).forEach(containerBuilder::addFileEntriesLayer); // TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared @@ -101,9 +101,9 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService // files should be added using the AssemblyFileEntry list. AssemblyManager, should provide // a common way to achieve this so that both the tar builder and any other builder could get a hold of // archive customizers, file entries, etc. - File dockerTarArchive = getAssemblyTarArchive(imageConfig, configuration, kitLogger); + File dockerTarArchive = getAssemblyTarArchive(imageConfigToBuild, configuration, kitLogger); JibServiceUtil.buildContainer(containerBuilder, - TarImage.at(dockerTarArchive.toPath()).named(imageConfig.getName()), jibLogger); + TarImage.at(dockerTarArchive.toPath()).named(imageConfigToBuild.getName()), jibLogger); kitLogger.info(" %s successfully built", dockerTarArchive.getAbsolutePath()); } catch (Exception ex) { throw new JKubeServiceException("Error when building JIB image", ex); @@ -113,14 +113,13 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService @Override protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException { try { - final String pushRegistry = getPushRegistry(imageConfiguration, registryConfig); - prependRegistry(imageConfiguration, pushRegistry); - kitLogger.info("This push refers to: %s", imageConfiguration.getName()); - kitLogger.info("Pushing image: %s", new ImageName(imageConfiguration.getName()).getFullName()); + final ImageConfiguration imageConfigToPush = prependPushRegistry(imageConfiguration, registryConfig); + kitLogger.info("This push refers to: %s", imageConfigToPush.getName()); + kitLogger.info("Pushing image: %s", new ImageName(imageConfigToPush.getName()).getFullName()); JibServiceUtil.jibPush( - imageConfiguration, - getRegistryCredentials(registryConfig, true, pushRegistry), - getBuildTarArchive(imageConfiguration, configuration), + imageConfigToPush, + getRegistryCredentials(registryConfig, true, getPushRegistry(imageConfigToPush, registryConfig)), + getBuildTarArchive(imageConfigToPush, configuration), jibLogger ); } catch (Exception ex) { @@ -133,13 +132,15 @@ public void postProcess() { // No post processing required } - static ImageConfiguration prependRegistry(ImageConfiguration imageConfiguration, String registry) { - ImageName imageName = new ImageName(imageConfiguration.getName()); - if (!imageName.hasRegistry() && registry != null) { - imageConfiguration.setName(imageName.getFullName(registry)); - imageConfiguration.setRegistry(registry); + static ImageConfiguration prependPushRegistry(ImageConfiguration imageConfiguration, RegistryConfig registryConfig) { + final ImageConfiguration.ImageConfigurationBuilder icBuilder = imageConfiguration.toBuilder(); + final ImageName imageName = new ImageName(imageConfiguration.getName()); + final String pushRegistry = getPushRegistry(imageConfiguration, registryConfig); + if (!imageName.hasRegistry() && pushRegistry != null) { + icBuilder.name(imageName.getFullName(pushRegistry)); + icBuilder.registry(pushRegistry); } - return imageConfiguration; + return icBuilder.build(); } static File getAssemblyTarArchive(ImageConfiguration imageConfig, JKubeConfiguration configuration, KitLogger log) throws IOException { diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildServiceTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildServiceTest.java index eac5fcf939..fa3b633336 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildServiceTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibBuildServiceTest.java @@ -19,6 +19,7 @@ import java.nio.file.Path; import java.util.Collections; +import com.google.cloud.tools.jib.api.JibContainerBuilder; import org.eclipse.jkube.kit.common.JKubeConfiguration; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; @@ -40,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; @@ -51,6 +53,7 @@ import static org.mockito.Mockito.when; class JibBuildServiceTest { + @TempDir Path temporaryFolder; @@ -80,8 +83,10 @@ void setUp() { .authConfig(Collections.emptyMap()) .settings(Collections.emptyList()) .build(); - jibServiceUtilMockedStatic.when(() -> JibServiceUtil.getBaseImage(eq(imageConfiguration), isNull())) + jibServiceUtilMockedStatic.when(() -> JibServiceUtil.getBaseImage(argThat(ic -> ic.getBuild().getFrom().equals("busybox")), isNull())) .thenReturn("busybox"); + jibServiceUtilMockedStatic.when(() -> JibServiceUtil.containerFromImageConfiguration(any(), any(), any())) + .thenReturn(mock(JibContainerBuilder.class, RETURNS_DEEP_STUBS)); } @AfterEach @@ -166,15 +171,6 @@ void getAssemblyTarArchive() throws IOException { .resolve("tmp").resolve("docker-build.tar").toFile()); } - @Test - void prependRegistry_prependsRegistryToTargetImageName() { - // When - JibBuildService.prependRegistry(imageConfiguration, "quay.io"); - // Then - assertThat(imageConfiguration).isNotNull() - .hasFieldOrPropertyWithValue("name", "quay.io/test/testimage:0.0.1"); - } - @Test void pushWithNoConfigurations() throws Exception { // When @@ -252,6 +248,20 @@ void build_shouldCallPluginServiceAddFiles() throws JKubeServiceException { verify(mockedServiceHub.getPluginManager().resolvePluginService(), times(1)).addExtraFiles(); } + @Test + void build_withRegistryConfig_shouldPrependRegistryToImageName() throws JKubeServiceException { + // Given + when(mockedServiceHub.getConfiguration().getRegistryConfig()) + .thenReturn(RegistryConfig.builder().registry("quay.io").settings(Collections.emptyList()).build()); + when(mockedServiceHub.getConfiguration().getProject()) + .thenReturn(JavaProject.builder().baseDirectory(temporaryFolder.toFile()).build()); + // When + new JibBuildService(mockedServiceHub).build(imageConfiguration); + // Then + jibServiceUtilMockedStatic.verify(() -> JibServiceUtil + .containerFromImageConfiguration(argThat(ic -> ic.getName().equals("quay.io/test/testimage:0.0.1")), any(), any()), times(1)); + } + private static JKubeConfiguration createJKubeConfiguration(File projectBaseDir) { return JKubeConfiguration.builder() .outputDirectory("target")