use of com.google.cloud.tools.jib.api.InvalidImageReferenceException in project jkube by eclipse.
the class JibServiceUtil method pushImage.
private static void pushImage(TarImage baseImage, String targetImageName, Credential credential, KitLogger logger) throws InterruptedException {
final ExecutorService jibBuildExecutor = Executors.newCachedThreadPool();
try {
submitPushToJib(baseImage, getRegistryImage(targetImageName, credential), jibBuildExecutor, logger);
} catch (RegistryException | CacheDirectoryCreationException | InvalidImageReferenceException | IOException | ExecutionException e) {
logger.error("Exception occurred while pushing the image: %s, %s", targetImageName, e.getMessage());
throw new IllegalStateException(e.getMessage(), e);
} catch (InterruptedException ex) {
logger.error("Thread interrupted", ex);
throw ex;
} finally {
jibBuildExecutor.shutdown();
jibBuildExecutor.awaitTermination(JIB_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
}
use of com.google.cloud.tools.jib.api.InvalidImageReferenceException in project quarkus by quarkusio.
the class JibProcessor method createContainerBuilderFromNative.
private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig, ContainerImageConfig containerImageConfig, NativeImageBuildItem nativeImageBuildItem, List<ContainerImageLabelBuildItem> containerImageLabels) {
List<String> entrypoint;
if (jibConfig.nativeEntrypoint.isPresent()) {
entrypoint = jibConfig.nativeEntrypoint.get();
} else {
List<String> nativeArguments = jibConfig.nativeArguments.orElse(Collections.emptyList());
entrypoint = new ArrayList<>(nativeArguments.size() + 1);
entrypoint.add("./" + BINARY_NAME_IN_CONTAINER);
entrypoint.addAll(nativeArguments);
}
try {
AbsoluteUnixPath workDirInContainer = AbsoluteUnixPath.get("/work");
JibContainerBuilder jibContainerBuilder = Jib.from(toRegistryImage(ImageReference.parse(jibConfig.baseNativeImage), jibConfig.baseRegistryUsername, jibConfig.baseRegistryPassword)).addFileEntriesLayer(FileEntriesLayer.builder().addEntry(nativeImageBuildItem.getPath(), workDirInContainer.resolve(BINARY_NAME_IN_CONTAINER), FilePermissions.fromOctalString("775")).build()).setWorkingDirectory(workDirInContainer).setEntrypoint(entrypoint).setEnvironment(getEnvironmentVariables(jibConfig)).setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels));
if (jibConfig.useCurrentTimestamp) {
jibContainerBuilder.setCreationTime(Instant.now());
}
for (int port : jibConfig.ports) {
jibContainerBuilder.addExposedPort(Port.tcp(port));
}
return jibContainerBuilder;
} catch (InvalidImageReferenceException e) {
throw new RuntimeException(e);
}
}
use of com.google.cloud.tools.jib.api.InvalidImageReferenceException in project quarkus by quarkusio.
the class JibProcessor method createContainerBuilderFromFastJar.
/**
* We don't use Jib's JavaContainerBuilder here because we need to support the custom fast-jar format
* We create the following layers (least likely to change to most likely to change):
*
* <ul>
* <li>lib</li>
* <li>boot-lib</li>
* <li>quarkus-run.jar</li>
* <li>quarkus</li>
* <li>app</li>
* </ul>
*/
private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImage, JibConfig jibConfig, ContainerImageConfig containerImageConfig, JarBuildItem sourceJarBuildItem, CurateOutcomeBuildItem curateOutcome, List<ContainerImageLabelBuildItem> containerImageLabels, Optional<AppCDSResultBuildItem> appCDSResult) {
Path componentsPath = sourceJarBuildItem.getPath().getParent();
Path appLibDir = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.MAIN);
AbsoluteUnixPath workDirInContainer = AbsoluteUnixPath.get(jibConfig.workingDirectory);
List<String> entrypoint;
if (jibConfig.jvmEntrypoint.isPresent()) {
entrypoint = jibConfig.jvmEntrypoint.get();
} else {
List<String> effectiveJvmArguments = determineEffectiveJvmArguments(jibConfig, appCDSResult);
entrypoint = new ArrayList<>(3 + effectiveJvmArguments.size());
entrypoint.add("java");
entrypoint.addAll(effectiveJvmArguments);
entrypoint.add("-jar");
entrypoint.add(JarResultBuildStep.QUARKUS_RUN_JAR);
}
List<ResolvedDependency> fastChangingLibs = new ArrayList<>();
Collection<ResolvedDependency> userDependencies = curateOutcome.getApplicationModel().getRuntimeDependencies();
for (ResolvedDependency artifact : userDependencies) {
if (artifact == null) {
continue;
}
String artifactVersion = artifact.getVersion();
if ((artifactVersion == null) || artifactVersion.isEmpty()) {
continue;
}
if (artifactVersion.toLowerCase().contains("snapshot")) {
fastChangingLibs.add(artifact);
}
}
Set<Path> fastChangingLibPaths = Collections.emptySet();
List<Path> nonFastChangingLibPaths = null;
if (!fastChangingLibs.isEmpty()) {
fastChangingLibPaths = new HashSet<>(fastChangingLibs.size());
Map<String, Path> libNameToPath = new HashMap<>();
try (DirectoryStream<Path> allLibPaths = Files.newDirectoryStream(appLibDir)) {
for (Path libPath : allLibPaths) {
libNameToPath.put(libPath.getFileName().toString(), libPath);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
List<String> libFileNames = new ArrayList<>(libNameToPath.keySet());
for (ResolvedDependency appArtifact : fastChangingLibs) {
String matchingLibDirFileName = null;
for (Path appArtifactPath : appArtifact.getResolvedPaths()) {
for (String libFileName : libFileNames) {
if (libFileName.contains(appArtifact.getGroupId()) && libFileName.contains(appArtifactPath.getFileName().toString())) {
matchingLibDirFileName = libFileName;
break;
}
}
if (matchingLibDirFileName != null) {
break;
}
}
if (matchingLibDirFileName != null) {
fastChangingLibPaths.add(libNameToPath.get(matchingLibDirFileName));
}
}
Collection<Path> allLibPaths = libNameToPath.values();
nonFastChangingLibPaths = new ArrayList<>(allLibPaths.size() - fastChangingLibPaths.size());
for (Path libPath : allLibPaths) {
if (!fastChangingLibPaths.contains(libPath)) {
nonFastChangingLibPaths.add(libPath);
}
}
}
try {
Instant now = Instant.now();
JibContainerBuilder jibContainerBuilder = Jib.from(toRegistryImage(ImageReference.parse(baseJvmImage), jibConfig.baseRegistryUsername, jibConfig.baseRegistryPassword));
if (fastChangingLibPaths.isEmpty()) {
// just create a layer with the entire lib structure intact
jibContainerBuilder.addLayer(Collections.singletonList(componentsPath.resolve(JarResultBuildStep.LIB)), workDirInContainer);
} else {
// we need to manually create each layer
// the idea here is that the fast changing libraries are created in a later layer, thus when they do change,
// docker doesn't have to create an entire layer with all dependencies - only change the fast ones
FileEntriesLayer.Builder bootLibsLayerBuilder = FileEntriesLayer.builder();
Path bootLibPath = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.BOOT_LIB);
try (Stream<Path> boolLibPaths = Files.list(bootLibPath)) {
boolLibPaths.forEach(lib -> {
try {
AbsoluteUnixPath libPathInContainer = workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.BOOT_LIB).resolve(lib.getFileName());
if (appCDSResult.isPresent()) {
// the boot lib jars need to preserve the modification time because otherwise AppCDS won't work
bootLibsLayerBuilder.addEntry(lib, libPathInContainer, Files.getLastModifiedTime(lib).toInstant());
} else {
bootLibsLayerBuilder.addEntry(lib, libPathInContainer);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
jibContainerBuilder.addFileEntriesLayer(bootLibsLayerBuilder.build());
Path deploymentPath = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.DEPLOYMENT_LIB);
if (Files.exists(deploymentPath)) {
// this is the case of mutable-jar
FileEntriesLayer.Builder deploymentLayerBuilder = FileEntriesLayer.builder();
Files.list(deploymentPath).forEach(lib -> {
AbsoluteUnixPath libPathInContainer = workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.DEPLOYMENT_LIB).resolve(lib.getFileName());
deploymentLayerBuilder.addEntry(lib, libPathInContainer);
});
jibContainerBuilder.addFileEntriesLayer(deploymentLayerBuilder.build());
}
jibContainerBuilder.addLayer(nonFastChangingLibPaths, workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.MAIN));
jibContainerBuilder.addLayer(new ArrayList<>(fastChangingLibPaths), workDirInContainer.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.MAIN));
}
if (appCDSResult.isPresent()) {
jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().addEntry(componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR), workDirInContainer.resolve(JarResultBuildStep.QUARKUS_RUN_JAR), Files.getLastModifiedTime(componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR)).toInstant()).build());
jibContainerBuilder.addLayer(Collections.singletonList(appCDSResult.get().getAppCDS()), workDirInContainer);
} else {
jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().addEntry(componentsPath.resolve(JarResultBuildStep.QUARKUS_RUN_JAR), workDirInContainer.resolve(JarResultBuildStep.QUARKUS_RUN_JAR)).build());
}
jibContainerBuilder.addLayer(Collections.singletonList(componentsPath.resolve(JarResultBuildStep.APP)), workDirInContainer).addLayer(Collections.singletonList(componentsPath.resolve(JarResultBuildStep.QUARKUS)), workDirInContainer);
if (JibConfig.DEFAULT_WORKING_DIR.equals(jibConfig.workingDirectory)) {
// this layer ensures that the working directory is writeable
// see https://github.com/GoogleContainerTools/jib/issues/1270
// TODO: is this needed for all working directories?
jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().addEntry(new FileEntry(Files.createTempDirectory("jib"), AbsoluteUnixPath.get(jibConfig.workingDirectory), FilePermissions.DEFAULT_FOLDER_PERMISSIONS, now, DEFAULT_BASE_IMAGE_USER)).build());
}
jibContainerBuilder.setWorkingDirectory(workDirInContainer).setEntrypoint(entrypoint).setEnvironment(getEnvironmentVariables(jibConfig)).setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels));
if (jibConfig.useCurrentTimestamp) {
jibContainerBuilder.setCreationTime(now);
}
for (int port : jibConfig.ports) {
jibContainerBuilder.addExposedPort(Port.tcp(port));
}
return jibContainerBuilder;
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InvalidImageReferenceException e) {
throw new RuntimeException(e);
}
}
use of com.google.cloud.tools.jib.api.InvalidImageReferenceException in project geronimo-arthur by apache.
the class JibMojo method prepare.
private JibContainerBuilder prepare() {
try {
final JibContainerBuilder from = Jib.from(ImageReference.parse(this.from));
if (ports != null) {
from.setExposedPorts(Ports.parse(ports));
}
if (labels != null) {
from.setLabels(labels);
}
if (programArguments != null) {
from.setProgramArguments(programArguments);
}
from.setCreationTime(creationTimestamp < 0 ? Instant.now() : Instant.ofEpochMilli(creationTimestamp));
final boolean hasNatives = useLDD || (includeNatives != null && !includeNatives.isEmpty() && !singletonList("false").equals(includeNatives));
final Path source = ofNullable(binarySource).map(File::toPath).orElseGet(() -> Paths.get(requireNonNull(project.getProperties().getProperty(propertiesPrefix + "binary.path"), "No binary path found, ensure to run native-image before or set entrypoint")));
final Map<String, String> env = environment == null ? new TreeMap<>() : new TreeMap<>(environment);
final List<FileEntriesLayer> layers = new ArrayList<>(8);
if (includeCacerts) {
layers.add(findCertificates());
}
String ldLinux = null;
if (hasNatives) {
if (!singletonList("false").equals(includeNatives)) {
layers.add(findNatives());
}
if (useLDD) {
ldLinux = addLddLibsAndFindLdLinux(env, layers);
}
}
if (otherFiles != null && !otherFiles.isEmpty()) {
layers.add(createOthersLayer());
}
layers.add(FileEntriesLayer.builder().setName("Binary").addEntry(new FileEntry(source, AbsoluteUnixPath.get(entrypoint.iterator().next()), FilePermissions.fromOctalString("755"), getTimestamp(source))).build());
from.setFileEntriesLayers(layers);
if (!env.isEmpty()) {
from.setEnvironment(env);
}
if (entrypoint == null || entrypoint.size() < 1) {
throw new IllegalArgumentException("No entrypoint set");
}
from.setEntrypoint(Stream.concat(Stream.concat(Stream.concat(useLDD && ldLinux != null && !skipLdLinuxInEntrypoint ? Stream.of(ldLinux) : Stream.empty(), entrypoint.stream()), hasNatives ? Stream.of("-Djava.library.path=" + nativeRootDir) : Stream.empty()), includeCacerts ? Stream.of("-Djavax.net.ssl.trustStore=" + cacertsTarget) : Stream.empty()).collect(toList()));
return from;
} catch (final InvalidImageReferenceException | IOException e) {
throw new IllegalStateException(e);
}
}
use of com.google.cloud.tools.jib.api.InvalidImageReferenceException in project geronimo-arthur by apache.
the class JibMojo method execute.
@Override
public void execute() {
final JibContainerBuilder prepared = prepare();
withExecutor(es -> {
try {
final Containerizer containerizer = createContainer();
final JibContainer container = prepared.containerize(configure(containerizer, es));
if (propertiesPrefix != null) {
project.getProperties().setProperty(propertiesPrefix + "image.imageId", container.getImageId().getHash());
project.getProperties().setProperty(propertiesPrefix + "image.digest", container.getDigest().getHash());
}
getLog().info("Built '" + to + "'");
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
} catch (final RegistryException | IOException | CacheDirectoryCreationException | ExecutionException | InvalidImageReferenceException e) {
throw new IllegalStateException(e);
}
});
}
Aggregations