Skip to content

Commit

Permalink
Add ContainerNetworking section with Aliases for custom network RunIm…
Browse files Browse the repository at this point in the history
…ageConfigurations. Fixes #466

Signed-off-by: Daniel Wegener <daniel@wegener.me>
  • Loading branch information
danielwegener committed Jul 27, 2016
1 parent 7e1af5b commit 6471c19
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 3 deletions.
98 changes: 98 additions & 0 deletions samples/custom-net/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!--
Sample project for demonstrating the custom network mode
Call it with 'mvn install'.
It will automatically create the custom network "test-network" and create two automatically named containers that can
talk to each other via their netAlias names.
-->

<parent>
<groupId>io.fabric8</groupId>
<artifactId>dmp-sample-parent</artifactId>
<version>0.15.12</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dmp-custom-net</artifactId>
<version>0.15.12</version>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<autoCreateCustomNetworks>true</autoCreateCustomNetworks>
<watchInterval>500</watchInterval>
<logDate>default</logDate>
<verbose>true</verbose>
<autoPull>always</autoPull>
<images>
<image>
<alias>box1</alias>
<name>busybox</name>
<run>
<net>test-network</net>
<networkAlias>box1,box1-alternative</networkAlias>
<namingStrategy>none</namingStrategy>
<cmd>
<exec>
<args>sh</args>
<args>-c</args>
<args>tail -f /dev/null</args>
</exec>
</cmd>
<log>
<prefix>1</prefix> <color>cyan</color>
</log>
</run>
</image>
<image>
<alias>box2</alias>
<name>busybox</name>
<run>
<net>test-network</net>
<networkAlias>box2</networkAlias>
<namingStrategy>none</namingStrategy>
<cmd>
<exec>
<args>sh</args>
<args>-c</args>
<args>nslookup box1-alternative; tail -f /dev/null</args>
</exec>
</cmd>
<wait>
<log>box1.test-network</log>
</wait>
<log>
<prefix>2</prefix> <color>blue</color>
</log>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
1 change: 1 addition & 0 deletions samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

<modules>
<module>net</module>
<module>custom-net</module>
<module>properties</module>
<module>dockerignore</module>
<module>data-jolokia-demo</module>
Expand Down
2 changes: 1 addition & 1 deletion src/main/asciidoc/inc/_image-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linking images together or for specifying it with the global *image* configurati
| Element which contains all the configuration aspects when doing a <<{plugin}:build>>. This element can be omitted if the image is only pulled from a registry e.g. as support for integration tests like database images.

| *run*
| Eelement which describe how containers should be
| Element which describe how containers should be
created and run when <<{plugin}:start>> is called. If this image is only used a _data container_ (i.e. is supposed only to be mounted as a volume) for exporting artifacts via volumes this section can be missing.

| *external*
Expand Down
3 changes: 3 additions & 0 deletions src/main/asciidoc/inc/start/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ a| Network mode for the container:
* *container:<alias or name>* : Connect to the network of the specified container
* *<custom network>* : Use the specified custom network which must be created before with `docker network create`. Available for Docker 1.9 and newer. For more about the networking options please refer to the https://docs.docker.com/engine/userguide/networking/work-with-networks[Docker documentation].

| *networkAlias*
| List of `network-alias` names that provides a way for a container to be discovered by alternate names by any other container within the scope of a particular network. This configuration only has effect for a *custom network* configuration.

| *portPropertyFile*
| File path into which the mapped port properties are written. The format of this file and its purpose are also described in <<start-port-mapping,Port mapping>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public ContainerCreateConfig hostConfig(ContainerHostConfig startConfig) {
return add("HostConfig", startConfig.toJsonObject());
}

public ContainerCreateConfig networkingConfig(ContainerNetworkingConfig startConfig) {
return add("NetworkingConfig", startConfig.toJsonObject());
}

/**
* Get JSON which is used for <em>creating</em> a container
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.fabric8.maven.docker.access;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

public class ContainerNetworkingConfig {

final JSONObject startConfig = new JSONObject();

public ContainerNetworkingConfig() {}

public ContainerNetworkingConfig endpointsConfig(Map<String, ContainerNetworkingEndpointsConfig> endpointsConfig) {
final JSONObject endpointConfigMap = new JSONObject();
for (Map.Entry<String, ContainerNetworkingEndpointsConfig> entry : endpointsConfig.entrySet()) {
endpointConfigMap.put(entry.getKey(), entry.getValue().toJsonObject());
}
return add("EndpointsConfig", endpointConfigMap);
}

/**
* Get JSON which is used for <em>starting</em> a container
*
* @return string representation for JSON representing the configuration for starting a container
*/
public String toJson() {
return startConfig.toString();
}

public Object toJsonObject() {
return startConfig;
}

ContainerNetworkingConfig addAsArray(String propKey, List<String> props) {
if (props != null) {
startConfig.put(propKey, new JSONArray(props));
}
return this;
}

private ContainerNetworkingConfig add(String name, Object value) {
if (value != null) {
startConfig.put(name, value);
}
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.fabric8.maven.docker.access;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class ContainerNetworkingEndpointsConfig {

final JSONObject startConfig = new JSONObject();

public ContainerNetworkingEndpointsConfig() {}

public ContainerNetworkingEndpointsConfig aliases(List<String> aliases) {
return addAsArray("Aliases", aliases);
}


/**
* Get JSON which is used for <em>starting</em> a container
*
* @return string representation for JSON representing the configuration for starting a container
*/
public String toJson() {
return startConfig.toString();
}

public Object toJsonObject() {
return startConfig;
}

ContainerNetworkingEndpointsConfig addAsArray(String propKey, List<String> props) {
if (props != null) {
startConfig.put(propKey, new JSONArray(props));
}
return this;
}

private ContainerNetworkingEndpointsConfig add(String name, Object value) {
if (value != null) {
startConfig.put(name, value);
}
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class RunImageConfiguration {
@Parameter
private String net;

@Parameter
private List<String> networkAlias;

@Parameter
private List<String> dns;

Expand Down Expand Up @@ -220,6 +223,10 @@ public NetworkingMode getNetworkingMode() {
return new NetworkingMode(net);
}

public List<String> getNetworkAlias() {
return networkAlias;
}

public List<String> getDnsSearch() {
return dnsSearch;
}
Expand Down Expand Up @@ -363,6 +370,11 @@ public Builder net(String net) {
return this;
}

public Builder netAlias(List<String> netAlias) {
config.networkAlias = netAlias;
return this;
}

public Builder dns(List<String> dns) {
config.dns = dns;
return this;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/fabric8/maven/docker/service/RunService.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ ContainerCreateConfig createContainerConfig(String imageName, RunImageConfigurat
if (volumeConfig != null) {
config.binds(volumeConfig.getBind());
}

if(runConfig.getNetworkingMode().isCustomNetwork() && runConfig.getNetworkAlias() != null && !runConfig.getNetworkAlias().isEmpty()) {
config.networkingConfig(new ContainerNetworkingConfig().endpointsConfig(
Collections.singletonMap(
runConfig.getNetworkingMode().getCustomNetwork(),
new ContainerNetworkingEndpointsConfig()
.aliases(runConfig.getNetworkAlias()))));
}

return config;
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Failed to create contained configuration for [%s]", imageName), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ private void givenARunConfiguration() {
.capAdd(capAdd())
.capDrop(capDrop())
.restartPolicy(restartPolicy())
.net("custom_network")
.netAlias(Collections.singletonList("net-alias"))
.build();
}

Expand All @@ -275,6 +277,7 @@ private void thenContainerConfigIsValid() throws IOException {

private void thenStartConfigIsValid() throws IOException {
String expectedHostConfig = loadFile("docker/containerHostConfigAll.json");
System.out.println(startConfig.toJson());
JSONAssert.assertEquals(expectedHostConfig, startConfig.toJson(), true);
}

Expand Down
10 changes: 9 additions & 1 deletion src/test/resources/docker/containerCreateConfigAll.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"CapAdd": ["NET_ADMIN"],
"Memory": 1,
"MemorySwap": 1,
"NetworkMode": "custom_network",
"Binds": ["/host_tmp:/container_tmp"],
"ExtraHosts": ["localhost:127.0.0.1"],
"VolumesFrom": [
Expand All @@ -42,7 +43,14 @@
]
}
},
"NetworkingConfig": {
"EndpointsConfig": {
"custom_network": {
"Aliases": ["net-alias"]
}
}
},
"WorkingDir": "/foo",
"Hostname": "hostname",
"Volumes": {"/container_tmp": {}}
}
}
3 changes: 2 additions & 1 deletion src/test/resources/docker/containerHostConfigAll.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"CapDrop": ["MKNOD"],
"Memory": 1,
"MemorySwap": 1,
"NetworkMode": "custom_network",
"RestartPolicy": { "Name": "on-failure", "MaximumRetryCount": 1 },
"Ulimits":[
{
Expand All @@ -20,4 +21,4 @@
"Soft":2048
}
]
}
}

0 comments on commit 6471c19

Please sign in to comment.