Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passthrough labels from base image #804

Merged
merged 6 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,9 +103,10 @@ private Image<CachedLayer> 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<Layer> baseImage = NonBlockingSteps.get(pullBaseImageStep).getBaseImage();
imageBuilder.addEnvironment(baseImage.getEnvironment());
imageBuilder.addLabels(baseImage.getLabels());

ContainerConfiguration containerConfiguration =
buildConfiguration.getContainerConfiguration();
Expand All @@ -114,7 +116,7 @@ private Image<CachedLayer> 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.
Expand Down
24 changes: 19 additions & 5 deletions jib-core/src/main/java/com/google/cloud/tools/jib/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public static class Builder<T extends Layer> {

private final ImageLayers.Builder<T> imageLayersBuilder = ImageLayers.builder();
private ImmutableMap.Builder<String, String> environmentBuilder = ImmutableMap.builder();
private ImmutableMap.Builder<String, String> labelsBuilder = ImmutableMap.builder();

@Nullable private Instant created;
@Nullable private ImmutableList<String> entrypoint;
@Nullable private ImmutableList<String> javaArguments;
@Nullable private ImmutableList<Port> exposedPorts;
@Nullable private ImmutableMap<String, String> labels;

/**
* Sets the image creation time.
Expand Down Expand Up @@ -109,13 +109,27 @@ public Builder<T> setExposedPorts(@Nullable List<Port> 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<T> setLabels(@Nullable Map<String, String> labels) {
this.labels = (labels == null) ? null : ImmutableMap.copyOf(labels);
public Builder<T> addLabels(@Nullable Map<String, String> 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<T> addLabel(String name, String value) {
labelsBuilder.put(name, value);
return this;
}

Expand All @@ -139,7 +153,7 @@ public Image<T> build() {
entrypoint,
javaArguments,
exposedPorts,
labels);
labelsBuilder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public void setUp() throws DigestException {
Mockito.when(mockContainerConfiguration.getEntrypoint()).thenReturn(ImmutableList.of());

Image<Layer> 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())
Expand Down Expand Up @@ -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()),
Expand All @@ -128,6 +133,11 @@ public void test_propagateBaseImageConfiguration()
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
Image<CachedLayer> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down