Search in sources :

Example 1 with JsonTemplate

use of com.google.cloud.tools.jib.json.JsonTemplate 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 JsonTemplate

use of com.google.cloud.tools.jib.json.JsonTemplate 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 3 with JsonTemplate

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

the class ImageToJsonTranslatorTest method testGetManifest.

/**
 * Tests translation of image to {@link BuildableManifestTemplate}.
 */
private <T extends BuildableManifestTemplate> void testGetManifest(Class<T> manifestTemplateClass, String translatedJsonFilename) throws URISyntaxException, IOException {
    // Loads the expected JSON string.
    Path jsonFile = Paths.get(Resources.getResource(translatedJsonFilename).toURI());
    String expectedJson = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
    // Translates the image to the manifest and writes the JSON string.
    JsonTemplate containerConfiguration = imageToJsonTranslator.getContainerConfiguration();
    BlobDescriptor blobDescriptor = Digests.computeDigest(containerConfiguration);
    T manifestTemplate = imageToJsonTranslator.getManifestTemplate(manifestTemplateClass, blobDescriptor);
    Assert.assertEquals(expectedJson, JsonTemplateMapper.toUtf8String(manifestTemplate));
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate)

Example 4 with JsonTemplate

use of com.google.cloud.tools.jib.json.JsonTemplate 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)

Example 5 with JsonTemplate

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

the class ImageToJsonTranslatorTest method testGetContainerConfiguration.

@Test
public void testGetContainerConfiguration() throws IOException, URISyntaxException, DigestException {
    setUp(V22ManifestTemplate.class);
    // Loads the expected JSON string.
    Path jsonFile = Paths.get(Resources.getResource("core/json/containerconfig.json").toURI());
    String expectedJson = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
    // Translates the image to the container configuration and writes the JSON string.
    JsonTemplate containerConfiguration = imageToJsonTranslator.getContainerConfiguration();
    Assert.assertEquals(expectedJson, JsonTemplateMapper.toUtf8String(containerConfiguration));
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) Test(org.junit.Test)

Aggregations

JsonTemplate (com.google.cloud.tools.jib.json.JsonTemplate)8 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)4 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)4 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)4 TarStreamBuilder (com.google.cloud.tools.jib.tar.TarStreamBuilder)4 Path (java.nio.file.Path)4 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 DockerManifestEntryTemplate (com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate)2 OciIndexTemplate (com.google.cloud.tools.jib.image.json.OciIndexTemplate)2 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)2 Test (org.junit.Test)2