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

Commit

Permalink
Corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Devang Gaur committed Jul 23, 2019
1 parent bab7b12 commit 16c2af4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.fabric8.maven.core.service.kubernetes;

import com.google.cloud.tools.jib.api.Credential;
import com.google.cloud.tools.jib.api.Port;
import io.fabric8.maven.core.util.FatJarDetector;

import io.fabric8.maven.docker.config.Arguments;
import io.fabric8.maven.docker.util.DeepCopy;
import org.apache.maven.project.MavenProject;
import io.fabric8.maven.docker.util.Logger;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -19,14 +18,10 @@ public class JibBuildConfiguration {

private List<String> ports;

private List<Path> dependencyList;

private String from;

private String target;

private String pushRegistry;

private Path fatJarPath;

private Arguments entrypoint;
Expand All @@ -35,10 +30,7 @@ public class JibBuildConfiguration {

private String outputDir;

private String tarImageName;

private JibBuildConfiguration() {
}
private JibBuildConfiguration() {}

public Arguments getEntryPoint() {return entrypoint;}

Expand All @@ -56,24 +48,21 @@ public Map<String, String> getEnvMap() {

public String getFrom() {return from;}

public String getPushRegistry() {return pushRegistry;}

public String getTargetImage() {return target;}

public List<Path> getDependencyList() {return dependencyList;}

public Path getFatJar() {return fatJarPath;}

public String getTarImageName() {return tarImageName;}

public static class Builder {
private final JibBuildConfiguration configutil;
private final Logger logger;

public Builder() {
this(null);
public Builder(Logger logger) {
this(null, logger);
}

public Builder(JibBuildConfiguration that) {
public Builder(JibBuildConfiguration that, Logger logger) {
this.logger = logger;
if (that == null) {
this.configutil = new JibBuildConfiguration();
} else {
Expand All @@ -96,11 +85,6 @@ public Builder ports(List<String> ports) {
return this;
}

public Builder depList(List<Path> dependencyList) {
configutil.dependencyList = dependencyList;
return this;
}

public Builder from(String from) {
configutil.from = from;
return this;
Expand All @@ -116,13 +100,8 @@ public Builder entrypoint(Arguments entrypoint) {
return this;
}

public Builder pushRegistry(String pushRegistry) {
configutil.pushRegistry = pushRegistry;
return this;
}

public Builder buildDirectory(String buildDir) {
configutil.fatJarPath = JibBuildServiceUtil.getFatJar(buildDir);
configutil.fatJarPath = JibBuildServiceUtil.getFatJar(buildDir, logger);
return this;
}

Expand All @@ -136,10 +115,6 @@ public Builder outputDir(String outputDir) {
return this;
}

public Builder tarImageName(String tarImageName) {
configutil.tarImageName = tarImageName;
return this;
}
public JibBuildConfiguration build() {
return configutil;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void build(ImageConfiguration imageConfiguration) {
} else {
fullName = new ImageName(imageConfiguration.getName(), null).getFullName();
}
log.debug("Target Image creation and tagging succesfull!");
jibBuildConfiguration = JibBuildServiceUtil.getJibBuildConfiguration(config, imageConfiguration, fullName);

jibBuildConfiguration = JibBuildServiceUtil.getJibBuildConfiguration(config, imageConfiguration, fullName, log);
JibBuildServiceUtil.buildImage(jibBuildConfiguration, log);
} catch (Exception ex) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.cloud.tools.jib.api.CacheDirectoryCreationException;
import com.google.cloud.tools.jib.api.Containerizer;
import com.google.cloud.tools.jib.api.Credential;
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.JibContainer;
Expand All @@ -21,6 +22,7 @@
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.RegistryService;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -50,12 +52,16 @@ public static void buildImage(JibBuildConfiguration buildConfiguration, Logger l
Map<String, String> envMap = buildConfiguration.getEnvMap();
List<String> portList = buildConfiguration.getPorts();
Set<Port> portSet = getPortSet(portList);
String outputDir = buildConfiguration.getOutputDir();
String targetDir = buildConfiguration.getTargetDir();
Path fatJar = buildConfiguration.getFatJar();

List<String> entrypointList = new ArrayList<>();
if(buildConfiguration.getEntryPoint() != null) {
entrypointList = buildConfiguration.getEntryPoint().asStrings();
}
buildImage(fromImage, targetImage, envMap, credential, portSet, buildConfiguration.getFatJar(), entrypointList, buildConfiguration.getTargetDir(), buildConfiguration.getOutputDir(), log, buildConfiguration.getTarImageName());

buildImage(fromImage, targetImage, envMap, credential, portSet, fatJar, entrypointList, targetDir, outputDir, log);
}

private static Set<Port> getPortSet(List<String> ports) {
Expand All @@ -68,23 +74,25 @@ private static Set<Port> getPortSet(List<String> ports) {
return portSet;
}

protected static JibContainer buildImage(String baseImage, String targetImage, Map<String, String> envMap, Credential credential, Set<Port> portSet, Path fatJar, List<String> entrypointList, String targetDir, String outputDir, Logger log, String tarImageName) throws InvalidImageReferenceException {
protected static JibContainer buildImage(String baseImage, String targetImage, Map<String, String> envMap, Credential credential, Set<Port> portSet, Path fatJar, List<String> entrypointList, String targetDir, String outputDir, Logger log) throws InvalidImageReferenceException {

String username = "";
String password = "";
String fatJarName = fatJar.getFileName().toString();
String imageTarName = ImageReference.parse(targetImage).getRepository().concat(".tar");

JibContainerBuilder contBuild = Jib.from(baseImage);

if (!envMap.isEmpty()) {
contBuild = contBuild.setEnvironment(envMap);
}

if (!portSet.isEmpty()) {

contBuild = contBuild.setExposedPorts(portSet);
}


if (fatJar != null) {
String fatJarName = fatJar.getFileName().toString();
String jarPath = targetDir + "/" + (fatJarName.isEmpty() ? DEFAULT_JAR_NAME: fatJarName);
contBuild = contBuild
.addLayer(LayerConfiguration.builder().addEntry(fatJar, AbsoluteUnixPath.get(jarPath)).build());
Expand All @@ -100,29 +108,31 @@ protected static JibContainer buildImage(String baseImage, String targetImage, M
}

RegistryImage registryImage = RegistryImage.named(targetImage);
TarImage tarImage = TarImage.named(targetImage).saveTo(Paths.get(outputDir + "/" + tarImageName));
TarImage tarImage = TarImage.named(targetImage).saveTo(Paths.get(outputDir + "/" + imageTarName));

try {
Containerizer containerizer = Containerizer.to(registryImage.addCredential(username, password));
JibContainer jibRegistryImageContainer = contBuild.containerize(containerizer);
log.info("Image %s successfully built and pushed.", targetImage);
return jibRegistryImageContainer;
} catch (Exception ex) {
if(ex instanceof RegistryAuthenticationFailedException) {
log.warn("Credentials are either not configured or incorrect credentials are configured");
try {
JibContainer jibTarImageContainer = contBuild.containerize(Containerizer.to(tarImage));
log.info("Image %s successfully built.", tarImage);
return jibTarImageContainer;
} catch (Exception e) {
throw new IllegalStateException(e);
}
} catch (RegistryException re) {
log.warn("Registry Exception occured : %s", re.getMessage());
log.warn("Credentials are probably either not configured or are incorrect.");
log.info("Building Image Tarball at %s." + imageTarName);
try {
JibContainer jibTarImageContainer = contBuild.containerize(Containerizer.to(tarImage));
log.info(" %s successfully built.", tarImage);
return jibTarImageContainer;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}

public static JibBuildConfiguration getJibBuildConfiguration(BuildService.BuildServiceConfig config, ImageConfiguration imageConfiguration, String fullImageName) throws MojoExecutionException {
public static JibBuildConfiguration getJibBuildConfiguration(BuildService.BuildServiceConfig config, ImageConfiguration imageConfiguration, String fullImageName, Logger log) throws MojoExecutionException {
BuildImageConfiguration buildImageConfiguration = imageConfiguration.getBuildConfiguration();

RegistryService.RegistryConfig registryConfig = config.getDockerBuildContext().getRegistryConfig();
Expand All @@ -131,37 +141,31 @@ public static JibBuildConfiguration getJibBuildConfiguration(BuildService.BuildS

String outputDir = config.getDockerMojoParameters().getOutputDirectory();

MavenProject project = config.getDockerMojoParameters().getProject();

if(targetDir == null) {
targetDir = "/deployments";
}

String tarImageName = project.getArtifactId() + "-" + project.getVersion() + ".tar";
AuthConfig authConfig = registryConfig.getAuthConfigFactory()
.createAuthConfig(true, true, registryConfig.getAuthConfig(),
registryConfig.getSettings(), null, registryConfig.getRegistry());


JibBuildConfiguration.Builder jibBuildConfigurationBuilder = new JibBuildConfiguration.Builder().from(buildImageConfiguration.getFrom())
JibBuildConfiguration.Builder jibBuildConfigurationBuilder = new JibBuildConfiguration.Builder(log).from(buildImageConfiguration.getFrom())
.envMap(buildImageConfiguration.getEnv())
.ports(buildImageConfiguration.getPorts())
.entrypoint(buildImageConfiguration.getEntryPoint())
.targetImage(fullImageName)
.pushRegistry(registryConfig.getRegistry())
.targetDir(targetDir)
.outputDir(outputDir)
.buildDirectory(config.getBuildDirectory())
.tarImageName(tarImageName);

.buildDirectory(config.getBuildDirectory());
if(authConfig != null) {
jibBuildConfigurationBuilder.credential(Credential.from(authConfig.getUsername(), authConfig.getPassword()));
}

return jibBuildConfigurationBuilder.build();
}

public static Path getFatJar(String buildDir) {
public static Path getFatJar(String buildDir, Logger log) {
FatJarDetector fatJarDetector = new FatJarDetector(buildDir);
try {
FatJarDetector.Result result = fatJarDetector.scan();
Expand All @@ -170,7 +174,7 @@ public static Path getFatJar(String buildDir) {
}

} catch (MojoExecutionException e) {
// TODO log.err("MOJO EXEC EXCEPTION!")
log.error("MOJO Execution exception occured: %s", e);
throw new UnsupportedOperationException();
}
return null;
Expand Down

0 comments on commit 16c2af4

Please sign in to comment.