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);
}
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);
}
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);
}
}
}
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()));
}
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());
}
Aggregations