Search in sources :

Example 1 with OciIndexTemplate

use of com.google.cloud.tools.jib.image.json.OciIndexTemplate in project jib by GoogleContainerTools.

the class ImageTarball method ociWriteTo.

private void ociWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    OciManifestTemplate manifest = new OciManifestTemplate();
    // Adds all the layers to the tarball and manifest
    for (Layer layer : image.getLayers()) {
        DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
        long size = layer.getBlobDescriptor().getSize();
        tarStreamBuilder.addBlobEntry(layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
        manifest.addLayer(size, digest);
    }
    // Adds the container configuration to the tarball and manifest
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
    manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), BLOB_PREFIX + configDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to the tarball
    BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(manifest), BLOB_PREFIX + manifestDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the oci-layout and index.json
    tarStreamBuilder.addByteEntry("{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8), "oci-layout", TAR_ENTRY_MODIFICATION_TIME);
    OciIndexTemplate index = new OciIndexTemplate();
    // TODO: figure out how to tag with allTargetImageTags
    index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 2 with OciIndexTemplate

use of com.google.cloud.tools.jib.image.json.OciIndexTemplate in project jib by GoogleContainerTools.

the class ImageTarballTest method testWriteTo_oci.

@Test
public void testWriteTo_oci() throws InvalidImageReferenceException, IOException, LayerPropertyNotFoundException {
    Image testImage = Image.builder(OciManifestTemplate.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("blobs/sha256/" + fakeDigestA.getHash(), 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("blobs/sha256/" + fakeDigestB.getHash(), 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("blobs/sha256/011212cff4d5d6b18c7d3a00a7a2701514a1fdd3ec0d250a03756f84f3d955d4", headerContainerConfiguration.getName());
        JsonTemplateMapper.readJson(tarArchiveInputStream, ContainerConfigurationTemplate.class);
        // Verifies manifest was added.
        TarArchiveEntry headerManifest = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("blobs/sha256/1543d061159a8d6877087938bfd62681cdeff873e1fa3e1fcf12dec358c112a4", headerManifest.getName());
        JsonTemplateMapper.readJson(tarArchiveInputStream, OciManifestTemplate.class);
        // Verifies oci-layout was added.
        TarArchiveEntry headerOciLayout = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("oci-layout", headerOciLayout.getName());
        String ociLayoutJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
        Assert.assertEquals("{\"imageLayoutVersion\": \"1.0.0\"}", ociLayoutJson);
        // Verifies index.json was added.
        TarArchiveEntry headerIndex = tarArchiveInputStream.getNextTarEntry();
        Assert.assertEquals("index.json", headerIndex.getName());
        OciIndexTemplate index = JsonTemplateMapper.readJson(tarArchiveInputStream, OciIndexTemplate.class);
        BuildableManifestTemplate.ContentDescriptorTemplate indexManifest = index.getManifests().get(0);
        Assert.assertEquals("1543d061159a8d6877087938bfd62681cdeff873e1fa3e1fcf12dec358c112a4", indexManifest.getDigest().getHash());
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 3 with OciIndexTemplate

use of com.google.cloud.tools.jib.image.json.OciIndexTemplate in project jib by GoogleContainerTools.

the class CacheStorageReaderTest method testVerifyImageMetadata_validOciImageIndex.

@Test
public void testVerifyImageMetadata_validOciImageIndex() throws CacheCorruptedException {
    ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(new OciManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest");
    ImageMetadataTemplate metadata = new ImageMetadataTemplate(new OciIndexTemplate(), Arrays.asList(manifestAndConfig, manifestAndConfig));
    CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
// should pass without CacheCorruptedException
}
Also used : OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) Test(org.junit.Test)

Example 4 with OciIndexTemplate

use of com.google.cloud.tools.jib.image.json.OciIndexTemplate in project jib by GoogleContainerTools.

the class CacheStorageWriterTest method testWriteMetadata_oci.

@Test
public void testWriteMetadata_oci() throws URISyntaxException, IOException, InvalidImageReferenceException {
    ContainerConfigurationTemplate containerConfig = loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class);
    OciManifestTemplate manifest = loadJsonResource("core/json/ocimanifest.json", OciManifestTemplate.class);
    OciIndexTemplate ociIndex = loadJsonResource("core/json/ociindex.json", OciIndexTemplate.class);
    ImageReference imageReference = ImageReference.parse("image.reference/project/thing:tag");
    cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(ociIndex, Arrays.asList(new ManifestAndConfigTemplate(manifest, containerConfig, "sha256:digest"))));
    Path savedMetadataPath = cacheRoot.resolve("images/image.reference/project/thing!tag/manifests_configs.json");
    Assert.assertTrue(Files.exists(savedMetadataPath));
    ImageMetadataTemplate savedMetadata = JsonTemplateMapper.readJsonFromFile(savedMetadataPath, ImageMetadataTemplate.class);
    MatcherAssert.assertThat(savedMetadata.getManifestList(), CoreMatchers.instanceOf(OciIndexTemplate.class));
    List<ContentDescriptorTemplate> savedManifestDescriptors = ((OciIndexTemplate) savedMetadata.getManifestList()).getManifests();
    Assert.assertEquals(1, savedManifestDescriptors.size());
    Assert.assertEquals("8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad", savedManifestDescriptors.get(0).getDigest().getHash());
    Assert.assertEquals(1, savedMetadata.getManifestsAndConfigs().size());
    ManifestAndConfigTemplate savedManifestAndConfig = savedMetadata.getManifestsAndConfigs().get(0);
    OciManifestTemplate savedManifest1 = (OciManifestTemplate) savedManifestAndConfig.getManifest();
    Assert.assertEquals(2, savedManifest1.getSchemaVersion());
    Assert.assertEquals(1, savedManifest1.getLayers().size());
    Assert.assertEquals("4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236", savedManifest1.getLayers().get(0).getDigest().getHash());
    Assert.assertEquals("8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad", savedManifest1.getContainerConfiguration().getDigest().getHash());
    Assert.assertEquals("wasm", savedManifestAndConfig.getConfig().getArchitecture());
}
Also used : Path(java.nio.file.Path) OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) ContentDescriptorTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate.ContentDescriptorTemplate) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Test(org.junit.Test)

Example 5 with OciIndexTemplate

use of com.google.cloud.tools.jib.image.json.OciIndexTemplate in project jib by google.

the class ImageTarball method ociWriteTo.

private void ociWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    OciManifestTemplate manifest = new OciManifestTemplate();
    // Adds all the layers to the tarball and manifest
    for (Layer layer : image.getLayers()) {
        DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
        long size = layer.getBlobDescriptor().getSize();
        tarStreamBuilder.addBlobEntry(layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
        manifest.addLayer(size, digest);
    }
    // Adds the container configuration to the tarball and manifest
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
    manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), BLOB_PREFIX + configDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to the tarball
    BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(manifest), BLOB_PREFIX + manifestDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the oci-layout and index.json
    tarStreamBuilder.addByteEntry("{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8), "oci-layout", TAR_ENTRY_MODIFICATION_TIME);
    OciIndexTemplate index = new OciIndexTemplate();
    // TODO: figure out how to tag with allTargetImageTags
    index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Aggregations

OciIndexTemplate (com.google.cloud.tools.jib.image.json.OciIndexTemplate)12 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)12 Test (org.junit.Test)10 ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)6 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)6 ContentDescriptorTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate.ContentDescriptorTemplate)4 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)4 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 ImageReference (com.google.cloud.tools.jib.api.ImageReference)2 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)2 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)2 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)2 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)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 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStreamReader (java.io.InputStreamReader)2