Search in sources :

Example 16 with V22ManifestListTemplate

use of com.google.cloud.tools.jib.image.json.V22ManifestListTemplate in project jib by google.

the class CacheStorageReaderTest method testVerifyImageMetadata_schema1ManifestsCorrupted_manifestListExists.

@Test
public void testVerifyImageMetadata_schema1ManifestsCorrupted_manifestListExists() {
    ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(new V21ManifestTemplate(), null);
    ImageMetadataTemplate metadata = new ImageMetadataTemplate(new V22ManifestListTemplate(), Arrays.asList(manifestAndConfig));
    try {
        CacheStorageReader.verifyImageMetadata(metadata, Paths.get("/cache/dir"));
        Assert.fail();
    } catch (CacheCorruptedException ex) {
        MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.startsWith("Schema 1 manifests corrupted"));
    }
}
Also used : V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Test(org.junit.Test)

Example 17 with V22ManifestListTemplate

use of com.google.cloud.tools.jib.image.json.V22ManifestListTemplate 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());
}
Also used : Path(java.nio.file.Path) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) Test(org.junit.Test)

Example 18 with V22ManifestListTemplate

use of com.google.cloud.tools.jib.image.json.V22ManifestListTemplate 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();
}
Also used : TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) Callable(java.util.concurrent.Callable) LayerPropertyNotFoundException(com.google.cloud.tools.jib.image.LayerPropertyNotFoundException) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) UnknownManifestFormatException(com.google.cloud.tools.jib.image.json.UnknownManifestFormatException) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) Credential(com.google.cloud.tools.jib.api.Credential) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Nullable(javax.annotation.Nullable) Verify(com.google.common.base.Verify) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) UnlistedPlatformInManifestListException(com.google.cloud.tools.jib.image.json.UnlistedPlatformInManifestListException) Collection(java.util.Collection) JsonTemplateMapper(com.google.cloud.tools.jib.json.JsonTemplateMapper) Set(java.util.Set) IOException(java.io.IOException) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) RegistryException(com.google.cloud.tools.jib.api.RegistryException) CredentialRetrievalException(com.google.cloud.tools.jib.registry.credentials.CredentialRetrievalException) Cache(com.google.cloud.tools.jib.cache.Cache) CacheCorruptedException(com.google.cloud.tools.jib.cache.CacheCorruptedException) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) LogEvent(com.google.cloud.tools.jib.api.LogEvent) ManifestAndDigest(com.google.cloud.tools.jib.registry.ManifestAndDigest) List(java.util.List) JsonToImageTranslator(com.google.cloud.tools.jib.image.json.JsonToImageTranslator) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Optional(java.util.Optional) Blobs(com.google.cloud.tools.jib.blob.Blobs) BadContainerConfigurationFormatException(com.google.cloud.tools.jib.image.json.BadContainerConfigurationFormatException) Preconditions(com.google.common.base.Preconditions) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LayerCountMismatchException(com.google.cloud.tools.jib.image.LayerCountMismatchException) Collections(java.util.Collections) Image(com.google.cloud.tools.jib.image.Image) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImmutableList(com.google.common.collect.ImmutableList) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Image(com.google.cloud.tools.jib.image.Image) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 19 with V22ManifestListTemplate

use of com.google.cloud.tools.jib.image.json.V22ManifestListTemplate in project jib by google.

the class BuildManifestListOrSingleManifestStepTest method testCall_manifestList.

@Test
public void testCall_manifestList() throws IOException {
    // Expected Manifest List JSON
    // {
    // "schemaVersion":2,
    // "mediaType":"application/vnd.docker.distribution.manifest.list.v2+json",
    // "manifests":[
    // {
    // "mediaType":"application/vnd.docker.distribution.manifest.v2+json",
    // "digest":"sha256:9467fc431ac5dd84dafdc13f75111fc467cd57aff4732edda8c9e0bbcabe0183",
    // "size":338,
    // "platform":{
    // "architecture":"amd64",
    // "os":"linux"
    // }
    // },
    // {
    // "mediaType":"application/vnd.docker.distribution.manifest.v2+json",
    // "digest":"sha256:439351c848845c46a3952f28416992b66003361d00943b6cdb04b6d5533f02bf",
    // "size":338,
    // "platform":{
    // "architecture":"arm64",
    // "os":"windows"
    // }
    // }
    // ]
    // }
    ManifestTemplate manifestTemplate = new BuildManifestListOrSingleManifestStep(buildContext, progressDispatcherFactory, Arrays.asList(image1, image2)).call();
    Assert.assertTrue(manifestTemplate instanceof V22ManifestListTemplate);
    V22ManifestListTemplate manifestList = (V22ManifestListTemplate) manifestTemplate;
    Assert.assertEquals(2, manifestList.getSchemaVersion());
    Assert.assertEquals(Arrays.asList("sha256:9467fc431ac5dd84dafdc13f75111fc467cd57aff4732edda8c9e0bbcabe0183"), manifestList.getDigestsForPlatform("amd64", "linux"));
    Assert.assertEquals(Arrays.asList("sha256:439351c848845c46a3952f28416992b66003361d00943b6cdb04b6d5533f02bf"), manifestList.getDigestsForPlatform("arm64", "windows"));
}
Also used : V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 20 with V22ManifestListTemplate

use of com.google.cloud.tools.jib.image.json.V22ManifestListTemplate in project jib by google.

the class PullBaseImageStepTest method testGetCachedBaseImages_v22ManifestListCached.

@Test
public void testGetCachedBaseImages_v22ManifestListCached() throws InvalidImageReferenceException, IOException, CacheCorruptedException, UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException, LayerCountMismatchException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration()).thenReturn(ImageConfiguration.builder(imageReference).build());
    ContainerConfigurationTemplate containerConfigJson1 = new ContainerConfigurationTemplate();
    ContainerConfigurationTemplate containerConfigJson2 = new ContainerConfigurationTemplate();
    containerConfigJson1.setContainerUser("user1");
    containerConfigJson2.setContainerUser("user2");
    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(), containerConfigJson1, "sha256:digest1"), new ManifestAndConfigTemplate(new V22ManifestTemplate(), containerConfigJson2, "sha256:digest2")));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(containerConfig.getPlatforms()).thenReturn(ImmutableSet.of(new Platform("arch1", "os1"), new Platform("arch2", "os2")));
    List<Image> images = pullBaseImageStep.getCachedBaseImages();
    Assert.assertEquals(2, images.size());
    Assert.assertEquals("user1", images.get(0).getUser());
    Assert.assertEquals("user2", images.get(1).getUser());
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Image(com.google.cloud.tools.jib.image.Image) Test(org.junit.Test)

Aggregations

V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)38 Test (org.junit.Test)34 ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)22 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)22 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)22 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)18 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)14 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)12 ImageReference (com.google.cloud.tools.jib.api.ImageReference)10 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)10 Image (com.google.cloud.tools.jib.image.Image)8 ManifestDescriptorTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate)8 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)6 Path (java.nio.file.Path)6 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)4 Cache (com.google.cloud.tools.jib.cache.Cache)4 ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)4 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)4 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)4 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)4