Search in sources :

Example 31 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.

the class JibRunHelper method assertThatExpectedImageDigestAndIdReturned.

static void assertThatExpectedImageDigestAndIdReturned(Path projectRoot) throws IOException, DigestException {
    Path digestPath = projectRoot.resolve("build/jib-image.digest");
    Assert.assertTrue(Files.exists(digestPath));
    String digest = new String(Files.readAllBytes(digestPath), StandardCharsets.UTF_8);
    DescriptorDigest digest1 = DescriptorDigest.fromDigest(digest);
    Path idPath = projectRoot.resolve("build/jib-image.id");
    Assert.assertTrue(Files.exists(idPath));
    String id = new String(Files.readAllBytes(idPath), StandardCharsets.UTF_8);
    DescriptorDigest digest2 = DescriptorDigest.fromDigest(id);
    Assert.assertNotEquals(digest1, digest2);
}
Also used : Path(java.nio.file.Path) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest)

Example 32 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class BuildResult method fromImage.

/**
 * Gets a {@link BuildResult} from an {@link Image}.
 *
 * @param image the image
 * @param targetFormat the target format of the image
 * @return a new {@link BuildResult} with the image's digest and id
 * @throws IOException if writing the digest or container configuration fails
 */
static BuildResult fromImage(Image image, Class<? extends BuildableManifestTemplate> targetFormat) throws IOException {
    ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
    BlobDescriptor containerConfigurationBlobDescriptor = Digests.computeDigest(imageToJsonTranslator.getContainerConfiguration());
    BuildableManifestTemplate manifestTemplate = imageToJsonTranslator.getManifestTemplate(targetFormat, containerConfigurationBlobDescriptor);
    DescriptorDigest imageDigest = Digests.computeJsonDigest(manifestTemplate);
    DescriptorDigest imageId = containerConfigurationBlobDescriptor.getDigest();
    return new BuildResult(imageDigest, imageId, false);
}
Also used : 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) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)

Example 33 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class LocalBaseImageSteps method cacheDockerImageTar.

@VisibleForTesting
static LocalImage cacheDockerImageTar(BuildContext buildContext, Path tarPath, ProgressEventDispatcher.Factory progressEventDispatcherFactory, TempDirectoryProvider tempDirectoryProvider) throws IOException, LayerCountMismatchException {
    ExecutorService executorService = buildContext.getExecutorService();
    Path destination = tempDirectoryProvider.newDirectory();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(buildContext.getEventHandlers(), "Extracting tar " + tarPath + " into " + destination)) {
        TarExtractor.extract(tarPath, destination);
        InputStream manifestStream = Files.newInputStream(destination.resolve("manifest.json"));
        DockerManifestEntryTemplate loadManifest = new ObjectMapper().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true).readValue(manifestStream, DockerManifestEntryTemplate[].class)[0];
        manifestStream.close();
        Path configPath = destination.resolve(loadManifest.getConfig());
        ContainerConfigurationTemplate configurationTemplate = JsonTemplateMapper.readJsonFromFile(configPath, ContainerConfigurationTemplate.class);
        // Don't compute the digest of the loaded Java JSON instance.
        BlobDescriptor originalConfigDescriptor = Blobs.from(configPath).writeTo(ByteStreams.nullOutputStream());
        List<String> layerFiles = loadManifest.getLayerFiles();
        if (configurationTemplate.getLayerCount() != layerFiles.size()) {
            throw new LayerCountMismatchException("Invalid base image format: manifest contains " + layerFiles.size() + " layers, but container configuration contains " + configurationTemplate.getLayerCount() + " layers");
        }
        buildContext.getBaseImageLayersCache().writeLocalConfig(originalConfigDescriptor.getDigest(), configurationTemplate);
        // Check the first layer to see if the layers are compressed already. 'docker save' output
        // is uncompressed, but a jib-built tar has compressed layers.
        boolean layersAreCompressed = !layerFiles.isEmpty() && isGzipped(destination.resolve(layerFiles.get(0)));
        // Process layer blobs
        try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("processing base image layers", layerFiles.size())) {
            // Start compressing layers in parallel
            List<Future<PreparedLayer>> preparedLayers = new ArrayList<>();
            for (int index = 0; index < layerFiles.size(); index++) {
                Path layerFile = destination.resolve(layerFiles.get(index));
                DescriptorDigest diffId = configurationTemplate.getLayerDiffId(index);
                ProgressEventDispatcher.Factory layerProgressDispatcherFactory = progressEventDispatcher.newChildProducer();
                preparedLayers.add(executorService.submit(() -> compressAndCacheTarLayer(buildContext.getBaseImageLayersCache(), diffId, layerFile, layersAreCompressed, layerProgressDispatcherFactory)));
            }
            return new LocalImage(preparedLayers, configurationTemplate);
        }
    }
}
Also used : Path(java.nio.file.Path) LayerCountMismatchException(com.google.cloud.tools.jib.image.LayerCountMismatchException) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) ExecutorService(java.util.concurrent.ExecutorService) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) Future(java.util.concurrent.Future) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 34 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class LocalBaseImageSteps method getCachedDockerImage.

@VisibleForTesting
static Optional<LocalImage> getCachedDockerImage(Cache cache, DockerImageDetails dockerImageDetails) throws DigestException, IOException, CacheCorruptedException {
    // Get config
    Optional<ContainerConfigurationTemplate> cachedConfig = cache.retrieveLocalConfig(dockerImageDetails.getImageId());
    if (!cachedConfig.isPresent()) {
        return Optional.empty();
    }
    // Get layers
    List<Future<PreparedLayer>> cachedLayers = new ArrayList<>();
    for (DescriptorDigest diffId : dockerImageDetails.getDiffIds()) {
        Optional<CachedLayer> cachedLayer = cache.retrieveTarLayer(diffId);
        if (!cachedLayer.isPresent()) {
            return Optional.empty();
        }
        CachedLayer layer = cachedLayer.get();
        cachedLayers.add(Futures.immediateFuture(new PreparedLayer.Builder(layer).build()));
    }
    return Optional.of(new LocalImage(cachedLayers, cachedConfig.get()));
}
Also used : ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 35 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class BlobCheckerIntegrationTest method testCheck_exists.

@Test
public void testCheck_exists() throws IOException, RegistryException {
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/base", httpClient).newRegistryClient();
    V22ManifestTemplate manifestTemplate = registryClient.pullManifest("latest", V22ManifestTemplate.class).getManifest();
    DescriptorDigest blobDigest = manifestTemplate.getLayers().get(0).getDigest();
    Assert.assertEquals(blobDigest, registryClient.checkBlob(blobDigest).get().getDigest());
}
Also used : V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Aggregations

DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)112 Test (org.junit.Test)68 Path (java.nio.file.Path)28 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)24 Blob (com.google.cloud.tools.jib.blob.Blob)18 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)16 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)16 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)16 Image (com.google.cloud.tools.jib.image.Image)14 ByteArrayInputStream (java.io.ByteArrayInputStream)14 IOException (java.io.IOException)14 InputStream (java.io.InputStream)14 RegistryException (com.google.cloud.tools.jib.api.RegistryException)12 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)12 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)10 DigestException (java.security.DigestException)10 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)8 Layer (com.google.cloud.tools.jib.image.Layer)8 OutputStream (java.io.OutputStream)8