Skip to content

Commit

Permalink
Support for volumes on plugins (GoogleContainerTools#1278)
Browse files Browse the repository at this point in the history
* Support for volumes on maven and gradle plugins.
* refactoring exception name, adding more unit and integration tests.
* change logs
* normalizing exception handling.
* Propagate the cause of the exception.
  • Loading branch information
paivagustavo authored and chanseokoh committed Nov 28, 2018
1 parent 7fe4b4b commit ebdae60
Show file tree
Hide file tree
Showing 26 changed files with 204 additions and 17 deletions.
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Image ID is now written to `build/jib-image.id` ([#1204](https://github.com/GoogleContainerTools/jib/issues/1204))
- `jib.container.entrypoint = 'INHERIT'` allows inheriting `ENTRYPOINT` and `CMD` from the base image. While inheriting `ENTRYPOINT`, you can also override `CMD` using `jib.container.args`.
- `container.workingDirectory` configuration parameter to set the working directory ([#1225](https://github.com/GoogleContainerTools/jib/issues/1225))
- Adds support for configuring volumes ([#1121](https://github.com/GoogleContainerTools/jib/issues/1121))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DefaultTargetProjectIntegrationTest {
public static final TestProject defaultTargetTestProject = new TestProject("default-target");

/**
* Asserts that the test project has the required exposed ports and labels.
* Asserts that the test project has the required exposed ports, labels and volumes.
*
* @param imageReference the image to test
* @throws IOException if the {@code docker inspect} command fails to run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void assertWorkingDirectory(String expected, String imageReferenc
}

/**
* Asserts that the test project has the required exposed ports and labels.
* Asserts that the test project has the required exposed ports, labels and volumes.
*
* @param imageReference the image to test
* @throws IOException if the {@code docker inspect} command fails to run
Expand All @@ -80,6 +80,13 @@ private static void assertWorkingDirectory(String expected, String imageReferenc
private static void assertDockerInspect(String imageReference)
throws IOException, InterruptedException {
String dockerInspect = new Command("docker", "inspect", imageReference).run();
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
" \"Volumes\": {\n"
+ " \"/var/log\": {},\n"
+ " \"/var/log2\": {}\n"
+ " },"));
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jib {
args = ['An argument.']
ports = ['1000/tcp', '2000-2003/udp']
labels = [key1:'value1', key2:'value2']
volumes = ['/var/log', '/var/log2']
workingDirectory = '/home'
}
extraDirectory = file('src/main/custom-extra-dir')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jib {
environment = [env1:'envvalue1', env2:'envvalue2']
ports = ['1000/tcp', '2000-2003/udp']
labels = [key1:'value1', key2:'value2']
volumes = ['/var/log', '/var/log2']
}
extraDirectory {
path = file('src/main/custom-extra-dir')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.cloud.tools.jib.plugins.common.InferredAuthRetrievalException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
Expand Down Expand Up @@ -153,6 +154,9 @@ public void buildDocker()
"container.workingDirectory is not an absolute Unix-style path: "
+ ex.getInvalidPathValue(),
ex);
} catch (InvalidContainerVolumeException ex) {
throw new GradleException(
"container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.cloud.tools.jib.plugins.common.InferredAuthRetrievalException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
Expand Down Expand Up @@ -133,6 +134,9 @@ public void buildImage()
"container.workingDirectory is not an absolute Unix-style path: "
+ ex.getInvalidPathValue(),
ex);
} catch (InvalidContainerVolumeException ex) {
throw new GradleException(
"container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.cloud.tools.jib.plugins.common.InferredAuthRetrievalException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
Expand Down Expand Up @@ -150,6 +151,9 @@ public void buildTar()
"container.workingDirectory is not an absolute Unix-style path: "
+ ex.getInvalidPathValue(),
ex);
} catch (InvalidContainerVolumeException ex) {
throw new GradleException(
"container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ContainerParameters {
@Nullable private List<String> args;
private ImageFormat format = ImageFormat.Docker;
private List<String> ports = Collections.emptyList();
private List<String> volumes = Collections.emptyList();
private Map<String, String> labels = Collections.emptyMap();
private String appRoot = "";
@Nullable private String user;
Expand Down Expand Up @@ -162,6 +163,20 @@ public void setPorts(List<String> ports) {
this.ports = ports;
}

@Input
@Optional
public List<String> getVolumes() {
if (System.getProperty(PropertyNames.CONTAINER_VOLUMES) != null) {
return ConfigurationPropertyValidator.parseListProperty(
System.getProperty(PropertyNames.CONTAINER_VOLUMES));
}
return volumes;
}

public void setVolumes(List<String> volumes) {
this.volumes = volumes;
}

@Input
@Optional
public Map<String, String> getLabels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public Map<String, String> getLabels() {
return jibExtension.getContainer().getLabels();
}

@Override
public List<String> getVolumes() {
return jibExtension.getContainer().getVolumes();
}

@Override
public List<String> getPorts() {
return jibExtension.getContainer().getPorts();
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 @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Image ID is now written to `target/jib-image.id` ([#1204](https://github.com/GoogleContainerTools/jib/issues/1204))
- `<container><entrypoint>INHERIT</entrypoint></container>` allows inheriting `ENTRYPOINT` and `CMD` from the base image. While inheriting `ENTRYPOINT`, you can also override `CMD` using `<container><args>`.
- `<container><workingDirectory>` configuration parameter to set the working directory ([#1225](https://github.com/GoogleContainerTools/jib/issues/1225))
- Adds support for configuring volumes ([#1121](https://github.com/GoogleContainerTools/jib/issues/1121))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.cloud.tools.jib.plugins.common.InferredAuthRetrievalException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
Expand Down Expand Up @@ -121,6 +122,9 @@ public void execute() throws MojoExecutionException {
+ ex.getInvalidPathValue(),
ex);

} catch (InvalidContainerVolumeException ex) {
throw new MojoExecutionException(
"<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidImageReferenceException
| IOException
| CacheDirectoryCreationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.cloud.tools.jib.plugins.common.InferredAuthRetrievalException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
Expand Down Expand Up @@ -137,6 +138,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
+ ex.getInvalidPathValue(),
ex);

} catch (InvalidContainerVolumeException ex) {
throw new MojoExecutionException(
"<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidImageReferenceException
| IOException
| CacheDirectoryCreationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions;
import com.google.cloud.tools.jib.plugins.common.InferredAuthRetrievalException;
import com.google.cloud.tools.jib.plugins.common.InvalidAppRootException;
import com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException;
import com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException;
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
Expand Down Expand Up @@ -115,6 +116,9 @@ public void execute() throws MojoExecutionException {
+ ex.getInvalidPathValue(),
ex);

} catch (InvalidContainerVolumeException ex) {
throw new MojoExecutionException(
"<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidImageReferenceException
| IOException
| CacheDirectoryCreationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public static class ContainerParameters {

@Parameter private List<String> ports = Collections.emptyList();

@Parameter private List<String> volumes = Collections.emptyList();

@Parameter private Map<String, String> labels = Collections.emptyMap();

@Parameter private String appRoot = "";
Expand Down Expand Up @@ -417,6 +419,19 @@ List<String> getExposedPorts() {
return container.ports;
}

/**
* Gets the configured volumes.
*
* @return the configured volumes
*/
List<String> getVolumes() {
if (System.getProperty(PropertyNames.CONTAINER_VOLUMES) != null) {
return ConfigurationPropertyValidator.parseListProperty(
System.getProperty(PropertyNames.CONTAINER_VOLUMES));
}
return container.volumes;
}

/**
* Gets the configured labels.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public Map<String, String> getLabels() {
return jibPluginConfiguration.getLabels();
}

@Override
public List<String> getVolumes() {
return jibPluginConfiguration.getVolumes();
}

@Override
public List<String> getPorts() {
return jibPluginConfiguration.getExposedPorts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe
buildToDockerDaemon(projectRoot, imageReference, "pom.xml");

String dockerInspect = new Command("docker", "inspect", imageReference).run();
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
" \"Volumes\": {\n"
+ " \"/var/log\": {},\n"
+ " \"/var/log2\": {}\n"
+ " },"));
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<key1>value1</key1>
<key2>value2</key2>
</labels>
<volumes>
<volume>/var/log</volume>
<volume>/var/log2</volume>
</volumes>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down
4 changes: 4 additions & 0 deletions jib-maven-plugin/src/test/resources/projects/empty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<key1>value1</key1>
<key2>value2</key2>
</labels>
<volumes>
<volume>/var/log</volume>
<volume>/var/log2</volume>
</volumes>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<key1>value1</key1>
<key2>value2</key2>
</labels>
<volumes>
<volume>/var/log</volume>
<volume>/var/log2</volume>
</volumes>
<format>Docker</format>
</container>
<extraDirectory>
Expand Down
4 changes: 4 additions & 0 deletions jib-maven-plugin/src/test/resources/projects/simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<key1>value1</key1>
<key2>value2</key2>
</labels>
<volumes>
<volume>/var/log</volume>
<volume>/var/log2</volume>
</volumes>
<workingDirectory>/home</workingDirectory>
</container>
<extraDirectory>${project.basedir}/src/main/jib-custom</extraDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2018 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.plugins.common;

/**
* Indicates that the {@code container.volumes} config value has at least one invalid path. (The
* path is not in the absolute unix-path style).
*/
public class InvalidContainerVolumeException extends Exception {

private String invalidVolume;

InvalidContainerVolumeException(String message, String invalidVolume, Throwable cause) {
super(message, cause);
this.invalidVolume = invalidVolume;
}

public String getInvalidVolume() {
return invalidVolume;
}
}
Loading

0 comments on commit ebdae60

Please sign in to comment.