use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class BlobTest method verifyBlobWriteTo.
/**
* Checks that the {@link Blob} streams the expected string.
*/
private void verifyBlobWriteTo(String expected, Blob blob) throws IOException {
OutputStream outputStream = new ByteArrayOutputStream();
BlobDescriptor blobDescriptor = blob.writeTo(outputStream);
String output = outputStream.toString();
Assert.assertEquals(expected, output);
byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8);
Assert.assertEquals(expectedBytes.length, blobDescriptor.getSize());
DescriptorDigest expectedDigest = Digests.computeDigest(new ByteArrayInputStream(expectedBytes)).getDigest();
Assert.assertEquals(expectedDigest, blobDescriptor.getDigest());
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
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();
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
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 GoogleContainerTools.
the class ImageTarball method ociWriteTo.
private void ociWriteTo(OutputStream out) throws IOException {
TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
OciManifestTemplate manifest = new OciManifestTemplate();
// Adds all the layers to the tarball and manifest
for (Layer layer : image.getLayers()) {
DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
long size = layer.getBlobDescriptor().getSize();
tarStreamBuilder.addBlobEntry(layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
manifest.addLayer(size, digest);
}
// Adds the container configuration to the tarball and manifest
JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), BLOB_PREFIX + configDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
// Adds the manifest to the tarball
BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(manifest), BLOB_PREFIX + manifestDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
// Adds the oci-layout and index.json
tarStreamBuilder.addByteEntry("{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8), "oci-layout", TAR_ENTRY_MODIFICATION_TIME);
OciIndexTemplate index = new OciIndexTemplate();
// TODO: figure out how to tag with allTargetImageTags
index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);
tarStreamBuilder.writeAsTarArchiveTo(out);
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class PushBlobStep method call.
@Override
public BlobDescriptor call() throws IOException, RegistryException {
EventHandlers eventHandlers = buildContext.getEventHandlers();
DescriptorDigest blobDigest = blobDescriptor.getDigest();
try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("pushing blob " + blobDigest, blobDescriptor.getSize());
TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION + blobDescriptor);
ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress)) {
// check if the BLOB is available
if (!forcePush && registryClient.checkBlob(blobDigest).isPresent()) {
eventHandlers.dispatch(LogEvent.info("Skipping push; BLOB already exists on target registry : " + blobDescriptor));
return blobDescriptor;
}
// If base and target images are in the same registry, then use mount/from to try mounting the
// BLOB from the base image repository to the target image repository and possibly avoid
// having to push the BLOB. See
// https://docs.docker.com/registry/spec/api/#cross-repository-blob-mount for details.
String baseRegistry = buildContext.getBaseImageConfiguration().getImageRegistry();
String baseRepository = buildContext.getBaseImageConfiguration().getImageRepository();
String targetRegistry = buildContext.getTargetImageConfiguration().getImageRegistry();
String sourceRepository = targetRegistry.equals(baseRegistry) ? baseRepository : null;
registryClient.pushBlob(blobDigest, blob, sourceRepository, throttledProgressReporter);
return blobDescriptor;
}
}
Aggregations