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 'ports' configuration to maven plugin #440

Merged
merged 13 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from 11 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><ports>` parameter to define container's exposed ports (similar to Dockerfile `EXPOSE`) ([#383](https://github.com/GoogleContainerTools/jib/issues/383))

### Changed

- Fetches credentials from inferred credential helper before Docker config ([#401](https://github.com/GoogleContainerTools/jib/issues/401))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void execute() throws MojoExecutionException {
.setJavaArguments(getArgs())
.setJvmFlags(getJvmFlags())
.setEnvironment(getEnvironment())
.setExposedPorts(getExposedPorts())
.build();

// TODO: Instead of disabling logging, have authentication credentials be provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.setJavaArguments(getArgs())
.setJvmFlags(getJvmFlags())
.setEnvironment(getEnvironment())
.setExposedPorts(getExposedPorts())
.setTargetFormat(ImageFormat.valueOf(getFormat()).getManifestTemplateClass())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.setJvmFlags(getJvmFlags())
.setMainClass(mainClass)
.setJavaArguments(getArgs())
.setExposedPorts(getExposedPorts())
.generate(Paths.get(targetDir));

mavenBuildLogger.info("Created Docker context at " + targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static class ContainerParameters {
@Nullable
@Parameter(required = true)
private String format = "Docker";

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

/**
Expand Down Expand Up @@ -194,6 +196,10 @@ List<String> getArgs() {
return container.args;
}

List<String> getExposedPorts() {
return container.ports;
}

String getFormat() {
return Preconditions.checkNotNull(container.format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Path;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -54,6 +55,15 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe
verifier.executeGoal("jib:" + BuildDockerMojo.GOAL_NAME);
verifier.verifyErrorFreeLog();

Assert.assertThat(
new Command("docker", "inspect", imageReference).run(),
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));
return new Command("docker", "run", imageReference).run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ private static String buildAndRun(Path projectRoot, String imageReference)
timeOne > timeTwo);

new Command("docker", "pull", imageReference).run();
Assert.assertThat(
new Command("docker", "inspect", imageReference).run(),
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));
return new Command("docker", "run", imageReference).run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -50,6 +51,16 @@ public void testExecute() throws VerificationException, IOException, Interrupted

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

Assert.assertEquals(
"Hello, world. An argument.\n", new Command("docker", "run", imageName).run());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<configuration>
<container>
<args>An argument.</args>
<ports>
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down
6 changes: 6 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 @@ -31,6 +31,12 @@
<to>
<image>gcr.io/jib-integration-testing/emptyimage:maven</image>
</to>
<container>
<ports>
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
</configuration>
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 @@ -43,6 +43,10 @@
</to>
<container>
<args>An argument.</args>
<ports>
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down