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

Propagate WorkingDir in container configuration #902

Merged
merged 4 commits into from
Aug 30, 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 @@ -117,6 +117,7 @@ private Image<CachedLayer> 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 =
Expand Down
35 changes: 29 additions & 6 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 @@ -40,6 +40,7 @@ public static class Builder<T extends Layer> {
@Nullable private ImmutableList<String> entrypoint;
@Nullable private ImmutableList<String> javaArguments;
@Nullable private ImmutableList<Port> exposedPorts;
@Nullable private String workingDirectory;

/**
* Sets the image creation time.
Expand Down Expand Up @@ -111,9 +112,9 @@ public Builder<T> setExposedPorts(@Nullable List<Port> 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<T> addLabels(@Nullable Map<String, String> labels) {
Expand All @@ -124,9 +125,9 @@ public Builder<T> addLabels(@Nullable Map<String, String> 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
*/
Expand All @@ -135,6 +136,17 @@ public Builder<T> 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<T> setWorkingDirectory(@Nullable String workingDirectory) {
this.workingDirectory = workingDirectory;
return this;
}

/**
* Adds a layer to the image.
*
Expand Down Expand Up @@ -167,7 +179,8 @@ public Image<T> build() {
entrypoint,
javaArguments,
exposedPorts,
labelsBuilder.build());
labelsBuilder.build(),
workingDirectory);
}
}

Expand Down Expand Up @@ -199,6 +212,9 @@ public static <T extends Layer> Builder<T> builder() {
/** Labels on the container configuration */
@Nullable private final ImmutableMap<String, String> labels;

/** Working directory on the container configuration */
@Nullable private final String workingDirectory;

private Image(
@Nullable Instant created,
ImageLayers<T> layers,
Expand All @@ -207,7 +223,8 @@ private Image(
@Nullable ImmutableList<String> entrypoint,
@Nullable ImmutableList<String> javaArguments,
@Nullable ImmutableList<Port> exposedPorts,
@Nullable ImmutableMap<String, String> labels) {
@Nullable ImmutableMap<String, String> labels,
@Nullable String workingDirectory) {
this.created = created;
this.layers = layers;
this.history = history;
Expand All @@ -216,6 +233,7 @@ private Image(
this.javaArguments = javaArguments;
this.exposedPorts = exposedPorts;
this.labels = labels;
this.workingDirectory = workingDirectory;
}

@Nullable
Expand Down Expand Up @@ -248,6 +266,11 @@ public ImmutableMap<String, String> getLabels() {
return labels;
}

@Nullable
public String getWorkingDirectory() {
return workingDirectory;
}

public ImmutableList<T> getLayers() {
return layers.getLayers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
* {
Expand Down Expand Up @@ -106,6 +107,9 @@ private static class ConfigurationObjectTemplate implements JsonTemplate {

/** Labels. */
@Nullable private Map<String, String> Labels;

/** Working directory. */
@Nullable private String WorkingDir;
}

/**
Expand Down Expand Up @@ -148,6 +152,10 @@ public void setContainerLabels(@Nullable Map<String, String> labels) {
config.Labels = labels;
}

public void setContainerWorkingDir(@Nullable String workingDirectory) {
config.WorkingDir = workingDirectory;
}

public void addLayerDiffId(DescriptorDigest diffId) {
rootfs.diff_ids.add(diffId);
}
Expand Down Expand Up @@ -194,6 +202,11 @@ Map<String, String> getContainerLabels() {
return config.Labels;
}

@Nullable
String getContainerWorkingDir() {
return config.WorkingDir;
}

@VisibleForTesting
DescriptorDigest getLayerDiffId(int index) {
return rootfs.diff_ids.get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public static Image<Layer> toImage(
}
}

imageBuilder.setWorkingDirectory(containerConfigurationTemplate.getContainerWorkingDir());

return imageBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private <T extends BuildableManifestTemplate> 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),
Expand Down
2 changes: 1 addition & 1 deletion jib-core/src/test/resources/json/containerconfig.json
Original file line number Diff line number Diff line change
@@ -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"]}}
{"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"]}}
Original file line number Diff line number Diff line change
@@ -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}]}
{"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}]}
Original file line number Diff line number Diff line change
@@ -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}]}
{"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}]}
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([#902](https://github.com/GoogleContainerTools/jib/pull/902))

### Changed

Expand Down
1 change: 1 addition & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([#902](https://github.com/GoogleContainerTools/jib/pull/902))

### Changed

Expand Down