Search in sources :

Example 1 with DockerManifestEntryTemplate

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

the class ImageTarball method dockerWriteTo.

private void dockerWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    DockerManifestEntryTemplate manifestTemplate = new DockerManifestEntryTemplate();
    // Adds all the layers to the tarball and manifest.
    for (Layer layer : image.getLayers()) {
        String layerName = layer.getBlobDescriptor().getDigest().getHash() + LAYER_FILE_EXTENSION;
        tarStreamBuilder.addBlobEntry(layer.getBlob(), layer.getBlobDescriptor().getSize(), layerName, TAR_ENTRY_MODIFICATION_TIME);
        manifestTemplate.addLayerFile(layerName);
    }
    // Adds the container configuration to the tarball.
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), CONTAINER_CONFIGURATION_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to tarball.
    for (String tag : allTargetImageTags) {
        manifestTemplate.addRepoTag(imageReference.withQualifier(tag).toStringWithQualifier());
    }
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)), MANIFEST_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 2 with DockerManifestEntryTemplate

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

the class ImageTarballTest method testWriteTo_docker.

@Test
public void testWriteTo_docker() throws InvalidImageReferenceException, IOException, LayerPropertyNotFoundException {
    Image testImage = Image.builder(V22ManifestTemplate.class).addLayer(mockLayer1).addLayer(mockLayer2).build();
    ImageTarball imageTarball = new ImageTarball(testImage, ImageReference.parse("my/image:tag"), ImmutableSet.of("tag", "another-tag", "tag3"));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    imageTarball.writeTo(out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(in)) {
        // Verifies layer with fileA was added.
        TarArchiveEntry headerFileALayer = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals(fakeDigestA.getHash() + ".tar.gz", headerFileALayer.getName());
        String fileAString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        Assert.assertEquals(Blobs.writeToString(Blobs.from(fileA)), fileAString);
        // Verifies layer with fileB was added.
        TarArchiveEntry headerFileBLayer = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals(fakeDigestB.getHash() + ".tar.gz", headerFileBLayer.getName());
        String fileBString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        Assert.assertEquals(Blobs.writeToString(Blobs.from(fileB)), fileBString);
        // Verifies container configuration was added.
        TarArchiveEntry headerContainerConfiguration = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("config.json", headerContainerConfiguration.getName());
        String containerConfigJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        JsonTemplateMapper.readJson(containerConfigJson, ContainerConfigurationTemplate.class);
        // Verifies manifest was added.
        TarArchiveEntry headerManifest = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("manifest.json", headerManifest.getName());
        String manifestJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        DockerManifestEntryTemplate manifest = JsonTemplateMapper.readListOfJson(manifestJson, DockerManifestEntryTemplate.class).get(0);
        Assert.assertEquals(ImmutableList.of("my/image:tag", "my/image:another-tag", "my/image:tag3"), manifest.getRepoTags());
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 3 with DockerManifestEntryTemplate

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

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)

Example 4 with DockerManifestEntryTemplate

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

the class ImageTarballTest method testWriteTo_docker.

@Test
public void testWriteTo_docker() throws InvalidImageReferenceException, IOException, LayerPropertyNotFoundException {
    Image testImage = Image.builder(V22ManifestTemplate.class).addLayer(mockLayer1).addLayer(mockLayer2).build();
    ImageTarball imageTarball = new ImageTarball(testImage, ImageReference.parse("my/image:tag"), ImmutableSet.of("tag", "another-tag", "tag3"));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    imageTarball.writeTo(out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(in)) {
        // Verifies layer with fileA was added.
        TarArchiveEntry headerFileALayer = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals(fakeDigestA.getHash() + ".tar.gz", headerFileALayer.getName());
        String fileAString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        Assert.assertEquals(Blobs.writeToString(Blobs.from(fileA)), fileAString);
        // Verifies layer with fileB was added.
        TarArchiveEntry headerFileBLayer = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals(fakeDigestB.getHash() + ".tar.gz", headerFileBLayer.getName());
        String fileBString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        Assert.assertEquals(Blobs.writeToString(Blobs.from(fileB)), fileBString);
        // Verifies container configuration was added.
        TarArchiveEntry headerContainerConfiguration = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("config.json", headerContainerConfiguration.getName());
        String containerConfigJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        JsonTemplateMapper.readJson(containerConfigJson, ContainerConfigurationTemplate.class);
        // Verifies manifest was added.
        TarArchiveEntry headerManifest = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("manifest.json", headerManifest.getName());
        String manifestJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        DockerManifestEntryTemplate manifest = JsonTemplateMapper.readListOfJson(manifestJson, DockerManifestEntryTemplate.class).get(0);
        Assert.assertEquals(ImmutableList.of("my/image:tag", "my/image:another-tag", "my/image:tag3"), manifest.getRepoTags());
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 5 with DockerManifestEntryTemplate

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

the class ImageTarball method dockerWriteTo.

private void dockerWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    DockerManifestEntryTemplate manifestTemplate = new DockerManifestEntryTemplate();
    // Adds all the layers to the tarball and manifest.
    for (Layer layer : image.getLayers()) {
        String layerName = layer.getBlobDescriptor().getDigest().getHash() + LAYER_FILE_EXTENSION;
        tarStreamBuilder.addBlobEntry(layer.getBlob(), layer.getBlobDescriptor().getSize(), layerName, TAR_ENTRY_MODIFICATION_TIME);
        manifestTemplate.addLayerFile(layerName);
    }
    // Adds the container configuration to the tarball.
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), CONTAINER_CONFIGURATION_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to tarball.
    for (String tag : allTargetImageTags) {
        manifestTemplate.addRepoTag(imageReference.withQualifier(tag).toStringWithQualifier());
    }
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)), MANIFEST_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

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