Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Remove unneeded executors + structure build code
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa authored and Devang Gaur committed Feb 14, 2020
1 parent a42e723 commit 66393d7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 137 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Feature #1483: Dekorate integration
* Fix #1769: Display root cause of failure if available
* Fix #1776: fat jar wrongly detected on simple jar
* Refator #1766: Jib Refactor
* Revert #1738 #1737: Revert fixes
* Refactor #1766: Jib Refactor

### 4.3.1 (18-10-2019)
* Updated Kubernetes client to 4.6.1
Expand All @@ -35,6 +36,8 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Fix #1732: Correctly replacing template placeholders in helm chart
* Fix #1734: Add logging during jib builds
* Fix #1696: Fix put operations for ImageStreams in ApplyService
* Fix #1738: JibBuildConfiguration ignores BuildImageConfiguration.registry
* Fix #1737: Wrong Jib output directory in Maven multi-module build

### 4.3.0 (04-10-2019)
* Updated custom-enricher sample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@
*/
package io.fabric8.maven.core.service.kubernetes.jib;

import com.google.cloud.tools.jib.api.ImageReference;
import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
import com.google.cloud.tools.jib.api.TarImage;
import io.fabric8.maven.core.service.BuildService;
import io.fabric8.maven.core.service.Fabric8ServiceException;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.Logger;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;

@Component(role = JibBuildService.class)
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.addAssemblyFiles;
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.buildContainer;
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.containerFromImageConfiguration;
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.imageNameFromImageConfiguration;

public class JibBuildService implements BuildService {

@Requirement
JibAssemblyManager jibAssemblyManager;
private static final String TAR_SUFFIX = ".tar";

private JibAssemblyManager jibAssemblyManager;

private BuildServiceConfig config;

Expand All @@ -44,12 +56,32 @@ public JibBuildService(BuildServiceConfig config, JibAssemblyManager jibAssembly
@Override
public void build(ImageConfiguration imageConfiguration) throws Fabric8ServiceException {
try {
JibServiceUtil.buildImage(config.getDockerMojoParameters(), imageConfiguration, jibAssemblyManager, log);
log.info("JIB image build started");
final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfiguration);
log.info("Preparing assembly files");
final TarImage tarImage = prepareAssembly(imageConfiguration, containerBuilder);
buildContainer(containerBuilder, tarImage, log);
log.info(" %s successfully built", imageNameFromImageConfiguration(imageConfiguration));
} catch (Exception ex) {
throw new Fabric8ServiceException("Error when building JIB image", ex);
}
}

private TarImage prepareAssembly(ImageConfiguration imageConfiguration, JibContainerBuilder containerBuilder)
throws InvalidImageReferenceException, MojoExecutionException, IOException {

final String targetImage = imageNameFromImageConfiguration(imageConfiguration);
final MojoParameters mojoParameters = config.getDockerMojoParameters();
final String outputDir = EnvUtil.prepareAbsoluteOutputDirPath(mojoParameters, "", "").getAbsolutePath();
addAssemblyFiles(containerBuilder, jibAssemblyManager,
imageConfiguration.getBuildConfiguration().getAssemblyConfiguration(),
mojoParameters, targetImage, log);

final String imageTarName = ImageReference.parse(targetImage).toString().concat(TAR_SUFFIX);
log.info("Building Image Tarball at %s ...", imageTarName);
return TarImage.at(Paths.get(outputDir, imageTarName)).named(targetImage);
}

@Override
public void postProcess(BuildServiceConfig config) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.cloud.tools.jib.api.Containerizer;
import com.google.cloud.tools.jib.api.Credential;
import com.google.cloud.tools.jib.api.ImageFormat;
import com.google.cloud.tools.jib.api.ImageReference;
import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
import com.google.cloud.tools.jib.api.Jib;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
Expand All @@ -33,10 +32,7 @@
import com.google.cloud.tools.jib.event.events.TimerEvent;
import com.google.cloud.tools.jib.event.progress.ProgressEventHandler;
import com.google.cloud.tools.jib.plugins.common.TimerEventHandler;
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLoggerBuilder;
import com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator;
import com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor;
import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.config.Arguments;
import io.fabric8.maven.docker.config.AssemblyConfiguration;
Expand All @@ -51,14 +47,14 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -69,44 +65,13 @@ public class JibServiceUtil {

private JibServiceUtil() {}

private static ConsoleLogger consoleLogger;
private static final String TAR_SUFFIX = ".tar";
private static final long THREAD_EXECUTOR_TIMEOUT_SECONDS = 60;
private static final String DOCKER_REGISTRY = "docker.io";
private static final String BUSYBOX = "busybox:latest";
/**
* Builds a container image using JIB
*/
static void buildImage(
MojoParameters mojoParameters, ImageConfiguration imageConfiguration, JibAssemblyManager jibAssemblyManager, Logger log)
throws InvalidImageReferenceException, MojoExecutionException, IOException {

final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfiguration);
final String targetImage = imageNameFromImageConfiguration(imageConfiguration);
final String outputDir = EnvUtil.prepareAbsoluteOutputDirPath(mojoParameters, "", "").getAbsolutePath();
addAssemblyFiles(containerBuilder, jibAssemblyManager,
imageConfiguration.getBuildConfiguration().getAssemblyConfiguration(),
mojoParameters, targetImage, log);

String imageTarName = ImageReference.parse(targetImage).toString().concat(TAR_SUFFIX);
TarImage tarImage = TarImage.at(Paths.get(outputDir, imageTarName)).named(targetImage);

log.info("Building Image Tarball at %s ...", imageTarName);

buildContainer(containerBuilder, tarImage, log);

log.info(" %s successfully built.", Paths.get(outputDir, imageTarName));
}

private static void addAssemblyFiles(JibContainerBuilder jibContainerBuilder, JibAssemblyManager jibAssemblyManager, AssemblyConfiguration assemblyConfiguration,
static void addAssemblyFiles(JibContainerBuilder jibContainerBuilder, JibAssemblyManager jibAssemblyManager, AssemblyConfiguration assemblyConfiguration,
MojoParameters mojoParameters, String imageName, Logger log) throws MojoExecutionException, IOException {

if (hasAssemblyConfiguration(assemblyConfiguration)) {

if (assemblyConfiguration.getUser() != null && !assemblyConfiguration.getUser().isEmpty()) {
jibContainerBuilder.setUser(assemblyConfiguration.getUser());
}

JibAssemblyManager.BuildDirs buildDirs = createBuildDirs(imageName, mojoParameters);
JibAssemblyConfigurationSource source =
new JibAssemblyConfigurationSource(mojoParameters, buildDirs, assemblyConfiguration);
Expand Down Expand Up @@ -134,7 +99,6 @@ private static void addAssemblyFiles(JibContainerBuilder jibContainerBuilder, Ji
}
}


private static JibAssemblyManager.BuildDirs createBuildDirs(String imageName, MojoParameters params) {
JibAssemblyManager.BuildDirs buildDirs = new JibAssemblyManager.BuildDirs(imageName, params);
buildDirs.createDirs();
Expand All @@ -149,52 +113,22 @@ private static boolean hasAssemblyConfiguration(AssemblyConfiguration assemblyCo
assemblyConfig.getDescriptorRef() != null);
}



static void buildContainer(JibContainerBuilder jibContainerBuilder, TarImage image, Logger logger) {
SingleThreadedExecutor singleThreadedExecutor = new SingleThreadedExecutor();
try {

consoleLogger = getConsoleLogger(logger, singleThreadedExecutor);
jibContainerBuilder.setCreationTime(Instant.now());

jibContainerBuilder.containerize(Containerizer.to(image)
.addEventHandler(LogEvent.class, JibServiceUtil::log)
.addEventHandler(TimerEvent.class,
new TimerEventHandler(message -> consoleLogger.log(LogEvent.Level.DEBUG, message)))
.addEventHandler(ProgressEvent.class,
new ProgressEventHandler(
update -> consoleLogger.setFooter(
ProgressDisplayGenerator.generateProgressDisplay(
update.getProgress(), update.getUnfinishedLeafTasks())))));

singleThreadedExecutor.shutDownAndAwaitTermination(Duration.ofSeconds(THREAD_EXECUTOR_TIMEOUT_SECONDS));
} catch (CacheDirectoryCreationException | IOException | InterruptedException | ExecutionException | RegistryException ex) {

singleThreadedExecutor.shutDownAndAwaitTermination(Duration.ofSeconds(THREAD_EXECUTOR_TIMEOUT_SECONDS));

jibContainerBuilder.containerize(Containerizer.to(image)
.addEventHandler(LogEvent.class, log(logger))
.addEventHandler(TimerEvent.class, new TimerEventHandler(logger::debug))
.addEventHandler(ProgressEvent.class, new ProgressEventHandler(logUpdate(logger))));
} catch (CacheDirectoryCreationException | IOException | ExecutionException | RegistryException ex) {
logger.error("Unable to build the image tarball: ", ex);
throw new IllegalStateException(ex);
} catch (InterruptedException ex) {
logger.error("Thread interrupted", ex);
Thread.currentThread().interrupt();
}
}

private static void log(LogEvent event) {
consoleLogger.log(event.getLevel(), event.getMessage());
}

private static ConsoleLogger getConsoleLogger(Logger logger, SingleThreadedExecutor executor) {
ConsoleLoggerBuilder consoleLoggerBuilder = ConsoleLoggerBuilder
.rich(executor, true)
.progress(logger::info)
.lifecycle(logger::info);
if (logger.isDebugEnabled()) {
consoleLoggerBuilder
.debug(logger::debug)
.info(logger::info);
}
return consoleLoggerBuilder.build();
}

static JibContainerBuilder containerFromImageConfiguration(ImageConfiguration imageConfiguration)
throws InvalidImageReferenceException {

Expand Down Expand Up @@ -240,26 +174,18 @@ static String imageNameFromImageConfiguration(ImageConfiguration imageConfigurat
* @param targetImageName Full name of the target Image to be pushed to the registry
* @param credential
* @param logger
* @throws InvalidImageReferenceException
*/
public static void pushImage(TarImage baseImage, String targetImageName, Credential credential, SingleThreadedExecutor executor, Logger logger) throws IllegalStateException {
public static void pushImage(TarImage baseImage, String targetImageName, Credential credential, Logger logger) {
try {
RegistryImage targetImage = RegistryImage.named(targetImageName);
consoleLogger = getConsoleLogger(logger, executor);
if (credential!= null && !credential.getUsername().isEmpty() && !credential.getPassword().isEmpty()) {
targetImage.addCredential(credential.getUsername(), credential.getPassword());
}

Jib.from(baseImage).containerize(Containerizer.to(targetImage)
.addEventHandler(LogEvent.class, JibServiceUtil::log)
.addEventHandler(TimerEvent.class,
new TimerEventHandler(message -> consoleLogger.log(LogEvent.Level.ERROR, message)))
.addEventHandler(ProgressEvent.class,
new ProgressEventHandler(
update -> consoleLogger.setFooter(
ProgressDisplayGenerator.generateProgressDisplay(
update.getProgress(), update.getUnfinishedLeafTasks())))));

.addEventHandler(LogEvent.class, log(logger))
.addEventHandler(TimerEvent.class, new TimerEventHandler(logger::error))
.addEventHandler(ProgressEvent.class, new ProgressEventHandler(logUpdate(logger))));
} catch (RegistryException | CacheDirectoryCreationException | InvalidImageReferenceException | IOException | ExecutionException | InterruptedException e) {
logger.error("Exception occured while pushing the image: %s", targetImageName);
throw new IllegalStateException(e.getMessage(), e);
Expand All @@ -281,6 +207,27 @@ public static Credential getRegistryCredentials(String registry, RegistryService
return null;
}

private static Consumer<LogEvent> log(Logger logger) {
return logEvent -> ((Function<LogEvent, Consumer<String>>)(le -> {
switch(le.getLevel()) {
case ERROR:
return logger::error;
case WARN:
return logger::warn;
case INFO:
return logger::info;
default:
return logger::debug;
}
})).apply(logEvent).accept(logEvent.getMessage());
}

private static Consumer<ProgressEventHandler.Update> logUpdate(Logger logger) {
return update ->
ProgressDisplayGenerator.generateProgressDisplay(update.getProgress(), update.getUnfinishedLeafTasks())
.forEach(logger::info);
}

static String getBaseImage(ImageConfiguration imageConfiguration) {
return Optional.ofNullable(imageConfiguration)
.map(ImageConfiguration::getBuildConfiguration)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fabric8.maven.generator.javaexec; /**
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
Expand All @@ -13,6 +13,7 @@
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.generator.javaexec;

import java.io.File;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fabric8.maven.generator.javaexec; /**
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
Expand All @@ -13,6 +13,7 @@
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.generator.javaexec;

import java.io.File;
import java.net.URL;
Expand Down
Loading

0 comments on commit 66393d7

Please sign in to comment.