Search in sources :

Example 1 with ReferenceNoDiffIdLayer

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

Aggregations

BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)1 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)1 DigestOnlyLayer (com.google.cloud.tools.jib.image.DigestOnlyLayer)1 Image (com.google.cloud.tools.jib.image.Image)1 Layer (com.google.cloud.tools.jib.image.Layer)1 LayerCountMismatchException (com.google.cloud.tools.jib.image.LayerCountMismatchException)1 ReferenceLayer (com.google.cloud.tools.jib.image.ReferenceLayer)1 ReferenceNoDiffIdLayer (com.google.cloud.tools.jib.image.ReferenceNoDiffIdLayer)1 ArrayList (java.util.ArrayList)1