diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java b/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java index 96442a5135..ada5cb6eff 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java @@ -159,6 +159,7 @@ public static Containerizer to(DockerClient dockerClient, DockerDaemonImage dock @Nullable private String toolVersion = DEFAULT_TOOL_VERSION; private boolean alwaysCacheBaseImage = false; private ListMultimap registryMirrors = ArrayListMultimap.create(); + private boolean enablePlatformTags = false; /** Instantiate with {@link #to}. */ private Containerizer( @@ -339,6 +340,18 @@ public Containerizer addRegistryMirrors(String registry, List mirrors) { return this; } + /** + * Enables adding platform tags to images. + * + * @param enablePlatformTags if {@code true}, adds platform tags to images. If {@code false} + * images are not tagged with platform tags. + * @return this + */ + public Containerizer setEnablePlatformTags(boolean enablePlatformTags) { + this.enablePlatformTags = enablePlatformTags; + return this; + } + Set getAdditionalTags() { return ImmutableSet.copyOf(additionalTags); } @@ -394,6 +407,10 @@ boolean getAlwaysCacheBaseImage() { return alwaysCacheBaseImage; } + boolean getEnablePlatformTags() { + return enablePlatformTags; + } + String getDescription() { return description; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/api/JibContainerBuilder.java b/jib-core/src/main/java/com/google/cloud/tools/jib/api/JibContainerBuilder.java index 9a809ef798..4549a4664c 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/api/JibContainerBuilder.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/api/JibContainerBuilder.java @@ -736,6 +736,7 @@ BuildContext toBuildContext(Containerizer containerizer) throws CacheDirectoryCr .setEventHandlers(containerizer.buildEventHandlers()) .setAlwaysCacheBaseImage(containerizer.getAlwaysCacheBaseImage()) .setRegistryMirrors(containerizer.getRegistryMirrors()) + .setEnablePlatformTags(containerizer.getEnablePlatformTags()) .build(); } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java index 6d1b34db69..32ce3ce942 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java @@ -66,7 +66,8 @@ private void verifyTo(Containerizer containerizer) throws CacheDirectoryCreation .setBaseImageLayersCache(Paths.get("base/image/layers")) .setApplicationLayersCache(Paths.get("application/layers")) .setAllowInsecureRegistries(true) - .setToolName("tool"); + .setToolName("tool") + .setEnablePlatformTags(true); Assert.assertEquals(ImmutableSet.of("tag1", "tag2"), containerizer.getAdditionalTags()); Assert.assertTrue(containerizer.getExecutorService().isPresent()); @@ -77,6 +78,7 @@ private void verifyTo(Containerizer containerizer) throws CacheDirectoryCreation Paths.get("application/layers"), containerizer.getApplicationLayersCacheDirectory()); Assert.assertTrue(containerizer.getAllowInsecureRegistries()); Assert.assertEquals("tool", containerizer.getToolName()); + Assert.assertTrue(containerizer.getEnablePlatformTags()); } @Test diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/api/JibContainerBuilderTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/api/JibContainerBuilderTest.java index 74ed36bca1..17d9d6de9c 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/api/JibContainerBuilderTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/api/JibContainerBuilderTest.java @@ -149,7 +149,8 @@ public void testToBuildContext() .setApplicationLayersCache(Paths.get("application/layers")) .setExecutorService(executorService) .addEventHandler(mockJibEventConsumer) - .setAlwaysCacheBaseImage(false); + .setAlwaysCacheBaseImage(false) + .setEnablePlatformTags(true); ImageConfiguration baseImageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")) @@ -220,6 +221,8 @@ public void testToBuildContext() ImmutableSet.of("latest", "tag1", "tag2"), buildContext.getAllTargetImageTags()); Assert.assertEquals("toolName", buildContext.getToolName()); Assert.assertFalse(buildContext.getAlwaysCacheBaseImage()); + + Assert.assertTrue(buildContext.getEnablePlatformTags()); } @Test diff --git a/jib-gradle-plugin/README.md b/jib-gradle-plugin/README.md index 5c4001018d..9f2cd7b925 100644 --- a/jib-gradle-plugin/README.md +++ b/jib-gradle-plugin/README.md @@ -225,6 +225,7 @@ Property | Type | Default | Description `auth` | [`auth`](#auth-closure) | *None* | Specifies credentials directly (alternative to `credHelper`). `credHelper` | `String` | *None* | Specifies a credential helper that can authenticate pushing the target image. This parameter can either be configured as an absolute path to the credential helper executable or as a credential helper suffix (following `docker-credential-`). `tags` | `List` | *None* | Additional tags to push to. +`enablePlatformTags` | `boolean` | `false` | When creating multi-platform images takes the tags, suffixes them with the platform, and tags the image. `auth` is a closure with the following properties (see [Using Specific Credentials](#using-specific-credentials)): diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java index 10195cc756..d8b5a63c09 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java @@ -222,4 +222,9 @@ public List getPluginExtensions() { public List getPlatforms() { return jibExtension.getFrom().getPlatforms().get(); } + + @Override + public boolean getEnablePlatformTags() { + return jibExtension.getTo().getEnablePlatformTags(); + } } diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TargetImageParameters.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TargetImageParameters.java index 684f7f4b21..792154a9a4 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TargetImageParameters.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TargetImageParameters.java @@ -39,6 +39,7 @@ public class TargetImageParameters { private final Property image; private final SetProperty tags; private final CredHelperParameters credHelper; + private boolean enablePlatformTags; @Inject public TargetImageParameters(ObjectFactory objectFactory) { @@ -123,4 +124,16 @@ public AuthParameters getAuth() { public void auth(Action action) { action.execute(auth); } + + @Input + public boolean getEnablePlatformTags() { + if (System.getProperty(PropertyNames.ENABLE_PLATFORM_TAGS) != null) { + return Boolean.parseBoolean(System.getProperty(PropertyNames.ENABLE_PLATFORM_TAGS)); + } + return enablePlatformTags; + } + + public void setEnablePlatformTags(boolean expand) { + enablePlatformTags = expand; + } } diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java index 8655241553..1833b60382 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java @@ -81,6 +81,7 @@ public void testGetters() { Mockito.when(toCredHelperParameters.getEnvironment()) .thenReturn(Collections.singletonMap("ENV_VARIABLE", "Value2")); Mockito.when(targetImageParameters.getCredHelper()).thenReturn(toCredHelperParameters); + Mockito.when(targetImageParameters.getEnablePlatformTags()).thenReturn(true); Mockito.when(containerParameters.getAppRoot()).thenReturn("/app/root"); Mockito.when(containerParameters.getArgs()).thenReturn(Arrays.asList("--log", "info")); @@ -150,5 +151,6 @@ public void testGetters() { Assert.assertEquals(Paths.get("id/path"), rawConfiguration.getImageIdOutputPath()); Assert.assertEquals(Paths.get("json/path"), rawConfiguration.getImageJsonOutputPath()); Assert.assertEquals(Paths.get("tar/path"), rawConfiguration.getTarOutputPath()); + Assert.assertTrue(rawConfiguration.getEnablePlatformTags()); } } diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java index 440ae17779..e97bab8025 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java @@ -453,6 +453,8 @@ public void testProperties() { assertThat(testJibExtension.getTo().getTags()).containsExactly("tag1", "tag2", "tag3"); System.setProperty("jib.to.credHelper", "credHelper"); assertThat(testJibExtension.getTo().getCredHelper().getHelper()).isEqualTo("credHelper"); + System.setProperty("jib.to.enablePlatformTags", "true"); + assertThat(testJibExtension.getTo().getEnablePlatformTags()).isTrue(); System.setProperty("jib.container.appRoot", "appRoot"); assertThat(testJibExtension.getContainer().getAppRoot()).isEqualTo("appRoot"); diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java index 8ac4001db5..c9ad85fbb9 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java @@ -1054,6 +1054,8 @@ private static void configureContainerizer( PropertyNames.APPLICATION_CACHE, projectProperties.getDefaultCacheDirectory())); rawConfiguration.getToTags().forEach(containerizer::withAdditionalTag); + + containerizer.setEnablePlatformTags(rawConfiguration.getEnablePlatformTags()); } /** diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java index 14e27c2ed3..655cabe344 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java @@ -27,6 +27,7 @@ public class PropertyNames { public static final String TO_IMAGE = "jib.to.image"; public static final String TO_IMAGE_ALTERNATE = "image"; public static final String TO_TAGS = "jib.to.tags"; + public static final String ENABLE_PLATFORM_TAGS = "jib.to.enablePlatformTags"; public static final String TO_CRED_HELPER = "jib.to.credHelper"; public static final String TO_AUTH_USERNAME = "jib.to.auth.username"; public static final String TO_AUTH_PASSWORD = "jib.to.auth.password"; diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java index b31cf326ec..d7f5907810 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/RawConfiguration.java @@ -134,4 +134,8 @@ interface CredHelperConfiguration { Path getImageJsonOutputPath(); List getPluginExtensions(); + + default boolean getEnablePlatformTags() { + return false; + } } diff --git a/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java b/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java index 51404d1351..55dbf2e0ca 100644 --- a/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java +++ b/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java @@ -216,6 +216,8 @@ public void testPluginConfigurationProcessor_defaults() verify(containerizer).setBaseImageLayersCache(Containerizer.DEFAULT_BASE_CACHE_DIRECTORY); verify(containerizer).setApplicationLayersCache(appCacheDirectory); + verify(containerizer).setEnablePlatformTags(false); + ArgumentMatcher isLogWarn = logEvent -> logEvent.getLevel() == LogEvent.Level.WARN; verify(logger, never()).accept(argThat(isLogWarn)); }