From f61cf97e44faa1ca45898a8951d2e4eda7f607c6 Mon Sep 17 00:00:00 2001 From: Tad Cordle Date: Wed, 22 Aug 2018 11:32:02 -0400 Subject: [PATCH] Add labels to maven config (#867) --- jib-maven-plugin/CHANGELOG.md | 2 ++ .../tools/jib/maven/JibPluginConfiguration.java | 11 ++++++----- .../jib/maven/PluginConfigurationProcessor.java | 3 ++- .../maven/BuildDockerMojoIntegrationTest.java | 10 +++++++++- .../jib/maven/BuildImageMojoIntegrationTest.java | 16 ++++++++++++---- .../resources/projects/default-target/pom.xml | 4 ++++ .../src/test/resources/projects/empty/pom.xml | 4 ++++ .../resources/projects/simple/pom-complex.xml | 4 ++++ .../src/test/resources/projects/simple/pom.xml | 4 ++++ 9 files changed, 47 insertions(+), 11 deletions(-) diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index 55a7d7d283..eca7fbe05c 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. ### Added +- `` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) + ### Changed ### Fixed diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java index 48fbb32b38..6fda990e9e 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java @@ -129,6 +129,8 @@ public static class ContainerParameters { private String format = "Docker"; @Parameter private List ports = Collections.emptyList(); + + @Parameter private Map labels = Collections.emptyMap(); } @Nullable @@ -273,6 +275,10 @@ List getExposedPorts() { return container.ports; } + Map getLabels() { + return container.labels; + } + String getFormat() { return Preconditions.checkNotNull(container.format); } @@ -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; diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java index b2bd0a7bcb..d6ce31295a 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java @@ -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."); diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java index 906e29e653..6944a015f4 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java @@ -56,8 +56,9 @@ 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" @@ -65,6 +66,13 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe + " \"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(); } diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java index ea49317fdd..c8aef461c4 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java @@ -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(); } @@ -117,7 +117,7 @@ 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()); @@ -125,10 +125,11 @@ private static String buildAndRunComplex( 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" @@ -136,6 +137,13 @@ private static void assertHasExposedPorts(String imageReference) + " \"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 { diff --git a/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml b/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml index 3ac515e2da..c413787d0e 100644 --- a/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml @@ -44,6 +44,10 @@ 1000/tcp 2000-2003/udp + + value1 + value2 + true diff --git a/jib-maven-plugin/src/test/resources/projects/empty/pom.xml b/jib-maven-plugin/src/test/resources/projects/empty/pom.xml index 6a7b746b27..b91a48c8ee 100644 --- a/jib-maven-plugin/src/test/resources/projects/empty/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/empty/pom.xml @@ -36,6 +36,10 @@ 1000/tcp 2000-2003/udp + + value1 + value2 + true diff --git a/jib-maven-plugin/src/test/resources/projects/simple/pom-complex.xml b/jib-maven-plugin/src/test/resources/projects/simple/pom-complex.xml index 76ff340f37..dc98ada0ba 100644 --- a/jib-maven-plugin/src/test/resources/projects/simple/pom-complex.xml +++ b/jib-maven-plugin/src/test/resources/projects/simple/pom-complex.xml @@ -66,6 +66,10 @@ 1000/tcp 2000-2003/udp + + value1 + value2 + Docker true diff --git a/jib-maven-plugin/src/test/resources/projects/simple/pom.xml b/jib-maven-plugin/src/test/resources/projects/simple/pom.xml index eb5331900a..1f9485fd48 100644 --- a/jib-maven-plugin/src/test/resources/projects/simple/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/simple/pom.xml @@ -48,6 +48,10 @@ 1000/tcp 2000-2003/udp + + value1 + value2 + true