Search in sources :

Example 6 with DockerManifestEntryTemplate

use of com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate in project jib by GoogleContainerTools.

the class LocalBaseImageSteps method cacheDockerImageTar.

@VisibleForTesting
static LocalImage cacheDockerImageTar(BuildContext buildContext, Path tarPath, ProgressEventDispatcher.Factory progressEventDispatcherFactory, TempDirectoryProvider tempDirectoryProvider) throws IOException, LayerCountMismatchException {
    ExecutorService executorService = buildContext.getExecutorService();
    Path destination = tempDirectoryProvider.newDirectory();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(buildContext.getEventHandlers(), "Extracting tar " + tarPath + " into " + destination)) {
        TarExtractor.extract(tarPath, destination);
        InputStream manifestStream = Files.newInputStream(destination.resolve("manifest.json"));
        DockerManifestEntryTemplate loadManifest = new ObjectMapper().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true).readValue(manifestStream, DockerManifestEntryTemplate[].class)[0];
        manifestStream.close();
        Path configPath = destination.resolve(loadManifest.getConfig());
        ContainerConfigurationTemplate configurationTemplate = JsonTemplateMapper.readJsonFromFile(configPath, ContainerConfigurationTemplate.class);
        // Don't compute the digest of the loaded Java JSON instance.
        BlobDescriptor originalConfigDescriptor = Blobs.from(configPath).writeTo(ByteStreams.nullOutputStream());
        List<String> layerFiles = loadManifest.getLayerFiles();
        if (configurationTemplate.getLayerCount() != layerFiles.size()) {
            throw new LayerCountMismatchException("Invalid base image format: manifest contains " + layerFiles.size() + " layers, but container configuration contains " + configurationTemplate.getLayerCount() + " layers");
        }
        buildContext.getBaseImageLayersCache().writeLocalConfig(originalConfigDescriptor.getDigest(), configurationTemplate);
        // Check the first layer to see if the layers are compressed already. 'docker save' output
        // is uncompressed, but a jib-built tar has compressed layers.
        boolean layersAreCompressed = !layerFiles.isEmpty() && isGzipped(destination.resolve(layerFiles.get(0)));
        // Process layer blobs
        try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("processing base image layers", layerFiles.size())) {
            // Start compressing layers in parallel
            List<Future<PreparedLayer>> preparedLayers = new ArrayList<>();
            for (int index = 0; index < layerFiles.size(); index++) {
                Path layerFile = destination.resolve(layerFiles.get(index));
                DescriptorDigest diffId = configurationTemplate.getLayerDiffId(index);
                ProgressEventDispatcher.Factory layerProgressDispatcherFactory = progressEventDispatcher.newChildProducer();
                preparedLayers.add(executorService.submit(() -> compressAndCacheTarLayer(buildContext.getBaseImageLayersCache(), diffId, layerFile, layersAreCompressed, layerProgressDispatcherFactory)));
            }
            return new LocalImage(preparedLayers, configurationTemplate);
        }
    }
}
Also used : Path(java.nio.file.Path) LayerCountMismatchException(com.google.cloud.tools.jib.image.LayerCountMismatchException) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) ExecutorService(java.util.concurrent.ExecutorService) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) Future(java.util.concurrent.Future) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

DockerManifestEntryTemplate (com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)2 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)2 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)2 LayerCountMismatchException (com.google.cloud.tools.jib.image.LayerCountMismatchException)2 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)2 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 JsonTemplate (com.google.cloud.tools.jib.json.JsonTemplate)2 TarStreamBuilder (com.google.cloud.tools.jib.tar.TarStreamBuilder)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2