From 912b0a22794a911956a047ca177567f621400cd5 Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Wed, 29 Aug 2018 17:51:04 -0400 Subject: [PATCH 1/4] Propagate WorkingDir in container configuration --- .../jib/builder/steps/BuildImageStep.java | 1 + .../google/cloud/tools/jib/image/Image.java | 21 +++++++++++++++++-- .../json/ContainerConfigurationTemplate.java | 19 ++++++++++++++--- .../jib/image/json/ImageToJsonTranslator.java | 3 +++ .../jib/image/json/JsonToImageTranslator.java | 4 ++++ .../jib/builder/steps/BuildImageStepTest.java | 2 ++ .../ContainerConfigurationTemplateTest.java | 2 ++ .../image/json/ImageToJsonTranslatorTest.java | 1 + .../image/json/JsonToImageTranslatorTest.java | 1 + .../test/resources/json/containerconfig.json | 2 +- .../json/translated_ocimanifest.json | 2 +- .../json/translated_v22manifest.json | 2 +- jib-gradle-plugin/CHANGELOG.md | 1 + jib-maven-plugin/CHANGELOG.md | 1 + 14 files changed, 54 insertions(+), 8 deletions(-) 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 b1b1b58248..9b8d4b292a 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 @@ -117,6 +117,7 @@ private Image afterCachedLayersSteps() } imageBuilder.addEnvironment(baseImage.getEnvironment()); imageBuilder.addLabels(baseImage.getLabels()); + imageBuilder.setWorkingDirectory(baseImage.getWorkingDirectory()); // Add history elements for non-empty layers that don't have one yet Instant layerCreationTime = 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 3e9478494b..a8d18a1880 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 @@ -40,6 +40,7 @@ public static class Builder { @Nullable private ImmutableList entrypoint; @Nullable private ImmutableList javaArguments; @Nullable private ImmutableList exposedPorts; + @Nullable private String workingDirectory; /** * Sets the image creation time. @@ -135,6 +136,11 @@ public Builder addLabel(String name, String value) { return this; } + public Builder setWorkingDirectory(@Nullable String workingDirectory) { + this.workingDirectory = workingDirectory; + return this; + } + /** * Adds a layer to the image. * @@ -167,7 +173,8 @@ public Image build() { entrypoint, javaArguments, exposedPorts, - labelsBuilder.build()); + labelsBuilder.build(), + workingDirectory); } } @@ -199,6 +206,9 @@ public static Builder builder() { /** Labels on the container configuration */ @Nullable private final ImmutableMap labels; + /** Working directory on the container configuration */ + @Nullable private final String workingDirectory; + private Image( @Nullable Instant created, ImageLayers layers, @@ -207,7 +217,8 @@ private Image( @Nullable ImmutableList entrypoint, @Nullable ImmutableList javaArguments, @Nullable ImmutableList exposedPorts, - @Nullable ImmutableMap labels) { + @Nullable ImmutableMap labels, + @Nullable String workingDirectory) { this.created = created; this.layers = layers; this.history = history; @@ -216,6 +227,7 @@ private Image( this.javaArguments = javaArguments; this.exposedPorts = exposedPorts; this.labels = labels; + this.workingDirectory = workingDirectory; } @Nullable @@ -248,6 +260,11 @@ public ImmutableMap getLabels() { return labels; } + @Nullable + public String getWorkingDirectory() { + return workingDirectory; + } + public ImmutableList getLayers() { return layers.getLayers(); } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java index 3aaaf32775..649061e03b 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplate.java @@ -38,9 +38,10 @@ * "config": { * "Env": ["/usr/bin/java"], * "Entrypoint": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"], - * "Cmd": ["arg1", "arg2"] - * "ExposedPorts": { "6000/tcp":{}, "8000/tcp":{}, "9000/tcp":{} } - * "Labels": { "com.example.label": "value" } + * "Cmd": ["arg1", "arg2"], + * "ExposedPorts": { "6000/tcp":{}, "8000/tcp":{}, "9000/tcp":{} }, + * "Labels": { "com.example.label": "value" }, + * "WorkingDir": "/home/user/workspace" * }, * "history": [ * { @@ -106,6 +107,9 @@ private static class ConfigurationObjectTemplate implements JsonTemplate { /** Labels. */ @Nullable private Map Labels; + + /** Working directory. */ + @Nullable private String WorkingDir; } /** @@ -148,6 +152,10 @@ public void setContainerLabels(@Nullable Map labels) { config.Labels = labels; } + public void setContainerWorkingDir(@Nullable String workingDirectory) { + config.WorkingDir = workingDirectory; + } + public void addLayerDiffId(DescriptorDigest diffId) { rootfs.diff_ids.add(diffId); } @@ -194,6 +202,11 @@ Map getContainerLabels() { return config.Labels; } + @Nullable + String getContainerWorkingDir() { + return config.WorkingDir; + } + @VisibleForTesting DescriptorDigest getLayerDiffId(int index) { return rootfs.diff_ids.get(index); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java index 6fb575be9b..1795e4cda8 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/ImageToJsonTranslator.java @@ -138,6 +138,9 @@ public Blob getContainerConfigurationBlob() { // Sets the labels. template.setContainerLabels(image.getLabels()); + // Sets the working directory. + template.setContainerWorkingDir(image.getWorkingDirectory()); + // Serializes into JSON. return JsonTemplateMapper.toBlob(template); } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java index 37c3367408..c4c6a462d8 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java @@ -162,6 +162,10 @@ public static Image toImage( } } + if (containerConfigurationTemplate.getContainerLabels() != null) { + imageBuilder.setWorkingDirectory(containerConfigurationTemplate.getContainerWorkingDir()); + } + return imageBuilder.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 04e3705fc2..4b1461f191 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 @@ -98,6 +98,7 @@ public void setUp() throws DigestException { Image.builder() .addEnvironment(ImmutableMap.of("BASE_ENV", "BASE_ENV_VALUE")) .addLabel("base.label", "base.label.value") + .setWorkingDirectory("/base/working/directory") .addHistory(nonEmptyLayerHistory) .addHistory(emptyLayerHistory) .addHistory(emptyLayerHistory) @@ -160,6 +161,7 @@ public void test_propagateBaseImageConfiguration() Assert.assertEquals( ImmutableMap.of("base.label", "base.label.value", "my.label", "my.label.value"), image.getLabels()); + Assert.assertEquals("/base/working/directory", image.getWorkingDirectory()); Assert.assertEquals(image.getHistory().get(0), nonEmptyLayerHistory); Assert.assertEquals(image.getHistory().get(1), emptyLayerHistory); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplateTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplateTest.java index 87562783c1..d25654429c 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplateTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/ContainerConfigurationTemplateTest.java @@ -60,6 +60,7 @@ public void testToJson() throws IOException, URISyntaxException, DigestException "3000/udp", ImmutableMap.of())); containerConfigJson.setContainerLabels(ImmutableMap.of("key1", "value1", "key2", "value2")); + containerConfigJson.setContainerWorkingDir("/some/workspace"); containerConfigJson.addLayerDiffId( DescriptorDigest.fromDigest( @@ -104,6 +105,7 @@ public void testFromJson() throws IOException, URISyntaxException, DigestExcepti Assert.assertEquals( ImmutableMap.of("key1", "value1", "key2", "value2"), containerConfigJson.getContainerLabels()); + Assert.assertEquals("/some/workspace", containerConfigJson.getContainerWorkingDir()); Assert.assertEquals( DescriptorDigest.fromDigest( "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"), 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 ee92dac87c..726cdd53b8 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 @@ -66,6 +66,7 @@ public void setUp() throws DigestException, LayerPropertyNotFoundException { new Port(2000, Protocol.TCP), new Port(3000, Protocol.UDP))); testImageBuilder.addLabels(ImmutableMap.of("key1", "value1", "key2", "value2")); + testImageBuilder.setWorkingDirectory("/some/workspace"); DescriptorDigest fakeDigest = DescriptorDigest.fromDigest( diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslatorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslatorTest.java index 4a1b31adca..4b0dd995f6 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslatorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslatorTest.java @@ -185,6 +185,7 @@ private void testToImage_buildable( Assert.assertEquals(Instant.ofEpochSecond(20), image.getCreated()); Assert.assertEquals(Arrays.asList("some", "entrypoint", "command"), image.getEntrypoint()); Assert.assertEquals(ImmutableMap.of("VAR1", "VAL1", "VAR2", "VAL2"), image.getEnvironment()); + Assert.assertEquals("/some/workspace", image.getWorkingDirectory()); Assert.assertEquals( ImmutableList.of( new Port(1000, Protocol.TCP), diff --git a/jib-core/src/test/resources/json/containerconfig.json b/jib-core/src/test/resources/json/containerconfig.json index d0ffb8e451..4cb079a8f3 100644 --- a/jib-core/src/test/resources/json/containerconfig.json +++ b/jib-core/src/test/resources/json/containerconfig.json @@ -1 +1 @@ -{"created":"1970-01-01T00:00:20Z","architecture":"amd64","os":"linux","config":{"Env":["VAR1=VAL1","VAR2=VAL2"],"Entrypoint":["some","entrypoint","command"],"Cmd":["arg1","arg2"],"ExposedPorts":{"1000/tcp":{},"2000/tcp":{},"3000/udp":{}},"Labels":{"key1":"value1","key2":"value2"}},"history":[{"created":"1970-01-01T00:00:00Z","author":"Bazel","created_by":"bazel build ...","empty_layer":true},{"created":"1970-01-01T00:00:20Z","author":"Jib","created_by":"jib"}],"rootfs":{"type":"layers","diff_ids":["sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"]}} \ No newline at end of file +{"created":"1970-01-01T00:00:20Z","architecture":"amd64","os":"linux","config":{"Env":["VAR1=VAL1","VAR2=VAL2"],"Entrypoint":["some","entrypoint","command"],"Cmd":["arg1","arg2"],"ExposedPorts":{"1000/tcp":{},"2000/tcp":{},"3000/udp":{}},"Labels":{"key1":"value1","key2":"value2"},"WorkingDir":"/some/workspace"},"history":[{"created":"1970-01-01T00:00:00Z","author":"Bazel","created_by":"bazel build ...","empty_layer":true},{"created":"1970-01-01T00:00:20Z","author":"Jib","created_by":"jib"}],"rootfs":{"type":"layers","diff_ids":["sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"]}} \ No newline at end of file diff --git a/jib-core/src/test/resources/json/translated_ocimanifest.json b/jib-core/src/test/resources/json/translated_ocimanifest.json index 3c7daf77af..5403c875db 100644 --- a/jib-core/src/test/resources/json/translated_ocimanifest.json +++ b/jib-core/src/test/resources/json/translated_ocimanifest.json @@ -1 +1 @@ -{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:5ee46ef07ac9a0b4d5bc5d2733e524a9fd9a3cd9bbe88c75276fbd03f01bc709","size":579},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad","size":1000}]} \ No newline at end of file +{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.oci.image.config.v1+json","digest":"sha256:64b29673c04ae315eb40e66ac8b0898c62ae6e75a09a45c1b89b7ab6adfba8e1","size":610},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar+gzip","digest":"sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad","size":1000}]} \ No newline at end of file diff --git a/jib-core/src/test/resources/json/translated_v22manifest.json b/jib-core/src/test/resources/json/translated_v22manifest.json index d01327ec73..a817387134 100644 --- a/jib-core/src/test/resources/json/translated_v22manifest.json +++ b/jib-core/src/test/resources/json/translated_v22manifest.json @@ -1 +1 @@ -{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","digest":"sha256:5ee46ef07ac9a0b4d5bc5d2733e524a9fd9a3cd9bbe88c75276fbd03f01bc709","size":579},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","digest":"sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad","size":1000}]} \ No newline at end of file +{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","digest":"sha256:64b29673c04ae315eb40e66ac8b0898c62ae6e75a09a45c1b89b7ab6adfba8e1","size":610},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","digest":"sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad","size":1000}]} \ No newline at end of file diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 99c0f49d2b..a9ed2254af 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. - `container.labels` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) - `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875)) +- Propagates working directory from the base image ### Changed diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index ccebb0341c..b57742c273 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. - `` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) - `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875)) +- Propagates working directory from the base image ### Changed From a07f5867dcc15a508d555573b8795326b292926d Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Wed, 29 Aug 2018 17:55:18 -0400 Subject: [PATCH 2/4] No need for null check --- .../cloud/tools/jib/image/json/JsonToImageTranslator.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java index c4c6a462d8..1ac0abd815 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/JsonToImageTranslator.java @@ -162,9 +162,7 @@ public static Image toImage( } } - if (containerConfigurationTemplate.getContainerLabels() != null) { - imageBuilder.setWorkingDirectory(containerConfigurationTemplate.getContainerWorkingDir()); - } + imageBuilder.setWorkingDirectory(containerConfigurationTemplate.getContainerWorkingDir()); return imageBuilder.build(); } From 045ffb9f201f6fca1a52df0daa5002ae647864b7 Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Wed, 29 Aug 2018 18:04:34 -0400 Subject: [PATCH 3/4] Update CHANGELOG files --- jib-gradle-plugin/CHANGELOG.md | 2 +- jib-maven-plugin/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index a9ed2254af..6f22272723 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file. - `container.labels` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) - `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875)) -- Propagates working directory from the base image +- Propagates working directory from the base image ([#902](https://github.com/GoogleContainerTools/jib/pull/902)) ### Changed diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index b57742c273..7597102e39 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file. - `` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) - `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875)) -- Propagates working directory from the base image +- Propagates working directory from the base image ([#902](https://github.com/GoogleContainerTools/jib/pull/902)) ### Changed From cd408e207bcb981096f8bf7c374e836c83447ce0 Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Wed, 29 Aug 2018 18:25:54 -0400 Subject: [PATCH 4/4] Fix and add Javadocs --- .../com/google/cloud/tools/jib/image/Image.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 a8d18a1880..140965fde6 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 @@ -112,9 +112,9 @@ public Builder setExposedPorts(@Nullable List exposedPorts) { } /** - * Add items to the "Labels" field in the container configuration. + * Adds items to the "Labels" field in the container configuration. * - * @param labels that map of labels to add + * @param labels the map of labels to add * @return this */ public Builder addLabels(@Nullable Map labels) { @@ -125,9 +125,9 @@ public Builder addLabels(@Nullable Map labels) { } /** - * A an item to the "Labels" field in the container configuration. + * Adds an item to the "Labels" field in the container configuration. * - * @param name that name of the label + * @param name the name of the label * @param value the value of the label * @return this */ @@ -136,6 +136,12 @@ public Builder addLabel(String name, String value) { return this; } + /** + * Sets the item in the "WorkingDir" field in the container configuration. + * + * @param workingDirectory the working directory + * @return this + */ public Builder setWorkingDirectory(@Nullable String workingDirectory) { this.workingDirectory = workingDirectory; return this;