Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building multi-platform OCI index #3975

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ To inspect the image that is produced from the build using Docker, you can use c

### How do I specify a platform in the manifest list (or OCI index) of a base image?

Newer Jib verisons added an _incubating feature_ that provides support for selecting base images with the desired platforms from a manifest list. For example,
Newer Jib versions added an _incubating feature_ that provides support for selecting base images with the desired platforms from a manifest list. For example,

```xml
<from>
Expand Down Expand Up @@ -534,7 +534,6 @@ The default when not specified is a single "amd64/linux" platform, whose behavio
When multiple platforms are specified, Jib creates and pushes a manifest list (also known as a fat manifest) after building and pushing all the images for the specified platforms.

As an incubating feature, there are certain limitations:
- OCI image indices are not supported (as opposed to Docker manifest lists).
- Only `architecture` and `os` are supported. If the base image manifest list contains multiple images with the given architecture and os, the first image will be selected.
- Does not support using a local Docker daemon or tarball image for a base image.
- Does not support pushing to a Docker daemon (`jib:dockerBuild` / `jibDockerBuild`) or building a local tarball (`jib:buildTar` / `jibBuildTar`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package com.google.cloud.tools.jib.api;

import com.google.cloud.tools.jib.Command;
import com.google.cloud.tools.jib.api.buildplan.ImageFormat;
import com.google.cloud.tools.jib.api.buildplan.Platform;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.event.EventHandlers;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.image.json.OciIndexTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestListTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
Expand Down Expand Up @@ -292,6 +294,32 @@ public void testScratch_multiPlatform()
Assert.assertEquals("windows", platform2.getOs());
}

@Test
public void testScratch_multiPlatformOci()
throws IOException, InterruptedException, ExecutionException, RegistryException,
CacheDirectoryCreationException, InvalidImageReferenceException {
Jib.fromScratch()
.setFormat(ImageFormat.OCI)
.setPlatforms(
ImmutableSet.of(new Platform("arm64", "windows"), new Platform("amd32", "windows")))
.containerize(
Containerizer.to(RegistryImage.named(dockerHost + ":5000/jib-scratch:multi-platform"))
.setAllowInsecureRegistries(true));

OciIndexTemplate manifestList =
(OciIndexTemplate) registryClient.pullManifest("multi-platform").getManifest();
Assert.assertEquals(2, manifestList.getManifests().size());
OciIndexTemplate.ManifestDescriptorTemplate.Platform platform1 =
manifestList.getManifests().get(0).getPlatform();
OciIndexTemplate.ManifestDescriptorTemplate.Platform platform2 =
manifestList.getManifests().get(1).getPlatform();

Assert.assertEquals("arm64", platform1.getArchitecture());
Assert.assertEquals("windows", platform1.getOs());
Assert.assertEquals("amd32", platform2.getArchitecture());
Assert.assertEquals("windows", platform2.getOs());
}

@Test
public void testOffline()
throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.cloud.tools.jib.blob.BlobDescriptor;
import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate;
import java.io.IOException;
import java.util.List;

Expand All @@ -44,23 +43,28 @@ public ManifestListGenerator(List<Image> images) {
*/
public <T extends BuildableManifestTemplate> ManifestTemplate getManifestListTemplate(
Class<T> manifestTemplateClass) throws IOException {
Preconditions.checkArgument(
manifestTemplateClass == V22ManifestTemplate.class,
"Build an OCI image index is not yet supported");
Preconditions.checkState(!images.isEmpty(), "no images given");

V22ManifestListTemplate manifestList = new V22ManifestListTemplate();
for (Image image : images) {
ImageToJsonTranslator imageTranslator = new ImageToJsonTranslator(image);
if (manifestTemplateClass == V22ManifestTemplate.class) {
return getV22ManifestListTemplate(manifestTemplateClass);

BlobDescriptor configDescriptor =
Digests.computeDigest(imageTranslator.getContainerConfiguration());
} else if (manifestTemplateClass == OciManifestTemplate.class) {
return getOciIndexTemplate(manifestTemplateClass);
}
throw new IllegalArgumentException(
"Unsupported manifestTemplateClass format " + manifestTemplateClass);
}

private <T extends BuildableManifestTemplate> V22ManifestListTemplate getV22ManifestListTemplate(
Class<T> manifestTemplateClass) throws IOException {
V22ManifestListTemplate manifestList = new V22ManifestListTemplate();
for (Image image : images) {
BuildableManifestTemplate manifestTemplate =
imageTranslator.getManifestTemplate(manifestTemplateClass, configDescriptor);
getBuildableManifestTemplate(manifestTemplateClass, image);
BlobDescriptor manifestDescriptor = Digests.computeDigest(manifestTemplate);

ManifestDescriptorTemplate manifest = new ManifestDescriptorTemplate();
V22ManifestListTemplate.ManifestDescriptorTemplate manifest =
new V22ManifestListTemplate.ManifestDescriptorTemplate();
manifest.setMediaType(manifestTemplate.getManifestMediaType());
manifest.setSize(manifestDescriptor.getSize());
manifest.setDigest(manifestDescriptor.getDigest().toString());
Expand All @@ -69,4 +73,32 @@ public <T extends BuildableManifestTemplate> ManifestTemplate getManifestListTem
}
return manifestList;
}

private <T extends BuildableManifestTemplate> OciIndexTemplate getOciIndexTemplate(
Class<T> manifestTemplateClass) throws IOException {
OciIndexTemplate manifestList = new OciIndexTemplate();
for (Image image : images) {
BuildableManifestTemplate manifestTemplate =
getBuildableManifestTemplate(manifestTemplateClass, image);
BlobDescriptor manifestDescriptor = Digests.computeDigest(manifestTemplate);

OciIndexTemplate.ManifestDescriptorTemplate manifest =
new OciIndexTemplate.ManifestDescriptorTemplate(
manifestTemplate.getManifestMediaType(),
manifestDescriptor.getSize(),
manifestDescriptor.getDigest());
manifest.setPlatform(image.getArchitecture(), image.getOs());
manifestList.addManifest(manifest);
}
return manifestList;
}

private <T extends BuildableManifestTemplate>
BuildableManifestTemplate getBuildableManifestTemplate(
Class<T> manifestTemplateClass, Image image) throws IOException {
ImageToJsonTranslator imageTranslator = new ImageToJsonTranslator(image);
BlobDescriptor configDescriptor =
Digests.computeDigest(imageTranslator.getContainerConfiguration());
return imageTranslator.getManifestTemplate(manifestTemplateClass, configDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.Layer;
import com.google.cloud.tools.jib.image.json.ManifestTemplate;
import com.google.cloud.tools.jib.image.json.OciIndexTemplate;
import com.google.cloud.tools.jib.image.json.OciManifestTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestListTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
import java.io.IOException;
Expand All @@ -36,7 +38,7 @@
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

/** Tests for {@link BuildManifestListOrSingleManifest}. */
/** Tests for {@link BuildManifestListOrSingleManifestStep}. */
@RunWith(MockitoJUnitRunner.class)
public class BuildManifestListOrSingleManifestStepTest {

Expand Down Expand Up @@ -149,6 +151,52 @@ public void testCall_manifestList() throws IOException {
manifestList.getDigestsForPlatform("arm64", "windows"));
}

@Test
public void testCall_manifestOciIndex() throws IOException {

// Expected Manifest Index JSON
// {
// "schemaVersion":2,
// "mediaType":"application/vnd.oci.image.index.v1+json",
// "manifests":[
// {
// "mediaType":"application/vnd.oci.image.manifest.v1+json",
// "digest":"sha256:9591d0e20a39c41abdf52d2f8f30c97d7aeccbc3835999152e73a85de434d781",
// "size":338,
// "platform":{
// "architecture":"amd64",
// "os":"linux"
// }
// },
// {
// "mediaType":"application/vnd.oci.image.manifest.v1+json",
// "digest":"sha256:8e0e6885ba5969d8fedf3f1b38ec68bb8fbf9f528c6e4c516328a81525ec479f",
// "size":338,
// "platform":{
// "architecture":"arm64",
// "os":"windows"
// }
// }
// ]
// }

Mockito.doReturn(OciManifestTemplate.class).when(buildContext).getTargetFormat();
ManifestTemplate manifestTemplate =
new BuildManifestListOrSingleManifestStep(
buildContext, progressDispatcherFactory, Arrays.asList(image1, image2))
.call();

Assert.assertTrue(manifestTemplate instanceof OciIndexTemplate);
OciIndexTemplate manifestList = (OciIndexTemplate) manifestTemplate;
Assert.assertEquals(2, manifestList.getSchemaVersion());
Assert.assertEquals(
Arrays.asList("sha256:9591d0e20a39c41abdf52d2f8f30c97d7aeccbc3835999152e73a85de434d781"),
manifestList.getDigestsForPlatform("amd64", "linux"));
Assert.assertEquals(
Arrays.asList("sha256:8e0e6885ba5969d8fedf3f1b38ec68bb8fbf9f528c6e4c516328a81525ec479f"),
manifestList.getDigestsForPlatform("arm64", "windows"));
}

@Test
public void testCall_emptyImagesList() throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ public class ManifestListGeneratorTest {
private ManifestListGenerator manifestListGenerator;

@Before
public void setUp() {
image1 =
Image.builder(V22ManifestTemplate.class).setArchitecture("amd64").setOs("linux").build();
image2 =
Image.builder(V22ManifestTemplate.class).setArchitecture("arm64").setOs("windows").build();
manifestListGenerator = new ManifestListGenerator(Arrays.asList(image1, image2));
}
public void setUp() {}

@Test
public void testGetManifestListTemplate() throws IOException {
Expand Down Expand Up @@ -68,6 +62,11 @@ public void testGetManifestListTemplate() throws IOException {
// }
// ]
// }
image1 =
Image.builder(V22ManifestTemplate.class).setArchitecture("amd64").setOs("linux").build();
image2 =
Image.builder(V22ManifestTemplate.class).setArchitecture("arm64").setOs("windows").build();
manifestListGenerator = new ManifestListGenerator(Arrays.asList(image1, image2));

ManifestTemplate manifestTemplate =
manifestListGenerator.getManifestListTemplate(V22ManifestTemplate.class);
Expand All @@ -82,6 +81,53 @@ public void testGetManifestListTemplate() throws IOException {
manifestList.getDigestsForPlatform("arm64", "windows"));
}

@Test
public void testGetManifestListTemplate_ociIndex() throws IOException {

// Expected Manifest List JSON
// {
// "schemaVersion":2,
// "mediaType":"application/vnd.oci.image.index.v1+json",
// "manifests":[
// {
// "mediaType":"application/vnd.oci.image.manifest.v1+json",
// "digest":"sha256:835e93ca9c952a5f811fecadbc6337c50415cce1ce4d7a4f9b6347ce4605c1fa",
// "size":248,
// "platform":{
// "architecture":"amd64",
// "os":"linux"
// }
// },
// {
// "mediaType":"application/vnd.oci.image.manifest.v1+json",
// "digest":"sha256:7ad84c70b22af31a7b0cc2218121d7e0a93f822374ccf0a634447921295c795d",
// "size":248,
// "platform":{
// "architecture":"arm64",
// "os":"windows"
// }
// }
// ]
// }
image1 =
Image.builder(OciManifestTemplate.class).setArchitecture("amd64").setOs("linux").build();
image2 =
Image.builder(OciManifestTemplate.class).setArchitecture("arm64").setOs("windows").build();
manifestListGenerator = new ManifestListGenerator(Arrays.asList(image1, image2));

ManifestTemplate manifestTemplate =
manifestListGenerator.getManifestListTemplate(OciManifestTemplate.class);
Assert.assertTrue(manifestTemplate instanceof OciIndexTemplate);
OciIndexTemplate manifestList = (OciIndexTemplate) manifestTemplate;
Assert.assertEquals(2, manifestList.getSchemaVersion());
Assert.assertEquals(
Arrays.asList("sha256:835e93ca9c952a5f811fecadbc6337c50415cce1ce4d7a4f9b6347ce4605c1fa"),
manifestList.getDigestsForPlatform("amd64", "linux"));
Assert.assertEquals(
Arrays.asList("sha256:7ad84c70b22af31a7b0cc2218121d7e0a93f822374ccf0a634447921295c795d"),
manifestList.getDigestsForPlatform("arm64", "windows"));
}

@Test
public void testGetManifestListTemplate_emptyImagesList() throws IOException {
try {
Expand All @@ -95,12 +141,14 @@ public void testGetManifestListTemplate_emptyImagesList() throws IOException {

@Test
public void testGetManifestListTemplate_unsupportedImageFormat() throws IOException {
Class<? extends BuildableManifestTemplate> unknownFormat = BuildableManifestTemplate.class;
try {
new ManifestListGenerator(Arrays.asList(image1, image2))
.getManifestListTemplate(OciManifestTemplate.class);
.getManifestListTemplate(unknownFormat);
Assert.fail();
} catch (IllegalArgumentException ex) {
Assert.assertEquals("Build an OCI image index is not yet supported", ex.getMessage());
Assert.assertEquals(
"Unsupported manifestTemplateClass format " + unknownFormat, ex.getMessage());
}
}
}