Skip to content

Commit

Permalink
Add labels to maven config (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
TadCordle authored Aug 22, 2018
1 parent 57d18e6 commit f61cf97
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
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

- `<container><labels>` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751))

### Changed

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public static class ContainerParameters {
private String format = "Docker";

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

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

@Nullable
Expand Down Expand Up @@ -273,6 +275,10 @@ List<String> getExposedPorts() {
return container.ports;
}

Map<String, String> getLabels() {
return container.labels;
}

String getFormat() {
return Preconditions.checkNotNull(container.format);
}
Expand Down Expand Up @@ -319,11 +325,6 @@ void setProject(MavenProject project) {
this.project = project;
}

@VisibleForTesting
void setTargetImage(@Nullable String targetImage) {
this.to.image = targetImage;
}

@VisibleForTesting
void setExtraDirectory(String extraDirectory) {
this.extraDirectory = extraDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ static PluginConfigurationProcessor processCommonConfiguration(
jibPluginConfiguration.getJvmFlags(), mainClass))
.setProgramArguments(jibPluginConfiguration.getArgs())
.setEnvironment(jibPluginConfiguration.getEnvironment())
.setExposedPorts(ExposedPortsParser.parse(jibPluginConfiguration.getExposedPorts()));
.setExposedPorts(ExposedPortsParser.parse(jibPluginConfiguration.getExposedPorts()))
.setLabels(jibPluginConfiguration.getLabels());
if (jibPluginConfiguration.getUseCurrentTimestamp()) {
logger.warn(
"Setting image creation time to current time; your image may not be reproducible.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,23 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe
verifier.executeGoal("jib:" + BuildDockerMojo.GOAL_NAME);
verifier.verifyErrorFreeLog();

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\": {}"));
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
" \"Labels\": {\n"
+ " \"key1\": \"value1\",\n"
+ " \"key2\": \"value2\"\n"
+ " }"));
return new Command("docker", "run", imageReference).run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static String buildAndRun(Path projectRoot, String imageReference, boole
verifier.verifyErrorFreeLog();

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

Expand All @@ -117,25 +117,33 @@ private static String buildAndRunComplex(

// Verify output
targetRegistry.pull(imageReference);
assertHasExposedPorts(imageReference);
assertDockerInspectParameters(imageReference);
Instant buildTime =
Instant.parse(
new Command("docker", "inspect", "-f", "{{.Created}}", imageReference).run().trim());
Assert.assertTrue(buildTime.isAfter(before) || buildTime.equals(before));
return new Command("docker", "run", imageReference).run();
}

private static void assertHasExposedPorts(String imageReference)
private static void assertDockerInspectParameters(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\": {}"));
Assert.assertThat(
dockerInspect,
CoreMatchers.containsString(
" \"Labels\": {\n"
+ " \"key1\": \"value1\",\n"
+ " \"key2\": \"value2\"\n"
+ " }"));
}

private static float getBuildTimeFromVerifierLog(Verifier verifier) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
<labels>
<key1>value1</key1>
<key2>value2</key2>
</labels>
</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 @@ -36,6 +36,10 @@
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
<labels>
<key1>value1</key1>
<key2>value2</key2>
</labels>
</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 @@ -66,6 +66,10 @@
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
<labels>
<key1>value1</key1>
<key2>value2</key2>
</labels>
<format>Docker</format>
</container>
<allowInsecureRegistries>true</allowInsecureRegistries>
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 @@ -48,6 +48,10 @@
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
<labels>
<key1>value1</key1>
<key2>value2</key2>
</labels>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down

0 comments on commit f61cf97

Please sign in to comment.