diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java index 87da8a3c77..a5a2627b82 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java @@ -23,6 +23,7 @@ import com.google.cloud.tools.jib.configuration.BuildConfiguration; import com.google.cloud.tools.jib.configuration.ContainerConfiguration; import com.google.cloud.tools.jib.image.Image; +import com.google.cloud.tools.jib.image.Layer; import com.google.cloud.tools.jib.image.LayerPropertyNotFoundException; import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.Futures; @@ -102,9 +103,10 @@ private Image afterCachedLayersSteps() imageBuilder.addLayer(NonBlockingSteps.get(buildAndCacheApplicationLayerStep)); } - // Start with environment from base image and overlay build configuration - imageBuilder.addEnvironment( - NonBlockingSteps.get(pullBaseImageStep).getBaseImage().getEnvironment()); + // Parameters that we passthrough from the base image + Image baseImage = NonBlockingSteps.get(pullBaseImageStep).getBaseImage(); + imageBuilder.addEnvironment(baseImage.getEnvironment()); + imageBuilder.addLabels(baseImage.getLabels()); ContainerConfiguration containerConfiguration = buildConfiguration.getContainerConfiguration(); @@ -114,7 +116,7 @@ private Image afterCachedLayersSteps() imageBuilder.setEntrypoint(containerConfiguration.getEntrypoint()); imageBuilder.setJavaArguments(containerConfiguration.getProgramArguments()); imageBuilder.setExposedPorts(containerConfiguration.getExposedPorts()); - imageBuilder.setLabels(containerConfiguration.getLabels()); + imageBuilder.addLabels(containerConfiguration.getLabels()); } // Gets the container configuration content descriptor. diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java index 2619afaf88..30f6e409fb 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java @@ -32,12 +32,12 @@ public static class Builder { private final ImageLayers.Builder imageLayersBuilder = ImageLayers.builder(); private ImmutableMap.Builder environmentBuilder = ImmutableMap.builder(); + private ImmutableMap.Builder labelsBuilder = ImmutableMap.builder(); @Nullable private Instant created; @Nullable private ImmutableList entrypoint; @Nullable private ImmutableList javaArguments; @Nullable private ImmutableList exposedPorts; - @Nullable private ImmutableMap labels; /** * Sets the image creation time. @@ -109,13 +109,27 @@ public Builder setExposedPorts(@Nullable List exposedPorts) { } /** - * Sets the items in the "Labels" field in the container configuration. + * Add items to the "Labels" field in the container configuration. * * @param labels that map of labels to add * @return this */ - public Builder setLabels(@Nullable Map labels) { - this.labels = (labels == null) ? null : ImmutableMap.copyOf(labels); + public Builder addLabels(@Nullable Map labels) { + if (labels != null) { + labelsBuilder.putAll(labels); + } + return this; + } + + /** + * A an item to the "Labels" field in the container configuration. + * + * @param name that name of the label + * @param value the value of the label + * @return this + */ + public Builder addLabel(String name, String value) { + labelsBuilder.put(name, value); return this; } @@ -139,7 +153,7 @@ public Image build() { entrypoint, javaArguments, exposedPorts, - labels); + labelsBuilder.build()); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java index c8360db412..924b5a0ce8 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/BuildImageStepTest.java @@ -77,7 +77,10 @@ public void setUp() throws DigestException { Mockito.when(mockContainerConfiguration.getEntrypoint()).thenReturn(ImmutableList.of()); Image baseImage = - Image.builder().addEnvironment(ImmutableMap.of("NAME", "VALUE")).build(); + Image.builder() + .addEnvironment(ImmutableMap.of("BASE_ENV", "BASE_ENV_VALUE")) + .addLabel("base.label", "base.label.value") + .build(); Mockito.when(mockPullAndCacheBaseImageLayerStep.getFuture()) .thenReturn(Futures.immediateFuture(testCachedLayer)); Mockito.when(mockPullAndCacheBaseImageLayersStep.getFuture()) @@ -116,7 +119,9 @@ public void test_validateAsyncDependencies() throws ExecutionException, Interrup public void test_propagateBaseImageConfiguration() throws ExecutionException, InterruptedException { Mockito.when(mockContainerConfiguration.getEnvironmentMap()) - .thenReturn(ImmutableMap.of("BASE", "IMAGE")); + .thenReturn(ImmutableMap.of("MY_ENV", "MY_ENV_VALUE")); + Mockito.when(mockContainerConfiguration.getLabels()) + .thenReturn(ImmutableMap.of("my.label", "my.label.value")); BuildImageStep buildImageStep = new BuildImageStep( MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()), @@ -128,6 +133,11 @@ public void test_propagateBaseImageConfiguration() mockBuildAndCacheApplicationLayerStep, mockBuildAndCacheApplicationLayerStep)); Image image = buildImageStep.getFuture().get().getFuture().get(); - Assert.assertEquals(ImmutableMap.of("NAME", "VALUE", "BASE", "IMAGE"), image.getEnvironment()); + Assert.assertEquals( + ImmutableMap.of("BASE_ENV", "BASE_ENV_VALUE", "MY_ENV", "MY_ENV_VALUE"), + image.getEnvironment()); + Assert.assertEquals( + ImmutableMap.of("base.label", "base.label.value", "my.label", "my.label.value"), + image.getLabels()); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslatorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslatorTest.java index 66945fbef4..00f9d7f770 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslatorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslatorTest.java @@ -65,7 +65,7 @@ public void setUp() throws DigestException, LayerPropertyNotFoundException { new Port(1000, Protocol.TCP), new Port(2000, Protocol.TCP), new Port(3000, Protocol.UDP))); - testImageBuilder.setLabels(ImmutableMap.of("key1", "value1", "key2", "value2")); + testImageBuilder.addLabels(ImmutableMap.of("key1", "value1", "key2", "value2")); DescriptorDigest fakeDigest = DescriptorDigest.fromDigest( diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 1638e4f333..3dfd7afe54 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. ### Added +- Passthrough labels from base image ([#750](https://github.com/GoogleContainerTools/jib/pull/750/files)) + ### Changed - Reordered classpath in entrypoint to allow dependency patching ([#777](https://github.com/GoogleContainerTools/jib/issues/777)) diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index ca9147b957..e0d1c5ae51 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. ### Added +- Passthrough labels from base image ([#750](https://github.com/GoogleContainerTools/jib/pull/750/files)) + ### Changed - Reordered classpath in entrypoint to allow dependency patching ([#777](https://github.com/GoogleContainerTools/jib/issues/777))