Skip to content

Commit

Permalink
refactor: further changes in JibBuildService to support multiplatform…
Browse files Browse the repository at this point in the history
… build (eclipse-jkube#3061)

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored May 16, 2024
1 parent c1a4ffd commit f93b33b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,32 @@ 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<Assembly, List<AssemblyFileEntry>> 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
// the call to AssemblyManager.getInstance().createDockerTarArchive should not be necessary,
// 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);
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -51,6 +53,7 @@
import static org.mockito.Mockito.when;

class JibBuildServiceTest {

@TempDir
Path temporaryFolder;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f93b33b

Please sign in to comment.