Skip to content

Commit f47000a

Browse files
tylerscovillemsvticket
authored andcommitted
Allow configuring enablePlatformTags via plugins
Creates a new config parameter jib.to.enablePlatformTags and passes it through to the build context.
1 parent 5d9beae commit f47000a

File tree

13 files changed

+57
-2
lines changed

13 files changed

+57
-2
lines changed

jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public static Containerizer to(DockerClient dockerClient, DockerDaemonImage dock
159159
@Nullable private String toolVersion = DEFAULT_TOOL_VERSION;
160160
private boolean alwaysCacheBaseImage = false;
161161
private ListMultimap<String, String> registryMirrors = ArrayListMultimap.create();
162+
private boolean enablePlatformTags = false;
162163

163164
/** Instantiate with {@link #to}. */
164165
private Containerizer(
@@ -339,6 +340,18 @@ public Containerizer addRegistryMirrors(String registry, List<String> mirrors) {
339340
return this;
340341
}
341342

343+
/**
344+
* Enables adding platform tags to images.
345+
*
346+
* @param enablePlatformTags if {@code true}, adds platform tags to images. If {@code false}
347+
* images are not tagged with platform tags.
348+
* @return this
349+
*/
350+
public Containerizer setEnablePlatformTags(boolean enablePlatformTags) {
351+
this.enablePlatformTags = enablePlatformTags;
352+
return this;
353+
}
354+
342355
Set<String> getAdditionalTags() {
343356
return ImmutableSet.copyOf(additionalTags);
344357
}
@@ -394,6 +407,10 @@ boolean getAlwaysCacheBaseImage() {
394407
return alwaysCacheBaseImage;
395408
}
396409

410+
boolean getEnablePlatformTags() {
411+
return enablePlatformTags;
412+
}
413+
397414
String getDescription() {
398415
return description;
399416
}

jib-core/src/main/java/com/google/cloud/tools/jib/api/JibContainerBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ BuildContext toBuildContext(Containerizer containerizer) throws CacheDirectoryCr
736736
.setEventHandlers(containerizer.buildEventHandlers())
737737
.setAlwaysCacheBaseImage(containerizer.getAlwaysCacheBaseImage())
738738
.setRegistryMirrors(containerizer.getRegistryMirrors())
739+
.setEnablePlatformTags(containerizer.getEnablePlatformTags())
739740
.build();
740741
}
741742

jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private void verifyTo(Containerizer containerizer) throws CacheDirectoryCreation
6666
.setBaseImageLayersCache(Paths.get("base/image/layers"))
6767
.setApplicationLayersCache(Paths.get("application/layers"))
6868
.setAllowInsecureRegistries(true)
69-
.setToolName("tool");
69+
.setToolName("tool")
70+
.setEnablePlatformTags(true);
7071

7172
Assert.assertEquals(ImmutableSet.of("tag1", "tag2"), containerizer.getAdditionalTags());
7273
Assert.assertTrue(containerizer.getExecutorService().isPresent());
@@ -77,6 +78,7 @@ private void verifyTo(Containerizer containerizer) throws CacheDirectoryCreation
7778
Paths.get("application/layers"), containerizer.getApplicationLayersCacheDirectory());
7879
Assert.assertTrue(containerizer.getAllowInsecureRegistries());
7980
Assert.assertEquals("tool", containerizer.getToolName());
81+
Assert.assertTrue(containerizer.getEnablePlatformTags());
8082
}
8183

8284
@Test

jib-core/src/test/java/com/google/cloud/tools/jib/api/JibContainerBuilderTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public void testToBuildContext()
149149
.setApplicationLayersCache(Paths.get("application/layers"))
150150
.setExecutorService(executorService)
151151
.addEventHandler(mockJibEventConsumer)
152-
.setAlwaysCacheBaseImage(false);
152+
.setAlwaysCacheBaseImage(false)
153+
.setEnablePlatformTags(true);
153154

154155
ImageConfiguration baseImageConfiguration =
155156
ImageConfiguration.builder(ImageReference.parse("base/image"))
@@ -220,6 +221,8 @@ public void testToBuildContext()
220221
ImmutableSet.of("latest", "tag1", "tag2"), buildContext.getAllTargetImageTags());
221222
Assert.assertEquals("toolName", buildContext.getToolName());
222223
Assert.assertFalse(buildContext.getAlwaysCacheBaseImage());
224+
225+
Assert.assertTrue(buildContext.getEnablePlatformTags());
223226
}
224227

225228
@Test

jib-gradle-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Property | Type | Default | Description
225225
`auth` | [`auth`](#auth-closure) | *None* | Specifies credentials directly (alternative to `credHelper`).
226226
`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-`).
227227
`tags` | `List<String>` | *None* | Additional tags to push to.
228+
`enablePlatformTags` | `boolean` | `false` | When creating multi-platform images takes the tags, suffixes them with the platform, and tags the image.
228229

229230
<a name="auth-closure"></a>`auth` is a closure with the following properties (see [Using Specific Credentials](#using-specific-credentials)):
230231

jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,9 @@ public List<? extends ExtensionConfiguration> getPluginExtensions() {
222222
public List<? extends PlatformConfiguration> getPlatforms() {
223223
return jibExtension.getFrom().getPlatforms().get();
224224
}
225+
226+
@Override
227+
public boolean getEnablePlatformTags() {
228+
return jibExtension.getTo().getEnablePlatformTags();
229+
}
225230
}

jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/TargetImageParameters.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class TargetImageParameters {
3939
private final Property<String> image;
4040
private final SetProperty<String> tags;
4141
private final CredHelperParameters credHelper;
42+
private boolean enablePlatformTags;
4243

4344
@Inject
4445
public TargetImageParameters(ObjectFactory objectFactory) {
@@ -123,4 +124,16 @@ public AuthParameters getAuth() {
123124
public void auth(Action<? super AuthParameters> action) {
124125
action.execute(auth);
125126
}
127+
128+
@Input
129+
public boolean getEnablePlatformTags() {
130+
if (System.getProperty(PropertyNames.ENABLE_PLATFORM_TAGS) != null) {
131+
return Boolean.parseBoolean(System.getProperty(PropertyNames.ENABLE_PLATFORM_TAGS));
132+
}
133+
return enablePlatformTags;
134+
}
135+
136+
public void setEnablePlatformTags(boolean expand) {
137+
enablePlatformTags = expand;
138+
}
126139
}

jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void testGetters() {
8181
Mockito.when(toCredHelperParameters.getEnvironment())
8282
.thenReturn(Collections.singletonMap("ENV_VARIABLE", "Value2"));
8383
Mockito.when(targetImageParameters.getCredHelper()).thenReturn(toCredHelperParameters);
84+
Mockito.when(targetImageParameters.getEnablePlatformTags()).thenReturn(true);
8485

8586
Mockito.when(containerParameters.getAppRoot()).thenReturn("/app/root");
8687
Mockito.when(containerParameters.getArgs()).thenReturn(Arrays.asList("--log", "info"));
@@ -150,5 +151,6 @@ public void testGetters() {
150151
Assert.assertEquals(Paths.get("id/path"), rawConfiguration.getImageIdOutputPath());
151152
Assert.assertEquals(Paths.get("json/path"), rawConfiguration.getImageJsonOutputPath());
152153
Assert.assertEquals(Paths.get("tar/path"), rawConfiguration.getTarOutputPath());
154+
Assert.assertTrue(rawConfiguration.getEnablePlatformTags());
153155
}
154156
}

jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ public void testProperties() {
453453
assertThat(testJibExtension.getTo().getTags()).containsExactly("tag1", "tag2", "tag3");
454454
System.setProperty("jib.to.credHelper", "credHelper");
455455
assertThat(testJibExtension.getTo().getCredHelper().getHelper()).isEqualTo("credHelper");
456+
System.setProperty("jib.to.enablePlatformTags", "true");
457+
assertThat(testJibExtension.getTo().getEnablePlatformTags()).isTrue();
456458

457459
System.setProperty("jib.container.appRoot", "appRoot");
458460
assertThat(testJibExtension.getContainer().getAppRoot()).isEqualTo("appRoot");

jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ private static void configureContainerizer(
10541054
PropertyNames.APPLICATION_CACHE, projectProperties.getDefaultCacheDirectory()));
10551055

10561056
rawConfiguration.getToTags().forEach(containerizer::withAdditionalTag);
1057+
1058+
containerizer.setEnablePlatformTags(rawConfiguration.getEnablePlatformTags());
10571059
}
10581060

10591061
/**

0 commit comments

Comments
 (0)