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 maven config #867

Merged
merged 5 commits 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
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