use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class BlobPusherIntegrationTest method testPush.
@Test
public void testPush() throws DigestException, IOException, RegistryException {
Blob testBlob = Blobs.from("crepecake");
// Known digest for 'crepecake'
DescriptorDigest testBlobDigest = DescriptorDigest.fromHash("52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "testimage", httpClient).newRegistryClient();
Assert.assertFalse(registryClient.pushBlob(testBlobDigest, testBlob, null, ignored -> {
}));
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class CacheStorageWriter method writeUncompressedLayerBlobToDirectory.
/**
* Writes an uncompressed {@code layerBlob} to the {@code layerDirectory}.
*
* @param uncompressedLayerBlob the uncompressed layer {@link Blob}
* @param layerDirectory the directory for the layer
* @return a {@link WrittenLayer} with the written layer information
* @throws IOException if an I/O exception occurs
*/
private WrittenLayer writeUncompressedLayerBlobToDirectory(Blob uncompressedLayerBlob, Path layerDirectory) throws IOException {
Path temporaryLayerFile = cacheStorageFiles.getTemporaryLayerFile(layerDirectory);
try (CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(new BufferedOutputStream(Files.newOutputStream(temporaryLayerFile)))) {
// Writes the layer with GZIP compression. The original bytes are captured as the layer's
// diff ID and the bytes outputted from the GZIP compression are captured as the layer's
// content descriptor.
GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream);
DescriptorDigest layerDiffId = uncompressedLayerBlob.writeTo(compressorStream).getDigest();
// The GZIPOutputStream must be closed in order to write out the remaining compressed data.
compressorStream.close();
BlobDescriptor blobDescriptor = compressedDigestOutputStream.computeDigest();
DescriptorDigest layerDigest = blobDescriptor.getDigest();
long layerSize = blobDescriptor.getSize();
// Renames the temporary layer file to the correct filename.
Path layerFile = layerDirectory.resolve(cacheStorageFiles.getLayerFilename(layerDiffId));
moveIfDoesNotExist(temporaryLayerFile, layerFile);
return new WrittenLayer(layerDigest, layerDiffId, layerSize);
}
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class CacheStorageWriter method writeCompressedLayerBlobToDirectory.
/**
* Writes a compressed {@code layerBlob} to the {@code layerDirectory}.
*
* @param compressedLayerBlob the compressed layer {@link Blob}
* @param layerDirectory the directory for the layer
* @return a {@link WrittenLayer} with the written layer information
* @throws IOException if an I/O exception occurs
*/
private WrittenLayer writeCompressedLayerBlobToDirectory(Blob compressedLayerBlob, Path layerDirectory) throws IOException {
// Writes the layer file to the temporary directory.
Path temporaryLayerFile = cacheStorageFiles.getTemporaryLayerFile(layerDirectory);
BlobDescriptor layerBlobDescriptor;
try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(temporaryLayerFile))) {
layerBlobDescriptor = compressedLayerBlob.writeTo(fileOutputStream);
}
// Gets the diff ID.
DescriptorDigest layerDiffId = getDiffIdByDecompressingFile(temporaryLayerFile);
// Renames the temporary layer file to the correct filename.
Path layerFile = layerDirectory.resolve(cacheStorageFiles.getLayerFilename(layerDiffId));
moveIfDoesNotExist(temporaryLayerFile, layerFile);
return new WrittenLayer(layerBlobDescriptor.getDigest(), layerDiffId, layerBlobDescriptor.getSize());
}
use of com.google.cloud.tools.jib.api.DescriptorDigest 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.
*
* @param manifestTemplate the template containing the image layers.
* @param containerConfigurationTemplate the template containing the diff IDs and container
* configuration properties.
* @return the translated {@link Image}.
* @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
* information.
* @throws LayerPropertyNotFoundException if adding image layers fails.
* @throws BadContainerConfigurationFormatException if the container configuration is in a bad
* format
*/
public static Image toImage(BuildableManifestTemplate manifestTemplate, ContainerConfigurationTemplate containerConfigurationTemplate) throws LayerCountMismatchException, LayerPropertyNotFoundException, BadContainerConfigurationFormatException {
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");
}
Image.Builder imageBuilder = Image.builder(manifestTemplate.getClass());
for (int layerIndex = 0; layerIndex < layers.size(); layerIndex++) {
ReferenceNoDiffIdLayer noDiffIdLayer = layers.get(layerIndex);
DescriptorDigest diffId = diffIds.get(layerIndex);
imageBuilder.addLayer(new ReferenceLayer(noDiffIdLayer.getBlobDescriptor(), diffId));
}
configureBuilderWithContainerConfiguration(imageBuilder, containerConfigurationTemplate);
return imageBuilder.build();
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class JsonToImageTranslator method toImage.
/**
* Translates {@link V21ManifestTemplate} to {@link Image}.
*
* @param manifestTemplate the template containing the image layers.
* @return the translated {@link Image}.
* @throws LayerPropertyNotFoundException if adding image layers fails.
* @throws BadContainerConfigurationFormatException if the container configuration is in a bad
* format
*/
public static Image toImage(V21ManifestTemplate manifestTemplate) throws LayerPropertyNotFoundException, BadContainerConfigurationFormatException {
Image.Builder imageBuilder = Image.builder(V21ManifestTemplate.class);
// V21 layers are in reverse order of V22. (The first layer is the latest one.)
for (DescriptorDigest digest : Lists.reverse(manifestTemplate.getLayerDigests())) {
imageBuilder.addLayer(new DigestOnlyLayer(digest));
}
Optional<ContainerConfigurationTemplate> configuration = manifestTemplate.getContainerConfiguration();
if (configuration.isPresent()) {
configureBuilderWithContainerConfiguration(imageBuilder, configuration.get());
}
return imageBuilder.build();
}
Aggregations