Search in sources :

Example 26 with V22ManifestListTemplate

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

the class ManifestPullerTest method testHandleResponse_v22ManifestListFromParentType.

@Test
public void testHandleResponse_v22ManifestListFromParentType() throws URISyntaxException, IOException, UnknownManifestFormatException {
    Path v22ManifestListFile = Paths.get(Resources.getResource("core/json/v22manifest_list.json").toURI());
    InputStream v22ManifestList = new ByteArrayInputStream(Files.readAllBytes(v22ManifestListFile));
    DescriptorDigest expectedDigest = Digests.computeDigest(v22ManifestList).getDigest();
    v22ManifestList.reset();
    Mockito.when(mockResponse.getBody()).thenReturn(v22ManifestList);
    ManifestAndDigest<?> manifestAndDigest = new ManifestPuller<>(fakeRegistryEndpointRequestProperties, "test-image-tag", ManifestTemplate.class).handleResponse(mockResponse);
    ManifestTemplate manifestTemplate = manifestAndDigest.getManifest();
    MatcherAssert.assertThat(manifestTemplate, CoreMatchers.instanceOf(V22ManifestListTemplate.class));
    Assert.assertTrue(((V22ManifestListTemplate) manifestTemplate).getManifests().size() > 0);
    Assert.assertEquals(expectedDigest, manifestAndDigest.getDigest());
}
Also used : Path(java.nio.file.Path) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 27 with V22ManifestListTemplate

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

the class RegistryClientTest method testPullManifest_manifestList.

@Test
public void testPullManifest_manifestList() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String manifestResponse = "HTTP/1.1 200 OK\nContent-Length: 403\n\n{\n" + "  \"schemaVersion\": 2,\n" + "  \"mediaType\": \"application/vnd.docker.distribution.manifest.list.v2+json\",\n" + "  \"manifests\": [\n" + "    {\n" + "      \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n" + "      \"size\": 7143,\n" + "      \"digest\": \"sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f\",\n" + "      \"platform\": {\n" + "        \"architecture\": \"amd64\",\n" + "        \"os\": \"linux\"\n" + "      }\n" + "    }\n" + "  ]\n" + "}";
    registry = new TestWebServer(false, Arrays.asList(manifestResponse), 1);
    RegistryClient registryClient = createRegistryClient(null);
    ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest("image-tag");
    Assert.assertEquals("sha256:a340fa38667f765f8cfd79d4bc684ec8a6f48cdd63abfe36e109f4125ee38488", manifestAndDigest.getDigest().toString());
    Assert.assertTrue(manifestAndDigest.getManifest() instanceof V22ManifestListTemplate);
    V22ManifestListTemplate manifestList = (V22ManifestListTemplate) manifestAndDigest.getManifest();
    Assert.assertEquals(2, manifestList.getSchemaVersion());
    Assert.assertEquals(Arrays.asList("sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f"), manifestList.getDigestsForPlatform("amd64", "linux"));
    MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.containsString("GET /v2/foo/bar/manifests/image-tag "));
}
Also used : V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 28 with V22ManifestListTemplate

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

the class PullBaseImageStep method pullBaseImages.

/**
 * Pulls the base images specified in the platforms list.
 *
 * @param registryClient to communicate with remote registry
 * @param progressDispatcherFactory the {@link ProgressEventDispatcher.Factory} for emitting
 *     {@link ProgressEvent}s
 * @return the list of pulled base images and a registry client
 * @throws IOException when an I/O exception occurs during the pulling
 * @throws RegistryException if communicating with the registry caused a known error
 * @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
 */
private List<Image> pullBaseImages(RegistryClient registryClient, ProgressEventDispatcher.Factory progressDispatcherFactory) throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, BadContainerConfigurationFormatException {
    Cache cache = buildContext.getBaseImageLayersCache();
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    ImageConfiguration baseImageConfig = buildContext.getBaseImageConfiguration();
    try (ProgressEventDispatcher progressDispatcher1 = progressDispatcherFactory.create("pulling base image manifest and container config", 2)) {
        ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest(baseImageConfig.getImageQualifier());
        eventHandlers.dispatch(LogEvent.lifecycle("Using base image with digest: " + manifestAndDigest.getDigest()));
        progressDispatcher1.dispatchProgress(1);
        ProgressEventDispatcher.Factory childProgressDispatcherFactory = progressDispatcher1.newChildProducer();
        ManifestTemplate manifestTemplate = manifestAndDigest.getManifest();
        if (manifestTemplate instanceof V21ManifestTemplate) {
            V21ManifestTemplate v21Manifest = (V21ManifestTemplate) manifestTemplate;
            cache.writeMetadata(baseImageConfig.getImage(), v21Manifest);
            return Collections.singletonList(JsonToImageTranslator.toImage(v21Manifest));
        } else if (manifestTemplate instanceof BuildableManifestTemplate) {
            // V22ManifestTemplate or OciManifestTemplate
            BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) manifestTemplate;
            ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(manifestAndDigest, registryClient, childProgressDispatcherFactory);
            PlatformChecker.checkManifestPlatform(buildContext, containerConfig);
            cache.writeMetadata(baseImageConfig.getImage(), imageManifest, containerConfig);
            return Collections.singletonList(JsonToImageTranslator.toImage(imageManifest, containerConfig));
        }
        // TODO: support OciIndexTemplate once AbstractManifestPuller starts to accept it.
        Verify.verify(manifestTemplate instanceof V22ManifestListTemplate);
        List<ManifestAndConfigTemplate> manifestsAndConfigs = new ArrayList<>();
        ImmutableList.Builder<Image> images = ImmutableList.builder();
        Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
        try (ProgressEventDispatcher progressDispatcher2 = childProgressDispatcherFactory.create("pulling platform-specific manifests and container configs", 2L * platforms.size())) {
            // If a manifest list, search for the manifests matching the given platforms.
            for (Platform platform : platforms) {
                String message = "Searching for architecture=%s, os=%s in the base image manifest list";
                eventHandlers.dispatch(LogEvent.info(String.format(message, platform.getArchitecture(), platform.getOs())));
                String manifestDigest = lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestTemplate, platform);
                // TODO: pull multiple manifests (+ container configs) in parallel.
                ManifestAndDigest<?> imageManifestAndDigest = registryClient.pullManifest(manifestDigest);
                progressDispatcher2.dispatchProgress(1);
                BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) imageManifestAndDigest.getManifest();
                ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(imageManifestAndDigest, registryClient, progressDispatcher2.newChildProducer());
                manifestsAndConfigs.add(new ManifestAndConfigTemplate(imageManifest, containerConfig, manifestDigest));
                images.add(JsonToImageTranslator.toImage(imageManifest, containerConfig));
            }
        }
        cache.writeMetadata(baseImageConfig.getImage(), new ImageMetadataTemplate(manifestTemplate, /* manifest list */
        manifestsAndConfigs));
        return images.build();
    }
}
Also used : ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) 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) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 29 with V22ManifestListTemplate

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

the class ManifestPullerIntegrationTest method testPull_v22ManifestList.

@Test
public void testPull_v22ManifestList() throws IOException, RegistryException {
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/base", httpClient).newRegistryClient();
    // Ensure ":latest" is a manifest list
    V22ManifestListTemplate manifestListTargeted = registryClient.pullManifest("latest", V22ManifestListTemplate.class).getManifest();
    Assert.assertEquals(2, manifestListTargeted.getSchemaVersion());
    Assert.assertTrue(manifestListTargeted.getManifests().size() > 0);
    // Generic call to ":latest" pulls a manifest list
    ManifestTemplate manifestListGeneric = registryClient.pullManifest("latest").getManifest();
    Assert.assertEquals(2, manifestListGeneric.getSchemaVersion());
    MatcherAssert.assertThat(manifestListGeneric, CoreMatchers.instanceOf(V22ManifestListTemplate.class));
    Assert.assertTrue(((V22ManifestListTemplate) manifestListGeneric).getManifests().size() > 0);
    // Referencing a manifest list by sha256, should return a manifest list
    ManifestTemplate manifestListSha256 = registryClient.pullManifest(KNOWN_MANIFEST_LIST_SHA).getManifest();
    Assert.assertEquals(2, manifestListSha256.getSchemaVersion());
    MatcherAssert.assertThat(manifestListSha256, CoreMatchers.instanceOf(V22ManifestListTemplate.class));
    Assert.assertTrue(((V22ManifestListTemplate) manifestListSha256).getManifests().size() > 0);
    // Call to ":latest" targeting a manifest pulls a manifest.
    V22ManifestTemplate manifestTargeted = registryClient.pullManifest("latest", V22ManifestTemplate.class).getManifest();
    Assert.assertEquals(2, manifestTargeted.getSchemaVersion());
}
Also used : V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 30 with V22ManifestListTemplate

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

the class BuildImageMojoIntegrationTest method testExecute_multiPlatformBuild.

@Test
public void testExecute_multiPlatformBuild() throws IOException, VerificationException, RegistryException {
    String targetImage = "localhost:5000/multiplatform:maven" + System.nanoTime();
    Verifier verifier = new Verifier(simpleTestProject.getProjectRoot().toString());
    verifier.setSystemProperty("_TARGET_IMAGE", targetImage);
    verifier.setSystemProperty("jib.to.auth.username", "testuser");
    verifier.setSystemProperty("jib.to.auth.password", "testpassword");
    verifier.setSystemProperty("sendCredentialsOverHttp", "true");
    verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
    verifier.setAutoclean(false);
    verifier.addCliOption("--file=pom-multiplatform-build.xml");
    verifier.executeGoals(Arrays.asList("clean", "compile", "jib:build"));
    verifier.verifyErrorFreeLog();
    FailoverHttpClient httpClient = new FailoverHttpClient(true, true, ignored -> {
    });
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "multiplatform", httpClient).setCredential(Credential.from("testuser", "testpassword")).newRegistryClient();
    registryClient.configureBasicAuth();
    // manifest list by tag ":latest"
    ManifestTemplate manifestList = registryClient.pullManifest("latest").getManifest();
    assertThat(manifestList).isInstanceOf(V22ManifestListTemplate.class);
    V22ManifestListTemplate v22ManifestList = (V22ManifestListTemplate) manifestList;
    assertThat(v22ManifestList.getManifests().size()).isEqualTo(2);
    ManifestDescriptorTemplate.Platform platform1 = v22ManifestList.getManifests().get(0).getPlatform();
    ManifestDescriptorTemplate.Platform platform2 = v22ManifestList.getManifests().get(1).getPlatform();
    assertThat(platform1.getArchitecture()).isEqualTo("arm64");
    assertThat(platform1.getOs()).isEqualTo("linux");
    assertThat(platform2.getArchitecture()).isEqualTo("amd64");
    assertThat(platform2.getOs()).isEqualTo("linux");
    // manifest list by tag ":another"
    ManifestTemplate anotherManifestList = registryClient.pullManifest("another").getManifest();
    assertThat(JsonTemplateMapper.toUtf8String(anotherManifestList)).isEqualTo(JsonTemplateMapper.toUtf8String(manifestList));
    // Check arm64/linux container config.
    List<String> arm64Digests = v22ManifestList.getDigestsForPlatform("arm64", "linux");
    assertThat(arm64Digests.size()).isEqualTo(1);
    String arm64Digest = arm64Digests.get(0);
    ManifestTemplate arm64Manifest = registryClient.pullManifest(arm64Digest).getManifest();
    assertThat(arm64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate arm64V22Manifest = (V22ManifestTemplate) arm64Manifest;
    DescriptorDigest arm64ConfigDigest = arm64V22Manifest.getContainerConfiguration().getDigest();
    Blob arm64ConfigBlob = registryClient.pullBlob(arm64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String arm64Config = Blobs.writeToString(arm64ConfigBlob);
    assertThat(arm64Config).contains("\"architecture\":\"arm64\"");
    assertThat(arm64Config).contains("\"os\":\"linux\"");
    // Check amd64/linux container config.
    List<String> amd64Digests = v22ManifestList.getDigestsForPlatform("amd64", "linux");
    assertThat(amd64Digests.size()).isEqualTo(1);
    String amd64Digest = amd64Digests.get(0);
    ManifestTemplate amd64Manifest = registryClient.pullManifest(amd64Digest).getManifest();
    assertThat(amd64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate amd64V22Manifest = (V22ManifestTemplate) amd64Manifest;
    DescriptorDigest amd64ConfigDigest = amd64V22Manifest.getContainerConfiguration().getDigest();
    Blob amd64ConfigBlob = registryClient.pullBlob(amd64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String amd64Config = Blobs.writeToString(amd64ConfigBlob);
    assertThat(amd64Config).contains("\"architecture\":\"amd64\"");
    assertThat(amd64Config).contains("\"os\":\"linux\"");
}
Also used : FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) Blob(com.google.cloud.tools.jib.blob.Blob) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Verifier(org.apache.maven.it.Verifier) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) 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