Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/faq.md
Original file line number Diff line number Diff line change
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 @@ -354,6 +356,33 @@ public void testDistroless_ociManifest()
Assert.assertEquals("linux", 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("amd64", "windows")))
.containerize(
Containerizer.to(
RegistryImage.named(dockerHost + ":5000/jib-scratch:multi-platform-oci"))
.setAllowInsecureRegistries(true));

OciIndexTemplate manifestList =
(OciIndexTemplate) registryClient.pullManifest("multi-platform-oci").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("amd64", platform2.getArchitecture());
Assert.assertEquals("windows", platform2.getOs());
Comment on lines +375 to +383
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current assertions rely on the order of manifests in the manifest list, which could make the test fragile if the underlying implementation changes how platforms are ordered. To make the test more robust, I suggest verifying the platforms in an order-independent way. For example, you could collect the architectures into a Set and assert its contents, and then verify the OS for all manifests.

Suggested change
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("amd64", platform2.getArchitecture());
Assert.assertEquals("windows", platform2.getOs());
java.util.Set<String> architectures =
manifestList.getManifests().stream()
.map(manifest -> manifest.getPlatform().getArchitecture())
.collect(java.util.stream.Collectors.toSet());
Assert.assertEquals(ImmutableSet.of("arm64", "amd64"), architectures);
for (OciIndexTemplate.ManifestDescriptorTemplate manifest : manifestList.getManifests()) {
Assert.assertEquals("windows", manifest.getPlatform().getOs());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer the current version, so that if the underlying implementation changes in Jib we see the impact somewhere.

}

@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,27 @@ 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();

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

private V22ManifestListTemplate getV22ManifestListTemplate() throws IOException {
V22ManifestListTemplate manifestList = new V22ManifestListTemplate();
for (Image image : images) {
BuildableManifestTemplate manifestTemplate =
imageTranslator.getManifestTemplate(manifestTemplateClass, configDescriptor);
getBuildableManifestTemplate(V22ManifestTemplate.class, 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 +72,31 @@ public <T extends BuildableManifestTemplate> ManifestTemplate getManifestListTem
}
return manifestList;
}

private OciIndexTemplate getOciIndexTemplate() throws IOException {
OciIndexTemplate manifestList = new OciIndexTemplate();
for (Image image : images) {
BuildableManifestTemplate manifestTemplate =
getBuildableManifestTemplate(OciManifestTemplate.class, 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 @@ -27,18 +27,10 @@
/** Tests for {@link ManifestListGenerator}. */
public class ManifestListGeneratorTest {

private Image image1;
private Image image2;
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 +60,11 @@ public void testGetManifestListTemplate() throws IOException {
// }
// ]
// }
Image image1 =
Image.builder(V22ManifestTemplate.class).setArchitecture("amd64").setOs("linux").build();
Image 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,8 +79,56 @@ 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"
// }
// }
// ]
// }
Image image1 =
Image.builder(OciManifestTemplate.class).setArchitecture("amd64").setOs("linux").build();
Image 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 {
new ManifestListGenerator(Collections.emptyList())
.getManifestListTemplate(V22ManifestTemplate.class);
Expand All @@ -95,12 +140,18 @@ public void testGetManifestListTemplate_emptyImagesList() throws IOException {

@Test
public void testGetManifestListTemplate_unsupportedImageFormat() throws IOException {
Image image1 =
Image.builder(V22ManifestTemplate.class).setArchitecture("amd64").setOs("linux").build();
Image image2 =
Image.builder(V22ManifestTemplate.class).setArchitecture("arm64").setOs("windows").build();
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());
}
}
}