Search in sources :

Example 1 with Layer

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

the class JsonToImageTranslatorTest method testToImage_buildable.

private <T extends BuildableManifestTemplate> void testToImage_buildable(String jsonFilename, Class<T> manifestTemplateClass) throws IOException, LayerPropertyNotFoundException, LayerCountMismatchException, DigestException, URISyntaxException {
    // Loads the container configuration JSON.
    Path containerConfigurationJsonFile = Paths.get(getClass().getClassLoader().getResource("json/containerconfig.json").toURI());
    ContainerConfigurationTemplate containerConfigurationTemplate = JsonTemplateMapper.readJsonFromFile(containerConfigurationJsonFile, ContainerConfigurationTemplate.class);
    // Loads the manifest JSON.
    Path manifestJsonFile = Paths.get(getClass().getClassLoader().getResource(jsonFilename).toURI());
    T manifestTemplate = JsonTemplateMapper.readJsonFromFile(manifestJsonFile, manifestTemplateClass);
    Image image = JsonToImageTranslator.toImage(manifestTemplate, containerConfigurationTemplate);
    List<Layer> layers = image.getLayers();
    Assert.assertEquals(1, layers.size());
    Assert.assertEquals(new BlobDescriptor(1000000, DescriptorDigest.fromDigest("sha256:4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236")), layers.get(0).getBlobDescriptor());
    Assert.assertEquals(DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"), layers.get(0).getDiffId());
    Assert.assertEquals(Arrays.asList("some", "entrypoint", "command"), image.getEntrypoint());
    Assert.assertEquals(Arrays.asList("VAR1=VAL1", "VAR2=VAL2"), image.getEnvironment());
}
Also used : Path(java.nio.file.Path) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Image(com.google.cloud.tools.jib.image.Image) Layer(com.google.cloud.tools.jib.image.Layer)

Example 2 with Layer

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

the class JsonToImageTranslator method toImage.

/**
 * Translates {@link V21ManifestTemplate} to {@link Image}.
 */
public static Image toImage(V21ManifestTemplate manifestTemplate) throws LayerPropertyNotFoundException {
    Image image = new Image();
    for (DescriptorDigest digest : manifestTemplate.getLayerDigests()) {
        Layer layer = new DigestOnlyLayer(digest);
        image.addLayer(layer);
    }
    return image;
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) DigestOnlyLayer(com.google.cloud.tools.jib.image.DigestOnlyLayer) Image(com.google.cloud.tools.jib.image.Image) ReferenceNoDiffIdLayer(com.google.cloud.tools.jib.image.ReferenceNoDiffIdLayer) Layer(com.google.cloud.tools.jib.image.Layer) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) DigestOnlyLayer(com.google.cloud.tools.jib.image.DigestOnlyLayer)

Example 3 with Layer

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

the class JsonToImageTranslator method toImage.

/**
 * Translates {@link BuildableManifestTemplate} to {@link Image}. Uses the corresponding {@link
 * ContainerConfigurationTemplate} to get the layer diff IDs.
 */
public static Image toImage(BuildableManifestTemplate manifestTemplate, ContainerConfigurationTemplate containerConfigurationTemplate) throws LayerCountMismatchException, LayerPropertyNotFoundException {
    Image image = new Image();
    List<ReferenceNoDiffIdLayer> layers = new ArrayList<>();
    for (BuildableManifestTemplate.ContentDescriptorTemplate layerObjectTemplate : manifestTemplate.getLayers()) {
        if (layerObjectTemplate.getDigest() == null) {
            throw new IllegalArgumentException("All layers in the manifest template must have digest set");
        }
        layers.add(new ReferenceNoDiffIdLayer(new BlobDescriptor(layerObjectTemplate.getSize(), layerObjectTemplate.getDigest())));
    }
    List<DescriptorDigest> diffIds = containerConfigurationTemplate.getDiffIds();
    if (layers.size() != diffIds.size()) {
        throw new LayerCountMismatchException("Mismatch between image manifest and container configuration");
    }
    for (int layerIndex = 0; layerIndex < layers.size(); layerIndex++) {
        ReferenceNoDiffIdLayer noDiffIdLayer = layers.get(layerIndex);
        DescriptorDigest diffId = diffIds.get(layerIndex);
        Layer layer = new ReferenceLayer(noDiffIdLayer.getBlobDescriptor(), diffId);
        image.addLayer(layer);
    }
    if (containerConfigurationTemplate.getContainerEntrypoint() == null) {
        throw new IllegalArgumentException("containerConfigurationTemplate must have an entrypoint");
    }
    image.setEntrypoint(containerConfigurationTemplate.getContainerEntrypoint());
    for (String environmentVariable : containerConfigurationTemplate.getContainerEnvironment()) {
        image.addEnvironmentVariableDefinition(environmentVariable);
    }
    return image;
}
Also used : LayerCountMismatchException(com.google.cloud.tools.jib.image.LayerCountMismatchException) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) ArrayList(java.util.ArrayList) Image(com.google.cloud.tools.jib.image.Image) ReferenceNoDiffIdLayer(com.google.cloud.tools.jib.image.ReferenceNoDiffIdLayer) Layer(com.google.cloud.tools.jib.image.Layer) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) DigestOnlyLayer(com.google.cloud.tools.jib.image.DigestOnlyLayer) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) ReferenceNoDiffIdLayer(com.google.cloud.tools.jib.image.ReferenceNoDiffIdLayer)

Example 4 with Layer

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

the class ImageToJsonTranslatorTest method setUp.

@Before
public void setUp() throws DigestException, LayerPropertyNotFoundException {
    Image testImage = new Image();
    testImage.setEnvironmentVariable("VAR1", "VAL1");
    testImage.setEnvironmentVariable("VAR2", "VAL2");
    testImage.setEntrypoint(Arrays.asList("some", "entrypoint", "command"));
    DescriptorDigest fakeDigest = DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");
    Layer fakeLayer = new ReferenceLayer(new BlobDescriptor(1000, fakeDigest), fakeDigest);
    testImage.addLayer(fakeLayer);
    imageToJsonTranslator = new ImageToJsonTranslator(testImage);
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) Image(com.google.cloud.tools.jib.image.Image) Layer(com.google.cloud.tools.jib.image.Layer) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) Before(org.junit.Before)

Example 5 with Layer

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

the class JsonToImageTranslatorTest method testToImage_v21.

@Test
public void testToImage_v21() throws IOException, LayerPropertyNotFoundException, DigestException, URISyntaxException {
    // Loads the JSON string.
    Path jsonFile = Paths.get(getClass().getClassLoader().getResource("json/v21manifest.json").toURI());
    // Deserializes into a manifest JSON object.
    V21ManifestTemplate manifestTemplate = JsonTemplateMapper.readJsonFromFile(jsonFile, V21ManifestTemplate.class);
    Image image = JsonToImageTranslator.toImage(manifestTemplate);
    List<Layer> layers = image.getLayers();
    Assert.assertEquals(1, layers.size());
    Assert.assertEquals(DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"), layers.get(0).getBlobDescriptor().getDigest());
}
Also used : Path(java.nio.file.Path) Image(com.google.cloud.tools.jib.image.Image) Layer(com.google.cloud.tools.jib.image.Layer) Test(org.junit.Test)

Aggregations

Layer (com.google.cloud.tools.jib.image.Layer)7 Image (com.google.cloud.tools.jib.image.Image)5 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)4 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)3 ReferenceLayer (com.google.cloud.tools.jib.image.ReferenceLayer)3 DigestOnlyLayer (com.google.cloud.tools.jib.image.DigestOnlyLayer)2 ReferenceNoDiffIdLayer (com.google.cloud.tools.jib.image.ReferenceNoDiffIdLayer)2 Path (java.nio.file.Path)2 LayerCountMismatchException (com.google.cloud.tools.jib.image.LayerCountMismatchException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 Test (org.junit.Test)1