use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.
the class Cache method writeMetadata.
/**
* Saves a schema 2 manifest for an image reference. This is a simple wrapper around {@link
* #writeMetadata(ImageReference, ImageMetadataTemplate)} to save a single manifest without a
* manifest list.
*
* @param imageReference the image reference to save the manifest for
* @param manifest the V2.2 or OCI manifest
* @param containerConfiguration the container configuration
* @throws IOException if an I/O exception occurs
*/
public void writeMetadata(ImageReference imageReference, BuildableManifestTemplate manifest, ContainerConfigurationTemplate containerConfiguration) throws IOException {
List<ManifestAndConfigTemplate> singleton = Collections.singletonList(new ManifestAndConfigTemplate(manifest, containerConfiguration));
cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(null, singleton));
}
use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.
the class CacheStorageReader method verifyImageMetadata.
@VisibleForTesting
static void verifyImageMetadata(ImageMetadataTemplate metadata, Path metadataCacheDirectory) throws CacheCorruptedException {
List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.getManifestsAndConfigs();
if (manifestsAndConfigs.isEmpty()) {
throw new CacheCorruptedException(metadataCacheDirectory, "Manifest cache empty");
}
if (manifestsAndConfigs.stream().anyMatch(entry -> entry.getManifest() == null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Manifest(s) missing");
}
if (metadata.getManifestList() == null && manifestsAndConfigs.size() != 1) {
throw new CacheCorruptedException(metadataCacheDirectory, "Manifest list missing");
}
ManifestTemplate firstManifest = manifestsAndConfigs.get(0).getManifest();
if (firstManifest instanceof V21ManifestTemplate) {
if (metadata.getManifestList() != null || manifestsAndConfigs.stream().anyMatch(entry -> entry.getConfig() != null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Schema 1 manifests corrupted");
}
} else if (firstManifest instanceof BuildableManifestTemplate) {
if (manifestsAndConfigs.stream().anyMatch(entry -> entry.getConfig() == null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Schema 2 manifests corrupted");
}
if (metadata.getManifestList() != null && manifestsAndConfigs.stream().anyMatch(entry -> entry.getManifestDigest() == null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Schema 2 manifests corrupted");
}
} else {
throw new CacheCorruptedException(metadataCacheDirectory, "Unknown manifest type: " + firstManifest);
}
}
use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.
the class CacheStorageWriter method verifyImageMetadata.
private static void verifyImageMetadata(ImageMetadataTemplate metadata) {
Predicate<ManifestAndConfigTemplate> isManifestNull = pair -> pair.getManifest() == null;
Predicate<ManifestAndConfigTemplate> isConfigNull = pair -> pair.getConfig() == null;
Predicate<ManifestAndConfigTemplate> isDigestNull = pair -> pair.getManifestDigest() == null;
List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.getManifestsAndConfigs();
Preconditions.checkArgument(!manifestsAndConfigs.isEmpty(), "no manifests given");
Preconditions.checkArgument(manifestsAndConfigs.stream().noneMatch(isManifestNull), "null manifest(s)");
Preconditions.checkArgument(metadata.getManifestList() != null || manifestsAndConfigs.size() == 1, "manifest list missing while multiple manifests given");
ManifestTemplate firstManifest = manifestsAndConfigs.get(0).getManifest();
if (firstManifest instanceof V21ManifestTemplate) {
Preconditions.checkArgument(metadata.getManifestList() == null, "manifest list given for schema 1");
Preconditions.checkArgument(isConfigNull.test(manifestsAndConfigs.get(0)), "container config given for schema 1");
} else if (firstManifest instanceof BuildableManifestTemplate) {
Preconditions.checkArgument(manifestsAndConfigs.stream().noneMatch(isConfigNull), "null container config(s)");
if (metadata.getManifestList() != null) {
Preconditions.checkArgument(manifestsAndConfigs.stream().noneMatch(isDigestNull), "null manifest digest(s)");
}
} else {
throw new IllegalArgumentException("Unknown manifest type: " + firstManifest);
}
}
use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.
the class PullBaseImageStepTest method testGetCachedBaseImages_v21ManifestCached.
@Test
public void testGetCachedBaseImages_v21ManifestCached() throws InvalidImageReferenceException, IOException, CacheCorruptedException, UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException, LayerCountMismatchException, DigestException {
ImageReference imageReference = ImageReference.parse("cat");
Mockito.when(buildContext.getBaseImageConfiguration()).thenReturn(ImageConfiguration.builder(imageReference).build());
DescriptorDigest layerDigest = DescriptorDigest.fromHash("1111111111111111111111111111111111111111111111111111111111111111");
V21ManifestTemplate v21Manifest = Mockito.mock(V21ManifestTemplate.class);
Mockito.when(v21Manifest.getLayerDigests()).thenReturn(Arrays.asList(layerDigest));
ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(null, Arrays.asList(new ManifestAndConfigTemplate(v21Manifest, null)));
Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
List<Image> images = pullBaseImageStep.getCachedBaseImages();
Assert.assertEquals(1, images.size());
Assert.assertEquals(1, images.get(0).getLayers().size());
Assert.assertEquals("1111111111111111111111111111111111111111111111111111111111111111", images.get(0).getLayers().get(0).getBlobDescriptor().getDigest().getHash());
}
use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.
the class PullBaseImageStepTest method testGetCachedBaseImages_v22ManifestListCached_partialMatches.
@Test
public void testGetCachedBaseImages_v22ManifestListCached_partialMatches() throws InvalidImageReferenceException, IOException, CacheCorruptedException, UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException, LayerCountMismatchException {
ImageReference imageReference = ImageReference.parse("cat");
Mockito.when(buildContext.getBaseImageConfiguration()).thenReturn(ImageConfiguration.builder(imageReference).build());
V22ManifestListTemplate manifestList = Mockito.mock(V22ManifestListTemplate.class);
Mockito.when(manifestList.getDigestsForPlatform("arch1", "os1")).thenReturn(Arrays.asList("sha256:digest1"));
Mockito.when(manifestList.getDigestsForPlatform("arch2", "os2")).thenReturn(Arrays.asList("sha256:digest2"));
ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(manifestList, Arrays.asList(new ManifestAndConfigTemplate(new V22ManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest1")));
Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
Mockito.when(containerConfig.getPlatforms()).thenReturn(ImmutableSet.of(new Platform("arch1", "os1"), new Platform("arch2", "os2")));
Assert.assertEquals(Arrays.asList(), pullBaseImageStep.getCachedBaseImages());
}
Aggregations