From d57bf6fc05a9b7d33b4f50ef43c1fc254769c890 Mon Sep 17 00:00:00 2001 From: Robert Macaulay Date: Tue, 30 Nov 2021 15:41:40 -0600 Subject: [PATCH 1/8] Prototype of an option to tag child images when publishing multi platfor --- .../jib/builder/steps/PushImageStep.java | 14 +++- .../configuration/ContainerConfiguration.java | 27 +++++++- .../jib/builder/steps/PushImageStepTest.java | 69 ++++++++++++++++++- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java index ef3600c457..9d58631a8f 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.Set; import java.util.concurrent.Callable; +import java.util.stream.Collectors; /** * Pushes a manifest or a manifest list for a tag. If not a manifest list, returns the manifest @@ -78,8 +79,8 @@ static ImmutableList makeList( DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate); - Set imageQualifiers = - singlePlatform ? tags : Collections.singleton(manifestDigest.toString()); + Set imageQualifiers = singlePlatform ? tags : getChildTags(builtImage, tags, manifestDigest, buildContext.getContainerConfiguration().isPlatformTag()); + return imageQualifiers.stream() .map( qualifier -> @@ -95,6 +96,15 @@ static ImmutableList makeList( } } + private static Set getChildTags(Image builtImage, Set tags, DescriptorDigest manifestDigest, boolean newTagFeatureEnabled) { + if(newTagFeatureEnabled){ + String architecture = builtImage.getArchitecture(); + return tags.stream().map(tag -> tag + "-" + architecture).collect(Collectors.toSet()); + }else{ + return Collections.singleton(manifestDigest.toString()); + } + } + static ImmutableList makeListForManifestList( BuildContext buildContext, ProgressEventDispatcher.Factory progressEventDispatcherFactory, diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java index 6656917386..0bce85420f 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java @@ -58,6 +58,7 @@ public static class Builder { @Nullable private Set volumes; @Nullable private Map labels; @Nullable private String user; + @Nullable private boolean platformTag; @Nullable private AbsoluteUnixPath workingDirectory; /** @@ -293,6 +294,20 @@ public Builder setWorkingDirectory(@Nullable AbsoluteUnixPath workingDirectory) return this; } + + /** + * Sets the working directory in the container. + * + * @param platformTag tag platforms + * @return this + */ + public Builder setPlatformTag(boolean platformTag) { + this.platformTag = platformTag; + return this; + } + + + /** * Builds the {@link ContainerConfiguration}. * @@ -309,7 +324,8 @@ public ContainerConfiguration build() { volumes == null ? null : ImmutableSet.copyOf(volumes), labels == null ? null : ImmutableMap.copyOf(labels), user, - workingDirectory); + workingDirectory, + platformTag); } private Builder() {} @@ -334,6 +350,7 @@ public static Builder builder() { @Nullable private final ImmutableMap labels; @Nullable private final String user; @Nullable private final AbsoluteUnixPath workingDirectory; + @Nullable private final boolean platformTag; private ContainerConfiguration( ImmutableSet platforms, @@ -345,7 +362,8 @@ private ContainerConfiguration( @Nullable ImmutableSet volumes, @Nullable ImmutableMap labels, @Nullable String user, - @Nullable AbsoluteUnixPath workingDirectory) { + @Nullable AbsoluteUnixPath workingDirectory, + boolean platformTag) { this.platforms = platforms; this.creationTime = creationTime; this.entrypoint = entrypoint; @@ -356,6 +374,7 @@ private ContainerConfiguration( this.labels = labels; this.user = user; this.workingDirectory = workingDirectory; + this.platformTag = platformTag; } public ImmutableSet getPlatforms() { @@ -406,6 +425,10 @@ public AbsoluteUnixPath getWorkingDirectory() { return workingDirectory; } + public boolean isPlatformTag() { + return platformTag; + } + @Override @VisibleForTesting public boolean equals(Object other) { diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java index 7104aec130..fed78d7792 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java @@ -16,29 +16,36 @@ package com.google.cloud.tools.jib.builder.steps; +import com.google.cloud.tools.jib.api.DescriptorDigest; import com.google.cloud.tools.jib.api.RegistryException; import com.google.cloud.tools.jib.api.buildplan.Platform; +import com.google.cloud.tools.jib.blob.BlobDescriptor; import com.google.cloud.tools.jib.builder.ProgressEventDispatcher; import com.google.cloud.tools.jib.configuration.BuildContext; import com.google.cloud.tools.jib.configuration.ContainerConfiguration; import com.google.cloud.tools.jib.event.EventHandlers; import com.google.cloud.tools.jib.global.JibSystemProperties; +import com.google.cloud.tools.jib.image.Image; 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; import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import java.io.IOException; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.RestoreSystemProperties; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import java.io.IOException; +import java.util.Set; + /** Tests for {@link PushImageStep}. */ @RunWith(MockitoJUnitRunner.class) public class PushImageStepTest { @@ -50,6 +57,8 @@ public class PushImageStepTest { @Mock private BuildContext buildContext; @Mock private RegistryClient registryClient; @Mock private ContainerConfiguration containerConfig; + @Mock private DescriptorDigest mockDescriptorDigest; + private final V22ManifestListTemplate manifestList = new V22ManifestListTemplate(); @@ -58,6 +67,7 @@ public void setUp() { Mockito.when(buildContext.getAllTargetImageTags()).thenReturn(ImmutableSet.of("tag1", "tag2")); Mockito.when(buildContext.getEventHandlers()).thenReturn(EventHandlers.NONE); Mockito.when(buildContext.getContainerConfiguration()).thenReturn(containerConfig); + Mockito.doReturn(V22ManifestTemplate.class).when(buildContext).getTargetFormat(); Mockito.when(containerConfig.getPlatforms()) .thenReturn( ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arm64", "windows"))); @@ -88,6 +98,63 @@ public void testMakeListForManifestList() throws IOException, RegistryException buildResult.getImageId().toString()); } } + @Test + public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException { + Image asd = Image.builder(V22ManifestTemplate.class) + .setArchitecture("wasm").build(); + + Mockito.when(containerConfig.isPlatformTag()).thenReturn(true); + + ImmutableList pushImageStepList = + PushImageStep.makeList( + buildContext, progressDispatcherFactory, registryClient, new BlobDescriptor(mockDescriptorDigest), asd, false); + + ArgumentCaptor tagCAtcher = ArgumentCaptor.forClass(String.class); + Mockito.when(registryClient.pushManifest(Mockito.any(),tagCAtcher.capture())).thenReturn(null); + + Assert.assertEquals(2, pushImageStepList.size()); + for (PushImageStep pushImageStep : pushImageStepList) { + BuildResult buildResult = pushImageStep.call(); + Assert.assertEquals( + "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1", + buildResult.getImageDigest().toString()); + Assert.assertEquals( + "mockDescriptorDigest", + buildResult.getImageId().toString()); + } + Set allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues()); + Set expectedTags = ImmutableSet.of("tag1-wasm", "tag2-wasm"); + Assert.assertEquals(expectedTags, allValues); + + } + @Test + public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException { + Image asd = Image.builder(V22ManifestTemplate.class) + .setArchitecture("wasm").build(); + Mockito.when(containerConfig.isPlatformTag()).thenReturn(false); + + ImmutableList pushImageStepList = + PushImageStep.makeList( + buildContext, progressDispatcherFactory, registryClient, new BlobDescriptor(mockDescriptorDigest), asd, false); + + ArgumentCaptor tagCAtcher = ArgumentCaptor.forClass(String.class); + Mockito.when(registryClient.pushManifest(Mockito.any(),tagCAtcher.capture())).thenReturn(null); + + Assert.assertEquals(1, pushImageStepList.size()); + for (PushImageStep pushImageStep : pushImageStepList) { + BuildResult buildResult = pushImageStep.call(); + Assert.assertEquals( + "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1", + buildResult.getImageDigest().toString()); + Assert.assertEquals( + "mockDescriptorDigest", + buildResult.getImageId().toString()); + } + Set allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues()); + Set expectedTags = ImmutableSet.of("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1"); + Assert.assertEquals(expectedTags, allValues); + + } @Test public void testMakeListForManifestList_singlePlatform() throws IOException { From de17f788bbf7ce3872e908d474951befb262141f Mon Sep 17 00:00:00 2001 From: Robert Macaulay Date: Wed, 8 Dec 2021 08:59:11 -0600 Subject: [PATCH 2/8] formatter --- .../google/cloud/tools/jib/builder/steps/PushImageStep.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java index 9d58631a8f..27d7b04649 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java @@ -97,10 +97,10 @@ static ImmutableList makeList( } private static Set getChildTags(Image builtImage, Set tags, DescriptorDigest manifestDigest, boolean newTagFeatureEnabled) { - if(newTagFeatureEnabled){ + if (newTagFeatureEnabled) { String architecture = builtImage.getArchitecture(); return tags.stream().map(tag -> tag + "-" + architecture).collect(Collectors.toSet()); - }else{ + } else { return Collections.singleton(manifestDigest.toString()); } } From 8dca82004e0814e216e2bee0e52894e6b7531f06 Mon Sep 17 00:00:00 2001 From: Robert Macaulay Date: Wed, 8 Dec 2021 09:33:40 -0600 Subject: [PATCH 3/8] Format tests too --- .../cloud/tools/jib/builder/steps/PushImageStepTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java index fed78d7792..4aa341481c 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java @@ -32,6 +32,8 @@ import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.util.Set; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -43,9 +45,6 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import java.io.IOException; -import java.util.Set; - /** Tests for {@link PushImageStep}. */ @RunWith(MockitoJUnitRunner.class) public class PushImageStepTest { @@ -98,6 +97,7 @@ public void testMakeListForManifestList() throws IOException, RegistryException buildResult.getImageId().toString()); } } + @Test public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException { Image asd = Image.builder(V22ManifestTemplate.class) @@ -127,6 +127,7 @@ public void testMakeList_multiPlatform_enabled() throws IOException, RegistryExc Assert.assertEquals(expectedTags, allValues); } + @Test public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException { Image asd = Image.builder(V22ManifestTemplate.class) From 68f7fad2fdd6ce12b25306ee00b7ffed1c3a6e52 Mon Sep 17 00:00:00 2001 From: Robert Macaulay Date: Wed, 8 Dec 2021 09:42:12 -0600 Subject: [PATCH 4/8] ./gradlew goJF --- .../jib/builder/steps/PushImageStep.java | 15 +++++++- .../configuration/ContainerConfiguration.java | 3 -- .../jib/builder/steps/PushImageStepTest.java | 38 ++++++++++--------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java index 27d7b04649..552fedc062 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java @@ -79,7 +79,14 @@ static ImmutableList makeList( DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate); - Set imageQualifiers = singlePlatform ? tags : getChildTags(builtImage, tags, manifestDigest, buildContext.getContainerConfiguration().isPlatformTag()); + Set imageQualifiers = + singlePlatform + ? tags + : getChildTags( + builtImage, + tags, + manifestDigest, + buildContext.getContainerConfiguration().isPlatformTag()); return imageQualifiers.stream() .map( @@ -96,7 +103,11 @@ static ImmutableList makeList( } } - private static Set getChildTags(Image builtImage, Set tags, DescriptorDigest manifestDigest, boolean newTagFeatureEnabled) { + private static Set getChildTags( + Image builtImage, + Set tags, + DescriptorDigest manifestDigest, + boolean newTagFeatureEnabled) { if (newTagFeatureEnabled) { String architecture = builtImage.getArchitecture(); return tags.stream().map(tag -> tag + "-" + architecture).collect(Collectors.toSet()); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java index 0bce85420f..9cd8a37039 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java @@ -294,7 +294,6 @@ public Builder setWorkingDirectory(@Nullable AbsoluteUnixPath workingDirectory) return this; } - /** * Sets the working directory in the container. * @@ -306,8 +305,6 @@ public Builder setPlatformTag(boolean platformTag) { return this; } - - /** * Builds the {@link ContainerConfiguration}. * diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java index 4aa341481c..dde356d192 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java @@ -58,7 +58,6 @@ public class PushImageStepTest { @Mock private ContainerConfiguration containerConfig; @Mock private DescriptorDigest mockDescriptorDigest; - private final V22ManifestListTemplate manifestList = new V22ManifestListTemplate(); @Before @@ -100,17 +99,21 @@ public void testMakeListForManifestList() throws IOException, RegistryException @Test public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException { - Image asd = Image.builder(V22ManifestTemplate.class) - .setArchitecture("wasm").build(); + Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); Mockito.when(containerConfig.isPlatformTag()).thenReturn(true); ImmutableList pushImageStepList = PushImageStep.makeList( - buildContext, progressDispatcherFactory, registryClient, new BlobDescriptor(mockDescriptorDigest), asd, false); + buildContext, + progressDispatcherFactory, + registryClient, + new BlobDescriptor(mockDescriptorDigest), + asd, + false); ArgumentCaptor tagCAtcher = ArgumentCaptor.forClass(String.class); - Mockito.when(registryClient.pushManifest(Mockito.any(),tagCAtcher.capture())).thenReturn(null); + Mockito.when(registryClient.pushManifest(Mockito.any(), tagCAtcher.capture())).thenReturn(null); Assert.assertEquals(2, pushImageStepList.size()); for (PushImageStep pushImageStep : pushImageStepList) { @@ -118,28 +121,29 @@ public void testMakeList_multiPlatform_enabled() throws IOException, RegistryExc Assert.assertEquals( "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1", buildResult.getImageDigest().toString()); - Assert.assertEquals( - "mockDescriptorDigest", - buildResult.getImageId().toString()); + Assert.assertEquals("mockDescriptorDigest", buildResult.getImageId().toString()); } Set allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues()); Set expectedTags = ImmutableSet.of("tag1-wasm", "tag2-wasm"); Assert.assertEquals(expectedTags, allValues); - } @Test public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException { - Image asd = Image.builder(V22ManifestTemplate.class) - .setArchitecture("wasm").build(); + Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); Mockito.when(containerConfig.isPlatformTag()).thenReturn(false); ImmutableList pushImageStepList = PushImageStep.makeList( - buildContext, progressDispatcherFactory, registryClient, new BlobDescriptor(mockDescriptorDigest), asd, false); + buildContext, + progressDispatcherFactory, + registryClient, + new BlobDescriptor(mockDescriptorDigest), + asd, + false); ArgumentCaptor tagCAtcher = ArgumentCaptor.forClass(String.class); - Mockito.when(registryClient.pushManifest(Mockito.any(),tagCAtcher.capture())).thenReturn(null); + Mockito.when(registryClient.pushManifest(Mockito.any(), tagCAtcher.capture())).thenReturn(null); Assert.assertEquals(1, pushImageStepList.size()); for (PushImageStep pushImageStep : pushImageStepList) { @@ -147,14 +151,12 @@ public void testMakeList_multiPlatform_disabled() throws IOException, RegistryEx Assert.assertEquals( "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1", buildResult.getImageDigest().toString()); - Assert.assertEquals( - "mockDescriptorDigest", - buildResult.getImageId().toString()); + Assert.assertEquals("mockDescriptorDigest", buildResult.getImageId().toString()); } Set allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues()); - Set expectedTags = ImmutableSet.of("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1"); + Set expectedTags = + ImmutableSet.of("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1"); Assert.assertEquals(expectedTags, allValues); - } @Test From d97db621808af5fcee95b69ef7e7678c8859de40 Mon Sep 17 00:00:00 2001 From: Robert Macaulay Date: Fri, 10 Dec 2021 09:40:49 -0600 Subject: [PATCH 5/8] PR updates --- .../jib/builder/steps/PushImageStep.java | 9 +--- .../tools/jib/configuration/BuildContext.java | 22 +++++++++- .../configuration/ContainerConfiguration.java | 12 +----- .../jib/builder/steps/PushImageStepTest.java | 43 +++++++------------ 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java index 552fedc062..a4e77c966b 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java @@ -82,11 +82,7 @@ static ImmutableList makeList( Set imageQualifiers = singlePlatform ? tags - : getChildTags( - builtImage, - tags, - manifestDigest, - buildContext.getContainerConfiguration().isPlatformTag()); + : getChildTags(builtImage, tags, manifestDigest, buildContext.isEnablePlatformTags()); return imageQualifiers.stream() .map( @@ -111,9 +107,8 @@ private static Set getChildTags( if (newTagFeatureEnabled) { String architecture = builtImage.getArchitecture(); return tags.stream().map(tag -> tag + "-" + architecture).collect(Collectors.toSet()); - } else { - return Collections.singleton(manifestDigest.toString()); } + return Collections.singleton(manifestDigest.toString()); } static ImmutableList makeListForManifestList( diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java index caa5780008..4e998b0afd 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java @@ -83,6 +83,7 @@ public static class Builder { @Nullable private ExecutorService executorService; private boolean alwaysCacheBaseImage = false; private ImmutableListMultimap registryMirrors = ImmutableListMultimap.of(); + private boolean enablePlatformTags = false; private Builder() {} @@ -97,6 +98,15 @@ public Builder setBaseImageConfiguration(ImageConfiguration imageConfiguration) return this; } + /** + * Enables/Disables the platform tags. + * + * @return this + */ + public void setEnablePlatformTags(boolean enablePlatformTags) { + this.enablePlatformTags = enablePlatformTags; + } + /** * Sets the target image configuration. * @@ -328,7 +338,8 @@ public BuildContext build() throws CacheDirectoryCreationException { executorService == null ? Executors.newCachedThreadPool() : executorService, executorService == null, // shutDownExecutorService alwaysCacheBaseImage, - registryMirrors); + registryMirrors, + enablePlatformTags); case 1: throw new IllegalStateException(missingFields.get(0) + " is required but not set"); @@ -386,6 +397,7 @@ public static Builder builder() { private final boolean shutDownExecutorService; private final boolean alwaysCacheBaseImage; private final ImmutableListMultimap registryMirrors; + private final boolean enablePlatformTags; /** Instantiate with {@link #builder}. */ private BuildContext( @@ -405,7 +417,8 @@ private BuildContext( ExecutorService executorService, boolean shutDownExecutorService, boolean alwaysCacheBaseImage, - ImmutableListMultimap registryMirrors) { + ImmutableListMultimap registryMirrors, + boolean enablePlatformTags) { this.baseImageConfiguration = baseImageConfiguration; this.targetImageConfiguration = targetImageConfiguration; this.additionalTargetImageTags = additionalTargetImageTags; @@ -423,12 +436,17 @@ private BuildContext( this.shutDownExecutorService = shutDownExecutorService; this.alwaysCacheBaseImage = alwaysCacheBaseImage; this.registryMirrors = registryMirrors; + this.enablePlatformTags = enablePlatformTags; } public ImageConfiguration getBaseImageConfiguration() { return baseImageConfiguration; } + public boolean isEnablePlatformTags() { + return enablePlatformTags; + } + public ImageConfiguration getTargetImageConfiguration() { return targetImageConfiguration; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java index 9cd8a37039..65a867760c 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java @@ -321,8 +321,7 @@ public ContainerConfiguration build() { volumes == null ? null : ImmutableSet.copyOf(volumes), labels == null ? null : ImmutableMap.copyOf(labels), user, - workingDirectory, - platformTag); + workingDirectory); } private Builder() {} @@ -347,7 +346,6 @@ public static Builder builder() { @Nullable private final ImmutableMap labels; @Nullable private final String user; @Nullable private final AbsoluteUnixPath workingDirectory; - @Nullable private final boolean platformTag; private ContainerConfiguration( ImmutableSet platforms, @@ -359,8 +357,7 @@ private ContainerConfiguration( @Nullable ImmutableSet volumes, @Nullable ImmutableMap labels, @Nullable String user, - @Nullable AbsoluteUnixPath workingDirectory, - boolean platformTag) { + @Nullable AbsoluteUnixPath workingDirectory) { this.platforms = platforms; this.creationTime = creationTime; this.entrypoint = entrypoint; @@ -371,7 +368,6 @@ private ContainerConfiguration( this.labels = labels; this.user = user; this.workingDirectory = workingDirectory; - this.platformTag = platformTag; } public ImmutableSet getPlatforms() { @@ -422,10 +418,6 @@ public AbsoluteUnixPath getWorkingDirectory() { return workingDirectory; } - public boolean isPlatformTag() { - return platformTag; - } - @Override @VisibleForTesting public boolean equals(Object other) { diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java index dde356d192..7e4b9d6934 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java @@ -16,6 +16,8 @@ package com.google.cloud.tools.jib.builder.steps; +import static com.google.common.truth.Truth.assertThat; + import com.google.cloud.tools.jib.api.DescriptorDigest; import com.google.cloud.tools.jib.api.RegistryException; import com.google.cloud.tools.jib.api.buildplan.Platform; @@ -33,7 +35,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.io.IOException; -import java.util.Set; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -101,7 +102,7 @@ public void testMakeListForManifestList() throws IOException, RegistryException public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException { Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); - Mockito.when(containerConfig.isPlatformTag()).thenReturn(true); + Mockito.when(buildContext.isEnablePlatformTags()).thenReturn(true); ImmutableList pushImageStepList = PushImageStep.makeList( @@ -112,26 +113,20 @@ public void testMakeList_multiPlatform_enabled() throws IOException, RegistryExc asd, false); - ArgumentCaptor tagCAtcher = ArgumentCaptor.forClass(String.class); - Mockito.when(registryClient.pushManifest(Mockito.any(), tagCAtcher.capture())).thenReturn(null); + ArgumentCaptor tagCatcher = ArgumentCaptor.forClass(String.class); + Mockito.when(registryClient.pushManifest(Mockito.any(), tagCatcher.capture())).thenReturn(null); Assert.assertEquals(2, pushImageStepList.size()); - for (PushImageStep pushImageStep : pushImageStepList) { - BuildResult buildResult = pushImageStep.call(); - Assert.assertEquals( - "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1", - buildResult.getImageDigest().toString()); - Assert.assertEquals("mockDescriptorDigest", buildResult.getImageId().toString()); - } - Set allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues()); - Set expectedTags = ImmutableSet.of("tag1-wasm", "tag2-wasm"); - Assert.assertEquals(expectedTags, allValues); + pushImageStepList.get(0).call(); + pushImageStepList.get(1).call(); + + assertThat(tagCatcher.getAllValues()).containsExactly("tag1-wasm", "tag2-wasm"); } @Test public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException { Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); - Mockito.when(containerConfig.isPlatformTag()).thenReturn(false); + Mockito.when(buildContext.isEnablePlatformTags()).thenReturn(false); ImmutableList pushImageStepList = PushImageStep.makeList( @@ -142,21 +137,13 @@ public void testMakeList_multiPlatform_disabled() throws IOException, RegistryEx asd, false); - ArgumentCaptor tagCAtcher = ArgumentCaptor.forClass(String.class); - Mockito.when(registryClient.pushManifest(Mockito.any(), tagCAtcher.capture())).thenReturn(null); + ArgumentCaptor tagCatcher = ArgumentCaptor.forClass(String.class); + Mockito.when(registryClient.pushManifest(Mockito.any(), tagCatcher.capture())).thenReturn(null); Assert.assertEquals(1, pushImageStepList.size()); - for (PushImageStep pushImageStep : pushImageStepList) { - BuildResult buildResult = pushImageStep.call(); - Assert.assertEquals( - "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1", - buildResult.getImageDigest().toString()); - Assert.assertEquals("mockDescriptorDigest", buildResult.getImageId().toString()); - } - Set allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues()); - Set expectedTags = - ImmutableSet.of("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1"); - Assert.assertEquals(expectedTags, allValues); + pushImageStepList.get(0).call(); + assertThat(tagCatcher.getAllValues()) + .containsExactly("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1"); } @Test From 4b90877c3a5204f1c1dcd66864077dd7a2106b71 Mon Sep 17 00:00:00 2001 From: Robert Macaulay Date: Mon, 13 Dec 2021 08:54:40 -0600 Subject: [PATCH 6/8] PR update --- .../jib/builder/steps/PushImageStep.java | 34 +++++++-------- .../tools/jib/configuration/BuildContext.java | 9 ++-- .../configuration/ContainerConfiguration.java | 12 ------ .../jib/builder/steps/PushImageStepTest.java | 43 ++++++++++--------- .../jib/configuration/BuildContextTest.java | 3 ++ 5 files changed, 49 insertions(+), 52 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java index a4e77c966b..bcc9d1fa69 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java @@ -58,32 +58,32 @@ static ImmutableList makeList( throws IOException { boolean singlePlatform = buildContext.getContainerConfiguration().getPlatforms().size() == 1; Set tags = buildContext.getAllTargetImageTags(); - int numPushers = singlePlatform ? tags.size() : 1; + + // Gets the image manifest to push. + BuildableManifestTemplate manifestTemplate = + new ImageToJsonTranslator(builtImage) + .getManifestTemplate( + buildContext.getTargetFormat(), containerConfigurationDigestAndSize); + + DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate); + + Set imageQualifiers = + singlePlatform + ? tags + : getChildTags(builtImage, tags, manifestDigest, buildContext.getEnablePlatformTags()); EventHandlers eventHandlers = buildContext.getEventHandlers(); try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Preparing manifest pushers"); ProgressEventDispatcher progressDispatcher = - progressEventDispatcherFactory.create("launching manifest pushers", numPushers)) { + progressEventDispatcherFactory.create( + "launching manifest pushers", imageQualifiers.size())) { if (JibSystemProperties.skipExistingImages() && manifestAlreadyExists) { eventHandlers.dispatch(LogEvent.info("Skipping pushing manifest; already exists.")); return ImmutableList.of(); } - // Gets the image manifest to push. - BuildableManifestTemplate manifestTemplate = - new ImageToJsonTranslator(builtImage) - .getManifestTemplate( - buildContext.getTargetFormat(), containerConfigurationDigestAndSize); - - DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate); - - Set imageQualifiers = - singlePlatform - ? tags - : getChildTags(builtImage, tags, manifestDigest, buildContext.isEnablePlatformTags()); - return imageQualifiers.stream() .map( qualifier -> @@ -103,8 +103,8 @@ private static Set getChildTags( Image builtImage, Set tags, DescriptorDigest manifestDigest, - boolean newTagFeatureEnabled) { - if (newTagFeatureEnabled) { + boolean enablePlatformTags) { + if (enablePlatformTags) { String architecture = builtImage.getArchitecture(); return tags.stream().map(tag -> tag + "-" + architecture).collect(Collectors.toSet()); } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java index 4e998b0afd..41af1536ed 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java @@ -99,12 +99,15 @@ public Builder setBaseImageConfiguration(ImageConfiguration imageConfiguration) } /** - * Enables/Disables the platform tags. + * Sets whether to automatically add architecture suffix to tags for platform-specific images + * when building multi-platform images. For example, when building amd64 and arm64 images for a + * given tag, the final tags will be <tag>-amd64 and <tag>-arm64. * * @return this */ - public void setEnablePlatformTags(boolean enablePlatformTags) { + public Builder setEnablePlatformTags(boolean enablePlatformTags) { this.enablePlatformTags = enablePlatformTags; + return this; } /** @@ -443,7 +446,7 @@ public ImageConfiguration getBaseImageConfiguration() { return baseImageConfiguration; } - public boolean isEnablePlatformTags() { + public boolean getEnablePlatformTags() { return enablePlatformTags; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java index 65a867760c..6656917386 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/ContainerConfiguration.java @@ -58,7 +58,6 @@ public static class Builder { @Nullable private Set volumes; @Nullable private Map labels; @Nullable private String user; - @Nullable private boolean platformTag; @Nullable private AbsoluteUnixPath workingDirectory; /** @@ -294,17 +293,6 @@ public Builder setWorkingDirectory(@Nullable AbsoluteUnixPath workingDirectory) return this; } - /** - * Sets the working directory in the container. - * - * @param platformTag tag platforms - * @return this - */ - public Builder setPlatformTag(boolean platformTag) { - this.platformTag = platformTag; - return this; - } - /** * Builds the {@link ContainerConfiguration}. * diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java index 7e4b9d6934..b520797245 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java @@ -17,6 +17,11 @@ package com.google.cloud.tools.jib.builder.steps; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; import com.google.cloud.tools.jib.api.DescriptorDigest; import com.google.cloud.tools.jib.api.RegistryException; @@ -43,7 +48,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; /** Tests for {@link PushImageStep}. */ @@ -63,16 +67,15 @@ public class PushImageStepTest { @Before public void setUp() { - Mockito.when(buildContext.getAllTargetImageTags()).thenReturn(ImmutableSet.of("tag1", "tag2")); - Mockito.when(buildContext.getEventHandlers()).thenReturn(EventHandlers.NONE); - Mockito.when(buildContext.getContainerConfiguration()).thenReturn(containerConfig); - Mockito.doReturn(V22ManifestTemplate.class).when(buildContext).getTargetFormat(); - Mockito.when(containerConfig.getPlatforms()) + when(buildContext.getAllTargetImageTags()).thenReturn(ImmutableSet.of("tag1", "tag2")); + when(buildContext.getEventHandlers()).thenReturn(EventHandlers.NONE); + when(buildContext.getContainerConfiguration()).thenReturn(containerConfig); + doReturn(V22ManifestTemplate.class).when(buildContext).getTargetFormat(); + when(containerConfig.getPlatforms()) .thenReturn( ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arm64", "windows"))); - Mockito.when(progressDispatcherFactory.create(Mockito.anyString(), Mockito.anyLong())) - .thenReturn(progressDispatcher); - Mockito.when(progressDispatcher.newChildProducer()).thenReturn(progressDispatcherFactory); + when(progressDispatcherFactory.create(anyString(), anyLong())).thenReturn(progressDispatcher); + when(progressDispatcher.newChildProducer()).thenReturn(progressDispatcherFactory); ManifestDescriptorTemplate manifest = new ManifestDescriptorTemplate(); manifest.setSize(100); @@ -86,7 +89,7 @@ public void testMakeListForManifestList() throws IOException, RegistryException PushImageStep.makeListForManifestList( buildContext, progressDispatcherFactory, registryClient, manifestList, false); - Assert.assertEquals(2, pushImageStepList.size()); + assertThat(pushImageStepList).hasSize(2); for (PushImageStep pushImageStep : pushImageStepList) { BuildResult buildResult = pushImageStep.call(); Assert.assertEquals( @@ -100,9 +103,9 @@ public void testMakeListForManifestList() throws IOException, RegistryException @Test public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException { - Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); + Image image = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); - Mockito.when(buildContext.isEnablePlatformTags()).thenReturn(true); + when(buildContext.getEnablePlatformTags()).thenReturn(true); ImmutableList pushImageStepList = PushImageStep.makeList( @@ -110,13 +113,13 @@ public void testMakeList_multiPlatform_enabled() throws IOException, RegistryExc progressDispatcherFactory, registryClient, new BlobDescriptor(mockDescriptorDigest), - asd, + image, false); ArgumentCaptor tagCatcher = ArgumentCaptor.forClass(String.class); - Mockito.when(registryClient.pushManifest(Mockito.any(), tagCatcher.capture())).thenReturn(null); + when(registryClient.pushManifest(any(), tagCatcher.capture())).thenReturn(null); - Assert.assertEquals(2, pushImageStepList.size()); + assertThat(pushImageStepList).hasSize(2); pushImageStepList.get(0).call(); pushImageStepList.get(1).call(); @@ -125,8 +128,8 @@ public void testMakeList_multiPlatform_enabled() throws IOException, RegistryExc @Test public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException { - Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); - Mockito.when(buildContext.isEnablePlatformTags()).thenReturn(false); + Image image = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); + when(buildContext.getEnablePlatformTags()).thenReturn(false); ImmutableList pushImageStepList = PushImageStep.makeList( @@ -134,11 +137,11 @@ public void testMakeList_multiPlatform_disabled() throws IOException, RegistryEx progressDispatcherFactory, registryClient, new BlobDescriptor(mockDescriptorDigest), - asd, + image, false); ArgumentCaptor tagCatcher = ArgumentCaptor.forClass(String.class); - Mockito.when(registryClient.pushManifest(Mockito.any(), tagCatcher.capture())).thenReturn(null); + when(registryClient.pushManifest(any(), tagCatcher.capture())).thenReturn(null); Assert.assertEquals(1, pushImageStepList.size()); pushImageStepList.get(0).call(); @@ -148,7 +151,7 @@ public void testMakeList_multiPlatform_disabled() throws IOException, RegistryEx @Test public void testMakeListForManifestList_singlePlatform() throws IOException { - Mockito.when(containerConfig.getPlatforms()) + when(containerConfig.getPlatforms()) .thenReturn(ImmutableSet.of(new Platform("amd64", "linux"))); ImmutableList pushImageStepList = diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/configuration/BuildContextTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/configuration/BuildContextTest.java index 98a365b013..4c052da192 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/configuration/BuildContextTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/configuration/BuildContextTest.java @@ -128,6 +128,7 @@ public void testBuilder() throws Exception { .setApplicationLayersCacheDirectory(expectedApplicationLayersCacheDirectory) .setBaseImageLayersCacheDirectory(expectedBaseImageLayersCacheDirectory) .setTargetFormat(ImageFormat.OCI) + .setEnablePlatformTags(true) .setAllowInsecureRegistries(true) .setLayerConfigurations(expectedLayerConfigurations) .setToolName(expectedCreatedBy) @@ -177,6 +178,7 @@ public void testBuilder() throws Exception { Assert.assertEquals(expectedCreatedBy, buildContext.getToolName()); Assert.assertEquals(expectedRegistryMirrors, buildContext.getRegistryMirrors()); Assert.assertNotNull(buildContext.getExecutorService()); + Assert.assertTrue(buildContext.getEnablePlatformTags()); } @Test @@ -220,6 +222,7 @@ public void testBuilder_default() throws CacheDirectoryCreationException { Assert.assertEquals(Collections.emptyList(), buildContext.getLayerConfigurations()); Assert.assertEquals("jib", buildContext.getToolName()); Assert.assertEquals(0, buildContext.getRegistryMirrors().size()); + Assert.assertFalse(buildContext.getEnablePlatformTags()); } @Test From 5518497dc3c721ecedc15987f9e652365e7644ea Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Mon, 13 Dec 2021 10:37:49 -0500 Subject: [PATCH 7/8] Refactor code --- .../jib/builder/steps/PushImageStep.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java index bcc9d1fa69..3d93b351d3 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PushImageStep.java @@ -56,21 +56,13 @@ static ImmutableList makeList( Image builtImage, boolean manifestAlreadyExists) throws IOException { - boolean singlePlatform = buildContext.getContainerConfiguration().getPlatforms().size() == 1; - Set tags = buildContext.getAllTargetImageTags(); - // Gets the image manifest to push. BuildableManifestTemplate manifestTemplate = new ImageToJsonTranslator(builtImage) .getManifestTemplate( buildContext.getTargetFormat(), containerConfigurationDigestAndSize); - DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate); - - Set imageQualifiers = - singlePlatform - ? tags - : getChildTags(builtImage, tags, manifestDigest, buildContext.getEnablePlatformTags()); + Set imageQualifiers = getImageQualifiers(buildContext, builtImage, manifestDigest); EventHandlers eventHandlers = buildContext.getEventHandlers(); try (TimerEventDispatcher ignored = @@ -99,12 +91,14 @@ static ImmutableList makeList( } } - private static Set getChildTags( - Image builtImage, - Set tags, - DescriptorDigest manifestDigest, - boolean enablePlatformTags) { - if (enablePlatformTags) { + private static Set getImageQualifiers( + BuildContext buildContext, Image builtImage, DescriptorDigest manifestDigest) { + boolean singlePlatform = buildContext.getContainerConfiguration().getPlatforms().size() == 1; + Set tags = buildContext.getAllTargetImageTags(); + if (singlePlatform) { + return tags; + } + if (buildContext.getEnablePlatformTags()) { String architecture = builtImage.getArchitecture(); return tags.stream().map(tag -> tag + "-" + architecture).collect(Collectors.toSet()); } From f4d4947f544cb2d5b7cda209f345abfafc54c89a Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Mon, 13 Dec 2021 10:53:02 -0500 Subject: [PATCH 8/8] Refactor code --- .../tools/jib/configuration/BuildContext.java | 25 +++++++------- .../jib/builder/steps/PushImageStepTest.java | 33 +++++++++---------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java index 41af1536ed..8de76cb46c 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/configuration/BuildContext.java @@ -98,18 +98,6 @@ public Builder setBaseImageConfiguration(ImageConfiguration imageConfiguration) return this; } - /** - * Sets whether to automatically add architecture suffix to tags for platform-specific images - * when building multi-platform images. For example, when building amd64 and arm64 images for a - * given tag, the final tags will be <tag>-amd64 and <tag>-arm64. - * - * @return this - */ - public Builder setEnablePlatformTags(boolean enablePlatformTags) { - this.enablePlatformTags = enablePlatformTags; - return this; - } - /** * Sets the target image configuration. * @@ -133,6 +121,19 @@ public Builder setAdditionalTargetImageTags(Set tags) { return this; } + /** + * Sets whether to automatically add architecture suffix to tags for platform-specific images + * when building multi-platform images. For example, when building amd64 and arm64 images for a + * given tag, the final tags will be {@code -amd64} and {@code -arm64}. + * + * @param enablePlatformTags whether to append architecture suffix to tags + * @return this + */ + public Builder setEnablePlatformTags(boolean enablePlatformTags) { + this.enablePlatformTags = enablePlatformTags; + return this; + } + /** * Sets configuration parameters for the container. * diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java index b520797245..b06096938a 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java @@ -37,10 +37,9 @@ import com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate; import com.google.cloud.tools.jib.image.json.V22ManifestTemplate; import com.google.cloud.tools.jib.registry.RegistryClient; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.io.IOException; -import org.junit.Assert; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -85,29 +84,27 @@ public void setUp() { @Test public void testMakeListForManifestList() throws IOException, RegistryException { - ImmutableList pushImageStepList = + List pushImageStepList = PushImageStep.makeListForManifestList( buildContext, progressDispatcherFactory, registryClient, manifestList, false); assertThat(pushImageStepList).hasSize(2); for (PushImageStep pushImageStep : pushImageStepList) { BuildResult buildResult = pushImageStep.call(); - Assert.assertEquals( - "sha256:64303e82b8a80ef20475dc7f807b81f172cacce1a59191927f3a7ea5222f38ae", - buildResult.getImageDigest().toString()); - Assert.assertEquals( - "sha256:64303e82b8a80ef20475dc7f807b81f172cacce1a59191927f3a7ea5222f38ae", - buildResult.getImageId().toString()); + assertThat(buildResult.getImageDigest().toString()) + .isEqualTo("sha256:64303e82b8a80ef20475dc7f807b81f172cacce1a59191927f3a7ea5222f38ae"); + assertThat(buildResult.getImageId().toString()) + .isEqualTo("sha256:64303e82b8a80ef20475dc7f807b81f172cacce1a59191927f3a7ea5222f38ae"); } } @Test - public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException { + public void testMakeList_multiPlatform_platformTags() throws IOException, RegistryException { Image image = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); when(buildContext.getEnablePlatformTags()).thenReturn(true); - ImmutableList pushImageStepList = + List pushImageStepList = PushImageStep.makeList( buildContext, progressDispatcherFactory, @@ -127,11 +124,11 @@ public void testMakeList_multiPlatform_enabled() throws IOException, RegistryExc } @Test - public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException { + public void testMakeList_multiPlatform_nonPlatformTags() throws IOException, RegistryException { Image image = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build(); when(buildContext.getEnablePlatformTags()).thenReturn(false); - ImmutableList pushImageStepList = + List pushImageStepList = PushImageStep.makeList( buildContext, progressDispatcherFactory, @@ -143,7 +140,7 @@ public void testMakeList_multiPlatform_disabled() throws IOException, RegistryEx ArgumentCaptor tagCatcher = ArgumentCaptor.forClass(String.class); when(registryClient.pushManifest(any(), tagCatcher.capture())).thenReturn(null); - Assert.assertEquals(1, pushImageStepList.size()); + assertThat(pushImageStepList).hasSize(1); pushImageStepList.get(0).call(); assertThat(tagCatcher.getAllValues()) .containsExactly("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1"); @@ -154,19 +151,19 @@ public void testMakeListForManifestList_singlePlatform() throws IOException { when(containerConfig.getPlatforms()) .thenReturn(ImmutableSet.of(new Platform("amd64", "linux"))); - ImmutableList pushImageStepList = + List pushImageStepList = PushImageStep.makeListForManifestList( buildContext, progressDispatcherFactory, registryClient, manifestList, false); - Assert.assertEquals(0, pushImageStepList.size()); + assertThat(pushImageStepList).isEmpty(); } @Test public void testMakeListForManifestList_manifestListAlreadyExists() throws IOException { System.setProperty(JibSystemProperties.SKIP_EXISTING_IMAGES, "true"); - ImmutableList pushImageStepList = + List pushImageStepList = PushImageStep.makeListForManifestList( buildContext, progressDispatcherFactory, registryClient, manifestList, true); - Assert.assertEquals(0, pushImageStepList.size()); + assertThat(pushImageStepList).isEmpty(); } }