Search in sources :

Example 11 with BlobDescriptor

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

the class ImageLayersTest method setUpFakes.

@Before
public void setUpFakes() throws LayerPropertyNotFoundException {
    DescriptorDigest mockDescriptorDigest1 = Mockito.mock(DescriptorDigest.class);
    DescriptorDigest mockDescriptorDigest2 = Mockito.mock(DescriptorDigest.class);
    DescriptorDigest mockDescriptorDigest3 = Mockito.mock(DescriptorDigest.class);
    BlobDescriptor cachedLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest1);
    BlobDescriptor referenceLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest2);
    BlobDescriptor referenceNoDiffIdLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest3);
    // Intentionally the same digest as the mockCachedLayer.
    BlobDescriptor unwrittenLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest1);
    Mockito.when(mockCachedLayer.getBlobDescriptor()).thenReturn(cachedLayerBlobDescriptor);
    Mockito.when(mockReferenceLayer.getBlobDescriptor()).thenReturn(referenceLayerBlobDescriptor);
    Mockito.when(mockDigestOnlyLayer.getBlobDescriptor()).thenReturn(referenceNoDiffIdLayerBlobDescriptor);
    Mockito.when(mockUnwrittenLayer.getBlobDescriptor()).thenReturn(unwrittenLayerBlobDescriptor);
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Before(org.junit.Before)

Example 12 with BlobDescriptor

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

the class CacheReaderTest method testAreBaseImageLayersCached.

@Test
public void testAreBaseImageLayersCached() throws DigestException, LayerPropertyNotFoundException, CacheMetadataCorruptedException, IOException {
    ImageLayers<ReferenceLayer> layers = new ImageLayers<>();
    layers.add(new ReferenceLayer(new BlobDescriptor(1000, DescriptorDigest.fromDigest("sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")), DescriptorDigest.fromDigest("sha256:b56ae66c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a4647"))).add(new ReferenceLayer(new BlobDescriptor(1001, DescriptorDigest.fromDigest("sha256:6f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")), DescriptorDigest.fromDigest("sha256:b56ae66c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a4647"))).add(new ReferenceLayer(new BlobDescriptor(2000, DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad")), DescriptorDigest.fromDigest("sha256:a3f3e99c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a8372")));
    try (Cache cache = Cache.init(testCacheFolder)) {
        CacheReader cacheReader = new CacheReader(cache);
        Assert.assertNotNull(cacheReader.getLayer(DescriptorDigest.fromDigest("sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")));
        Assert.assertNull(cacheReader.getLayer(DescriptorDigest.fromDigest("sha256:6f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")));
        Assert.assertNotNull(cacheReader.getLayer(DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad")));
    }
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) ImageLayers(com.google.cloud.tools.jib.image.ImageLayers) Test(org.junit.Test)

Example 13 with BlobDescriptor

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

the class CacheWriterTest method getExpectedLayer.

/**
 * @return the expected layer to test against, represented by the {@code resourceBlob} resource
 *     file
 */
private ExpectedLayer getExpectedLayer() throws IOException {
    String expectedBlobAString = new String(Files.readAllBytes(resourceBlob), StandardCharsets.UTF_8);
    // Gets the expected content descriptor, diff ID, and compressed BLOB.
    ByteArrayOutputStream compressedBlobOutputStream = new ByteArrayOutputStream();
    CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(compressedBlobOutputStream);
    CountingDigestOutputStream uncompressedDigestOutputStream;
    try (GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream)) {
        uncompressedDigestOutputStream = new CountingDigestOutputStream(compressorStream);
        uncompressedDigestOutputStream.write(expectedBlobAString.getBytes(StandardCharsets.UTF_8));
    }
    BlobDescriptor expectedBlobADescriptor = compressedDigestOutputStream.toBlobDescriptor();
    DescriptorDigest expectedBlobADiffId = uncompressedDigestOutputStream.toBlobDescriptor().getDigest();
    ByteArrayInputStream compressedBlobInputStream = new ByteArrayInputStream(compressedBlobOutputStream.toByteArray());
    Blob blob = Blobs.from(compressedBlobInputStream);
    return new ExpectedLayer(expectedBlobADescriptor, expectedBlobADiffId, blob);
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Blob(com.google.cloud.tools.jib.blob.Blob) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with BlobDescriptor

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

the class BuildAndPushContainerConfigurationStep method afterBaseImageLayerFuturesFuture.

/**
 * Depends on {@code pushAuthorizationFuture}, {@code pullBaseImageLayerFuturesFuture.get()}, and
 * {@code buildApplicationLayerFutures}.
 */
private BlobDescriptor afterBaseImageLayerFuturesFuture() throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException, RegistryException {
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pushAuthorizationFuture), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
        // Constructs the image.
        Image image = new Image();
        for (Future<CachedLayer> cachedLayerFuture : NonBlockingFutures.get(pullBaseImageLayerFuturesFuture)) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        for (Future<CachedLayer> cachedLayerFuture : buildApplicationLayerFutures) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        image.setEnvironment(buildConfiguration.getEnvironment());
        image.setEntrypoint(entrypoint);
        ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
        // Gets the container configuration content descriptor.
        Blob containerConfigurationBlob = imageToJsonTranslator.getContainerConfigurationBlob();
        CountingDigestOutputStream digestOutputStream = new CountingDigestOutputStream(ByteStreams.nullOutputStream());
        containerConfigurationBlob.writeTo(digestOutputStream);
        BlobDescriptor containerConfigurationBlobDescriptor = digestOutputStream.toBlobDescriptor();
        timer.lap("Pushing container configuration " + containerConfigurationBlobDescriptor.getDigest());
        // TODO: Use PushBlobStep.
        // Pushes the container configuration.
        registryClient.pushBlob(containerConfigurationBlobDescriptor.getDigest(), containerConfigurationBlob);
        return containerConfigurationBlobDescriptor;
    }
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Timer(com.google.cloud.tools.jib.Timer) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Image(com.google.cloud.tools.jib.image.Image)

Example 15 with BlobDescriptor

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

the class CacheMetadataTranslator method fromTemplate.

/**
 * Translates {@link CacheMetadataTemplate} to {@link CacheMetadata}.
 */
static CacheMetadata fromTemplate(CacheMetadataTemplate template, Path cacheDirectory) throws CacheMetadataCorruptedException {
    try {
        CacheMetadata cacheMetadata = new CacheMetadata();
        // Converts each layer object in the template to a cache metadata layer.
        for (CacheMetadataLayerObjectTemplate layerObjectTemplate : template.getLayers()) {
            if (layerObjectTemplate.getDigest() == null || layerObjectTemplate.getDiffId() == null) {
                throw new IllegalStateException("Cannot translate cache metadata layer without a digest or diffId");
            }
            Path layerContentFile = CacheFiles.getLayerFile(cacheDirectory, layerObjectTemplate.getDigest());
            // Gets the properties for a layer. Properties only exist for application layers.
            CacheMetadataLayerPropertiesObjectTemplate propertiesObjectTemplate = layerObjectTemplate.getProperties();
            // Constructs the cache metadata layer from a cached layer and layer metadata.
            LayerMetadata layerMetadata = null;
            if (propertiesObjectTemplate != null) {
                layerMetadata = new LayerMetadata(propertiesObjectTemplate.getSourceFiles(), propertiesObjectTemplate.getLastModifiedTime());
            }
            CachedLayer cachedLayer = new CachedLayer(layerContentFile, new BlobDescriptor(layerObjectTemplate.getSize(), layerObjectTemplate.getDigest()), layerObjectTemplate.getDiffId());
            CachedLayerWithMetadata cachedLayerWithMetadata = new CachedLayerWithMetadata(cachedLayer, layerMetadata);
            cacheMetadata.addLayer(cachedLayerWithMetadata);
        }
        return cacheMetadata;
    } catch (LayerPropertyNotFoundException ex) {
        throw new CacheMetadataCorruptedException(ex);
    }
}
Also used : Path(java.nio.file.Path) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) LayerPropertyNotFoundException(com.google.cloud.tools.jib.image.LayerPropertyNotFoundException) CacheMetadataLayerObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate) CacheMetadataLayerPropertiesObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)

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