Search in sources :

Example 6 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor 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 7 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.

the class CacheWriter method getCachedLayer.

/**
 * @return a {@link CachedLayer} from a layer digest and the {@link CountingOutputStream} the
 *     layer BLOB was written to
 */
public CachedLayer getCachedLayer(DescriptorDigest layerDigest, CountingOutputStream countingOutputStream) throws IOException, LayerPropertyNotFoundException {
    Path layerFile = getLayerFile(layerDigest);
    countingOutputStream.close();
    CachedLayer cachedLayer = new CachedLayer(layerFile, new BlobDescriptor(countingOutputStream.getCount(), layerDigest), getDiffId(layerFile));
    cache.addLayerToMetadata(cachedLayer, null);
    return cachedLayer;
}
Also used : Path(java.nio.file.Path) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor)

Example 8 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor 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 9 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.

the class BlobCheckerTest method testHandleResponse.

@Test
public void testHandleResponse() throws RegistryErrorException {
    Mockito.when(mockResponse.getContentLength()).thenReturn(0L);
    BlobDescriptor expectedBlobDescriptor = new BlobDescriptor(0, fakeDigest);
    BlobDescriptor blobDescriptor = testBlobChecker.handleResponse(mockResponse);
    Assert.assertEquals(expectedBlobDescriptor, blobDescriptor);
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Test(org.junit.Test)

Example 10 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.

the class CountingDigestOutputStreamTest method test_smokeTest.

@Test
public void test_smokeTest() throws IOException, DigestException {
    for (Map.Entry<String, String> knownHash : knownSha256Hashes.entrySet()) {
        String toHash = knownHash.getKey();
        String expectedHash = knownHash.getValue();
        OutputStream underlyingOutputStream = new ByteArrayOutputStream();
        CountingDigestOutputStream countingDigestOutputStream = new CountingDigestOutputStream(underlyingOutputStream);
        byte[] bytesToHash = toHash.getBytes(StandardCharsets.UTF_8);
        InputStream toHashInputStream = new ByteArrayInputStream(bytesToHash);
        ByteStreams.copy(toHashInputStream, countingDigestOutputStream);
        BlobDescriptor expectedBlobDescriptor = new BlobDescriptor(bytesToHash.length, DescriptorDigest.fromHash(expectedHash));
        Assert.assertEquals(expectedBlobDescriptor, countingDigestOutputStream.toBlobDescriptor());
        Assert.assertEquals(bytesToHash.length, countingDigestOutputStream.getTotalBytes());
    }
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)17 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)6 Path (java.nio.file.Path)5 Image (com.google.cloud.tools.jib.image.Image)4 Test (org.junit.Test)4 Blob (com.google.cloud.tools.jib.blob.Blob)3 CountingDigestOutputStream (com.google.cloud.tools.jib.hash.CountingDigestOutputStream)3 Layer (com.google.cloud.tools.jib.image.Layer)3 ReferenceLayer (com.google.cloud.tools.jib.image.ReferenceLayer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Before (org.junit.Before)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 HttpResponseException (com.google.api.client.http.HttpResponseException)1 Timer (com.google.cloud.tools.jib.Timer)1 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)1 CacheMetadataLayerObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate)1 CacheMetadataLayerPropertiesObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)1 DigestOnlyLayer (com.google.cloud.tools.jib.image.DigestOnlyLayer)1 ImageLayers (com.google.cloud.tools.jib.image.ImageLayers)1