Skip to content

Commit

Permalink
Allow configuring enablePlatformTags via plugins
Browse files Browse the repository at this point in the history
Creates a new config parameter jib.to.enablePlatformTags and passes it through to the build context.
  • Loading branch information
tylerscoville authored and eifinger committed Jul 12, 2023
1 parent 0ab45c6 commit 2665966
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> registryMirrors = ArrayListMultimap.create();
private boolean enablePlatformTags = false;

/** Instantiate with {@link #to}. */
private Containerizer(
Expand Down Expand Up @@ -339,6 +340,18 @@ public Containerizer addRegistryMirrors(String registry, List<String> 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<String> getAdditionalTags() {
return ImmutableSet.copyOf(additionalTags);
}
Expand Down Expand Up @@ -394,6 +407,10 @@ boolean getAlwaysCacheBaseImage() {
return alwaysCacheBaseImage;
}

boolean getEnablePlatformTags() {
return enablePlatformTags;
}

String getDescription() {
return description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ BuildContext toBuildContext(Containerizer containerizer) throws CacheDirectoryCr
.setEventHandlers(containerizer.buildEventHandlers())
.setAlwaysCacheBaseImage(containerizer.getAlwaysCacheBaseImage())
.setRegistryMirrors(containerizer.getRegistryMirrors())
.setEnablePlatformTags(containerizer.getEnablePlatformTags())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions jib-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>` | *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.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,9 @@ public List<? extends ExtensionConfiguration> getPluginExtensions() {
public List<? extends PlatformConfiguration> getPlatforms() {
return jibExtension.getFrom().getPlatforms().get();
}

@Override
public boolean getEnablePlatformTags() {
return jibExtension.getTo().getEnablePlatformTags();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class TargetImageParameters {
private final Property<String> image;
private final SetProperty<String> tags;
private final CredHelperParameters credHelper;
private boolean enablePlatformTags;

@Inject
public TargetImageParameters(ObjectFactory objectFactory) {
Expand Down Expand Up @@ -123,4 +124,16 @@ public AuthParameters getAuth() {
public void auth(Action<? super AuthParameters> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ private static void configureContainerizer(
PropertyNames.APPLICATION_CACHE, projectProperties.getDefaultCacheDirectory()));

rawConfiguration.getToTags().forEach(containerizer::withAdditionalTag);

containerizer.setEnablePlatformTags(rawConfiguration.getEnablePlatformTags());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ interface CredHelperConfiguration {
Path getImageJsonOutputPath();

List<? extends ExtensionConfiguration> getPluginExtensions();

default boolean getEnablePlatformTags() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogEvent> isLogWarn = logEvent -> logEvent.getLevel() == LogEvent.Level.WARN;
verify(logger, never()).accept(argThat(isLogWarn));
}
Expand Down

0 comments on commit 2665966

Please sign in to comment.