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

Add labels to docker context generator #870

Merged
merged 1 commit into from
Aug 22, 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 @@ -31,6 +31,8 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -103,6 +105,7 @@ private static void addIfNotEmpty(
private String mainClass = "";
private List<String> javaArguments = Collections.emptyList();
private List<String> exposedPorts = Collections.emptyList();
private Map<String, String> labels = Collections.emptyMap();

/**
* Constructs a Docker context generator for a Java application.
Expand Down Expand Up @@ -190,6 +193,17 @@ public JavaDockerContextGenerator setExposedPorts(List<String> exposedPorts) {
return this;
}

/**
* Sets the labels
*
* @param labels the map of labels
* @return this
*/
public JavaDockerContextGenerator setLabels(Map<String, String> labels) {
this.labels = labels;
return this;
}

/**
* Creates the Docker context in {@code #targetDirectory}.
*
Expand Down Expand Up @@ -238,6 +252,9 @@ public void generate(Path targetDirectory) throws IOException {
*
* EXPOSE [port]
* [More EXPOSE instructions, if necessary]
* LABEL [key1]="[value1]" \
* [key2]="[value2]" \
* [...]
* ENTRYPOINT java [jvm flags] -cp [classpaths] [main class]
* CMD [main class args]
* }</pre>
Expand All @@ -256,10 +273,22 @@ String makeDockerfile() throws JsonProcessingException {
.append(" ")
.append(copyDirective.extractionPath);
}

dockerfile.append("\n");
for (String port : exposedPorts) {
dockerfile.append("\nEXPOSE ").append(port);
}

boolean firstLabel = true;
for (Entry<String, String> label : labels.entrySet()) {
dockerfile
.append(firstLabel ? "\nLABEL " : " \\\n ")
.append(label.getKey())
.append("=")
.append(objectMapper.writeValueAsString(label.getValue()));
firstLabel = false;
}

dockerfile
.append("\nENTRYPOINT ")
.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.cloud.tools.jib.filesystem.DirectoryWalker;
import com.google.cloud.tools.jib.image.LayerEntry;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
Expand All @@ -30,6 +31,7 @@
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Rule;
Expand Down Expand Up @@ -124,6 +126,14 @@ public void testMakeDockerfile() throws IOException {
String expectedMainClass = "SomeMainClass";
List<String> expectedJavaArguments = Arrays.asList("arg1", "arg2");
List<String> exposedPorts = Arrays.asList("1000/tcp", "2000-2010/udp");
Map<String, String> expectedLabels =
ImmutableMap.of(
"key1",
"value",
"key2",
"value with\\backslashes\"and\\\\\"\"quotes\"\\",
"key3",
"value3");

Mockito.when(mockJavaLayerConfigurations.getDependenciesLayerEntry())
.thenReturn(
Expand All @@ -145,6 +155,7 @@ public void testMakeDockerfile() throws IOException {
.setMainClass(expectedMainClass)
.setJavaArguments(expectedJavaArguments)
.setExposedPorts(exposedPorts)
.setLabels(expectedLabels)
.makeDockerfile();

// Need to split/rejoin the string here to avoid cross-platform troubles
Expand Down
3 changes: 3 additions & 0 deletions jib-core/src/test/resources/sampleDockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ COPY root /

EXPOSE 1000/tcp
EXPOSE 2000-2010/udp
LABEL key1="value" \
key2="value with\\backslashes\"and\\\\\"\"quotes\"\\" \
key3="value3"
ENTRYPOINT ["java","-flag","another\"Flag","-cp","/app/resources/:/app/classes/:/app/libs/*","SomeMainClass"]
CMD ["arg1","arg2"]
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static String buildAndRun(TestProject testProject, String imageReference
Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(imageReference));

new Command("docker", "pull", imageReference).run();
assertHasExposedPorts(imageReference);
assertDockerInspect(imageReference);
return new Command("docker", "run", imageReference).run();
}

Expand All @@ -80,8 +80,7 @@ private static String buildAndRunComplex(
Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(imageReference));

targetRegistry.pull(imageReference);
assertHasExposedPorts(imageReference);
assertHasLabels(imageReference);
assertDockerInspect(imageReference);
return new Command("docker", "run", imageReference).run();
}

Expand All @@ -94,8 +93,7 @@ private static String buildToDockerDaemonAndRun(TestProject testProject, String
buildResult, JibPlugin.BUILD_DOCKER_TASK_NAME, "Built image to Docker daemon as ");
Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(imageReference));

assertHasExposedPorts(imageReference);
assertHasLabels(imageReference);
assertDockerInspect(imageReference);
return new Command("docker", "run", imageReference).run();
}

Expand Down Expand Up @@ -137,36 +135,26 @@ private static void assertSimpleCreationTimeIsAfter(Instant before, String image
}

/**
* Asserts that the test project has the required exposed ports.
* Asserts that the test project has the required exposed ports and labels.
*
* @param imageReference the image to test
* @throws IOException if the {@code docker inspect} command fails to run
* @throws InterruptedException if the {@code docker inspect} command is interrupted
*/
private static void assertHasExposedPorts(String imageReference)
private static void assertDockerInspect(String imageReference)
throws IOException, InterruptedException {
String dockerInspect = new Command("docker", "inspect", imageReference).run();
Assert.assertThat(
new Command("docker", "inspect", imageReference).run(),
dockerInspect,
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));
}

/**
* Asserts that the test project has the required labels.
*
* @param imageReference the image to test
* @throws IOException if the {@code docker inspect} command fails to run
* @throws InterruptedException if the {@code docker inspect} command is interrupted
*/
private static void assertHasLabels(String imageReference)
throws IOException, InterruptedException {
Assert.assertThat(
new Command("docker", "inspect", imageReference).run(),
dockerInspect,
CoreMatchers.containsString(
" \"Labels\": {\n"
+ " \"key1\": \"value1\",\n"
Expand Down Expand Up @@ -300,7 +288,7 @@ public void testBuildTar_simple() throws IOException, InterruptedException {
new Command("docker", "load", "--input", outputPath).run();
Assert.assertEquals(
"Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", targetImage).run());
assertHasExposedPorts(targetImage);
assertDockerInspect(targetImage);
assertSimpleCreationTimeIsAfter(beforeBuild, targetImage);
}

Expand All @@ -325,7 +313,7 @@ public void testDockerContext() throws IOException, InterruptedException {
.toString())
.run();

assertHasExposedPorts(imageName);
assertDockerInspect(imageName);
Assert.assertEquals(
"Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", imageName).run());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public void generateDockerContext() {
.setMainClass(mainClass)
.setJavaArguments(jibExtension.getArgs())
.setExposedPorts(jibExtension.getExposedPorts())
.setLabels(jibExtension.getLabels())
.generate(Paths.get(targetDir));

gradleJibLogger.lifecycle("Created Docker context at " + targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void execute() throws MojoExecutionException {
.setMainClass(mainClass)
.setJavaArguments(getArgs())
.setExposedPorts(getExposedPorts())
.setLabels(getLabels())
.generate(Paths.get(targetDir));

mavenJibLogger.lifecycle("Created Docker context at " + targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,23 @@ public void testExecute() throws VerificationException, IOException, Interrupted

String imageName = "jib/integration-test" + System.nanoTime();
new Command("docker", "build", "-t", imageName, dockerContextDirectory.toString()).run();
String dockerInspect = new Command("docker", "inspect", imageName).run();
Assert.assertThat(
new Command("docker", "inspect", imageName).run(),
dockerInspect,
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
" \"Labels\": {\n"
+ " \"key1\": \"value1\",\n"
+ " \"key2\": \"value2\"\n"
+ " }"));

Assert.assertEquals(
"Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", imageName).run());
Expand Down