use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class CacheStorageReaderTest method testVerifyImageMetadata_validV21.
@Test
public void testVerifyImageMetadata_validV21() throws CacheCorruptedException {
ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(new V21ManifestTemplate(), null);
ImageMetadataTemplate metadata = new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig));
CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
// should pass without CacheCorruptedException
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class CacheStorageWriterTest method testWriteMetadata_v22.
@Test
public void testWriteMetadata_v22() throws IOException, URISyntaxException, InvalidImageReferenceException {
ContainerConfigurationTemplate containerConfig = loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class);
V22ManifestTemplate manifest1 = loadJsonResource("core/json/v22manifest.json", V22ManifestTemplate.class);
V22ManifestTemplate manifest2 = loadJsonResource("core/json/v22manifest_optional_properties.json", V22ManifestTemplate.class);
V22ManifestListTemplate manifestList = loadJsonResource("core/json/v22manifest_list.json", V22ManifestListTemplate.class);
ImageReference imageReference = ImageReference.parse("image.reference/project/thing:tag");
List<ManifestAndConfigTemplate> manifestsAndConfigs = Arrays.asList(new ManifestAndConfigTemplate(manifest1, containerConfig, "sha256:digest"), new ManifestAndConfigTemplate(manifest2, containerConfig, "sha256:digest"));
cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(manifestList, manifestsAndConfigs));
Path savedMetadataPath = cacheRoot.resolve("images/image.reference/project/thing!tag/manifests_configs.json");
Assert.assertTrue(Files.exists(savedMetadataPath));
ImageMetadataTemplate savedMetadata = JsonTemplateMapper.readJsonFromFile(savedMetadataPath, ImageMetadataTemplate.class);
MatcherAssert.assertThat(savedMetadata.getManifestList(), CoreMatchers.instanceOf(V22ManifestListTemplate.class));
List<ManifestDescriptorTemplate> savedManifestDescriptors = ((V22ManifestListTemplate) savedMetadata.getManifestList()).getManifests();
Assert.assertEquals(3, savedManifestDescriptors.size());
Assert.assertEquals("sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f", savedManifestDescriptors.get(0).getDigest());
Assert.assertEquals("sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270", savedManifestDescriptors.get(1).getDigest());
Assert.assertEquals("sha256:cccbcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501999", savedManifestDescriptors.get(2).getDigest());
Assert.assertEquals(2, savedMetadata.getManifestsAndConfigs().size());
ManifestAndConfigTemplate savedManifestAndConfig1 = savedMetadata.getManifestsAndConfigs().get(0);
ManifestAndConfigTemplate savedManifestAndConfig2 = savedMetadata.getManifestsAndConfigs().get(1);
V22ManifestTemplate savedManifest1 = (V22ManifestTemplate) savedManifestAndConfig1.getManifest();
V22ManifestTemplate savedManifest2 = (V22ManifestTemplate) savedManifestAndConfig2.getManifest();
Assert.assertEquals(2, savedManifest1.getSchemaVersion());
Assert.assertEquals(2, savedManifest2.getSchemaVersion());
Assert.assertEquals(1, savedManifest1.getLayers().size());
Assert.assertEquals("4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236", savedManifest1.getLayers().get(0).getDigest().getHash());
Assert.assertEquals(Arrays.asList("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), savedManifest2.getLayers().stream().map(layer -> layer.getDigest().getHash()).collect(Collectors.toList()));
Assert.assertEquals("8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad", savedManifest1.getContainerConfiguration().getDigest().getHash());
Assert.assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", savedManifest2.getContainerConfiguration().getDigest().getHash());
Assert.assertEquals("wasm", savedManifestAndConfig1.getConfig().getArchitecture());
Assert.assertEquals("wasm", savedManifestAndConfig2.getConfig().getArchitecture());
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class CacheStorageWriterTest method testWriteMetadata_oci.
@Test
public void testWriteMetadata_oci() throws URISyntaxException, IOException, InvalidImageReferenceException {
ContainerConfigurationTemplate containerConfig = loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class);
OciManifestTemplate manifest = loadJsonResource("core/json/ocimanifest.json", OciManifestTemplate.class);
OciIndexTemplate ociIndex = loadJsonResource("core/json/ociindex.json", OciIndexTemplate.class);
ImageReference imageReference = ImageReference.parse("image.reference/project/thing:tag");
cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(ociIndex, Arrays.asList(new ManifestAndConfigTemplate(manifest, containerConfig, "sha256:digest"))));
Path savedMetadataPath = cacheRoot.resolve("images/image.reference/project/thing!tag/manifests_configs.json");
Assert.assertTrue(Files.exists(savedMetadataPath));
ImageMetadataTemplate savedMetadata = JsonTemplateMapper.readJsonFromFile(savedMetadataPath, ImageMetadataTemplate.class);
MatcherAssert.assertThat(savedMetadata.getManifestList(), CoreMatchers.instanceOf(OciIndexTemplate.class));
List<ContentDescriptorTemplate> savedManifestDescriptors = ((OciIndexTemplate) savedMetadata.getManifestList()).getManifests();
Assert.assertEquals(1, savedManifestDescriptors.size());
Assert.assertEquals("8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad", savedManifestDescriptors.get(0).getDigest().getHash());
Assert.assertEquals(1, savedMetadata.getManifestsAndConfigs().size());
ManifestAndConfigTemplate savedManifestAndConfig = savedMetadata.getManifestsAndConfigs().get(0);
OciManifestTemplate savedManifest1 = (OciManifestTemplate) savedManifestAndConfig.getManifest();
Assert.assertEquals(2, savedManifest1.getSchemaVersion());
Assert.assertEquals(1, savedManifest1.getLayers().size());
Assert.assertEquals("4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236", savedManifest1.getLayers().get(0).getDigest().getHash());
Assert.assertEquals("8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad", savedManifest1.getContainerConfiguration().getDigest().getHash());
Assert.assertEquals("wasm", savedManifestAndConfig.getConfig().getArchitecture());
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class PullBaseImageStep method getCachedBaseImages.
/**
* Retrieves the cached base images. If a base image reference is not a manifest list, returns a
* single image (if cached). If a manifest list, returns all the images matching the configured
* platforms in the manifest list but only when all of the images are cached.
*
* @return the cached images, if found
* @throws IOException when an I/O exception occurs
* @throws CacheCorruptedException if the cache is corrupted
* @throws LayerPropertyNotFoundException if adding image layers fails
* @throws BadContainerConfigurationFormatException if the container configuration is in a bad
* format
* @throws UnlistedPlatformInManifestListException if a cached manifest list has no manifests
* matching the configured platform
*/
@VisibleForTesting
List<Image> getCachedBaseImages() throws IOException, CacheCorruptedException, BadContainerConfigurationFormatException, LayerCountMismatchException, UnlistedPlatformInManifestListException {
ImageReference baseImage = buildContext.getBaseImageConfiguration().getImage();
Optional<ImageMetadataTemplate> metadata = buildContext.getBaseImageLayersCache().retrieveMetadata(baseImage);
if (!metadata.isPresent()) {
return Collections.emptyList();
}
ManifestTemplate manifestList = metadata.get().getManifestList();
List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.get().getManifestsAndConfigs();
if (manifestList == null) {
Verify.verify(manifestsAndConfigs.size() == 1);
ManifestTemplate manifest = manifestsAndConfigs.get(0).getManifest();
if (manifest instanceof V21ManifestTemplate) {
return Collections.singletonList(JsonToImageTranslator.toImage((V21ManifestTemplate) manifest));
}
ContainerConfigurationTemplate containerConfig = Verify.verifyNotNull(manifestsAndConfigs.get(0).getConfig());
PlatformChecker.checkManifestPlatform(buildContext, containerConfig);
return Collections.singletonList(JsonToImageTranslator.toImage((BuildableManifestTemplate) Verify.verifyNotNull(manifest), containerConfig));
}
// Manifest list cached. Identify matching platforms and check if all of them are cached.
ImmutableList.Builder<Image> images = ImmutableList.builder();
for (Platform platform : buildContext.getContainerConfiguration().getPlatforms()) {
String manifestDigest = lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestList, platform);
Optional<ManifestAndConfigTemplate> manifestAndConfigFound = manifestsAndConfigs.stream().filter(entry -> manifestDigest.equals(entry.getManifestDigest())).findFirst();
if (!manifestAndConfigFound.isPresent()) {
return Collections.emptyList();
}
ManifestTemplate manifest = Verify.verifyNotNull(manifestAndConfigFound.get().getManifest());
ContainerConfigurationTemplate containerConfig = Verify.verifyNotNull(manifestAndConfigFound.get().getConfig());
images.add(JsonToImageTranslator.toImage((BuildableManifestTemplate) manifest, containerConfig));
}
return images.build();
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class Cache method writeMetadata.
/**
* Saves a V2.1 image manifest. 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 manifestTemplate the V2.1 manifest
* @throws IOException if an I/O exception occurs
*/
public void writeMetadata(ImageReference imageReference, V21ManifestTemplate manifestTemplate) throws IOException {
List<ManifestAndConfigTemplate> singleton = Collections.singletonList(new ManifestAndConfigTemplate(manifestTemplate, null));
cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(null, singleton));
}
Aggregations