Search in sources :

Example 61 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibLayerFilterExtension method moveParentDepsToNewLayers.

private ContainerBuildPlan moveParentDepsToNewLayers(ContainerBuildPlan buildPlan, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
    logger.log(LogLevel.INFO, "Moving parent dependencies to new layers.");
    // The key is the source file path for the parent dependency. We only consider artifacts that
    // have been resolved.
    Map<Path, Artifact> parentDependencies = getParentDependencies(mavenData).stream().map(Dependency::getArtifact).filter(artifact -> artifact.getFile() != null).collect(Collectors.toMap(artifact -> artifact.getFile().toPath(), artifact -> artifact));
    // Parent dependencies that have not been found in any layer (due to different version or
    // filtering). Only needed for logging.
    Map<Path, Artifact> parentDependenciesNotFound = new HashMap<>(parentDependencies);
    List<FileEntriesLayer.Builder> newLayerBuilders = new ArrayList<>();
    @SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = ((List<FileEntriesLayer>) buildPlan.getLayers());
    for (FileEntriesLayer originalLayer : originalLayers) {
        String parentLayerName = originalLayer.getName() + "-parent";
        FileEntriesLayer.Builder parentLayerBuilder = FileEntriesLayer.builder().setName(parentLayerName);
        FileEntriesLayer.Builder layerBuilder = originalLayer.toBuilder().setEntries(Collections.emptyList());
        newLayerBuilders.add(parentLayerBuilder);
        newLayerBuilders.add(layerBuilder);
        for (FileEntry entry : originalLayer.getEntries()) {
            Path sourceFilePath = entry.getSourceFile();
            if (parentDependencies.containsKey(sourceFilePath)) {
                // move to parent layer
                logger.log(LogLevel.DEBUG, "Moving " + sourceFilePath + " to " + parentLayerName + ".");
                parentLayerBuilder.addEntry(entry);
                // mark parent dep as found
                parentDependenciesNotFound.remove(sourceFilePath);
            } else {
                // keep in original layer
                logger.log(LogLevel.DEBUG, "Keeping " + sourceFilePath + " in " + originalLayer.getName() + ".");
                layerBuilder.addEntry(entry);
            }
        }
    }
    logMissingParentDependencies(logger, parentDependenciesNotFound, originalLayers);
    List<FileEntriesLayer> newLayers = newLayerBuilders.stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).collect(Collectors.toList());
    return buildPlan.toBuilder().setLayers(newLayers).build();
}
Also used : Path(java.nio.file.Path) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) Dependency(org.eclipse.aether.graph.Dependency) HashMap(java.util.HashMap) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) Singleton(javax.inject.Singleton) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) Named(javax.inject.Named) Path(java.nio.file.Path) Verify(com.google.common.base.Verify) Artifact(org.eclipse.aether.artifact.Artifact) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) Paths(java.nio.file.Paths) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) MavenData(com.google.cloud.tools.jib.maven.extension.MavenData) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) FileSystems(java.nio.file.FileSystems) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Dependency(org.eclipse.aether.graph.Dependency) Artifact(org.eclipse.aether.artifact.Artifact) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry)

Example 62 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibLayerFilterExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Configuration> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
    logger.log(LogLevel.LIFECYCLE, "Running Jib Layer Filter Extension");
    if (!config.isPresent()) {
        logger.log(LogLevel.WARN, "Nothing configured for Jib Layer Filter Extension");
        return buildPlan;
    }
    preparePathMatchersAndLayerBuilders(buildPlan, config.get());
    ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
    newPlanBuilder.setLayers(Collections.emptyList());
    @SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = (List<FileEntriesLayer>) buildPlan.getLayers();
    // Start filtering original layers.
    for (FileEntriesLayer layer : originalLayers) {
        List<FileEntry> filesToKeep = new ArrayList<>();
        for (FileEntry entry : layer.getEntries()) {
            Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
            // Either keep, move, or delete this FileEntry.
            if (finalLayerName.isPresent()) {
                if (finalLayerName.get().equals(layer.getName())) {
                    filesToKeep.add(entry);
                } else {
                    FileEntriesLayer.Builder targetLayerBuilder = Verify.verifyNotNull(newToLayers.get(finalLayerName.get()));
                    targetLayerBuilder.addEntry(entry);
                }
            }
        }
        if (!filesToKeep.isEmpty()) {
            newPlanBuilder.addLayer(layer.toBuilder().setEntries(filesToKeep).build());
        }
    }
    // Add newly created non-empty to-layers (if any).
    newToLayers.values().stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).forEach(newPlanBuilder::addLayer);
    return newPlanBuilder.build();
}
Also used : GradleData(com.google.cloud.tools.jib.gradle.extension.GradleData) Verify(com.google.common.base.Verify) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension) Paths(java.nio.file.Paths) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) Collections(java.util.Collections) FileSystems(java.nio.file.FileSystems) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 63 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.

the class JibNativeImageExtensionTest method testExtendContainerBuildPlan.

@Test
public void testExtendContainerBuildPlan() throws JibPluginExtensionException, IOException {
    Map<String, String> properties = Collections.singletonMap("imageName", "theExecutable");
    tempFolder.newFile("theExecutable");
    FileEntriesLayer layer = buildLayer("original layer", Paths.get("foo.txt"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer).build();
    ContainerBuildPlan newPlan = new JibNativeImageExtension().extendContainerBuildPlan(buildPlan, properties, Optional.empty(), mavenData, logger);
    assertEquals(1, newPlan.getLayers().size());
    FileEntriesLayer newLayer = (FileEntriesLayer) newPlan.getLayers().get(0);
    assertEquals("native image", newLayer.getName());
    assertEquals(1, newLayer.getEntries().size());
    FileEntry fileEntry = newLayer.getEntries().get(0);
    assertEquals(AbsoluteUnixPath.get("/app/theExecutable"), fileEntry.getExtractionPath());
    assertEquals(FilePermissions.fromOctalString("755"), fileEntry.getPermissions());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 64 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry 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);
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) IOException(java.io.IOException) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Aggregations

FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)64 Test (org.junit.Test)45 Path (java.nio.file.Path)39 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)24 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)16 ArrayList (java.util.ArrayList)13 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)10 Blob (com.google.cloud.tools.jib.blob.Blob)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 List (java.util.List)9 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)8 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)8 PathMatcher (java.nio.file.PathMatcher)7 Instant (java.time.Instant)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6