Added ExtraHosts option to template

This commit is contained in:
Andrew Kennedy 2015-09-17 13:06:37 +01:00 committed by Ignasi Barrera
parent 15651822be
commit 3901403379
5 changed files with 57 additions and 11 deletions

View File

@ -59,6 +59,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
protected List<String> env = ImmutableList.of();
protected Map<Integer, Integer> portBindings = ImmutableMap.of();
protected String networkMode;
protected Map<String, String> extraHosts = ImmutableMap.of();
@Override
public DockerTemplateOptions clone() {
@ -91,6 +92,9 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
eTo.portBindings(portBindings);
}
eTo.networkMode(networkMode);
if (!extraHosts.isEmpty()) {
eTo.extraHosts(extraHosts);
}
}
}
@ -108,12 +112,13 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
equal(this.commands, that.commands) &&
equal(this.cpuShares, that.cpuShares) &&
equal(this.env, that.env) &&
equal(this.portBindings, that.portBindings);
equal(this.portBindings, that.portBindings) &&
equal(this.extraHosts, that.extraHosts);
}
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), volumes, hostname, dns, memory, commands, cpuShares, env, portBindings);
return Objects.hashCode(super.hashCode(), volumes, hostname, dns, memory, commands, cpuShares, env, portBindings, extraHosts);
}
@Override
@ -127,6 +132,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
.add("volumes", volumes)
.add("env", env)
.add("portBindings", portBindings)
.add("extraHosts", extraHosts)
.toString();
}
@ -192,7 +198,6 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
return this;
}
/**
* Sets the networking mode for the container. Supported values are: bridge, host, and container:[name|id]
* @param networkMode
@ -203,6 +208,20 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
return this;
}
/**
* Set extra hosts file entries for a container.
* <p>
* The {@link Map} keys are host names, and the value is an IP address that
* can be accessed by the container. This is the same order as the arguments for the
* {@code --add-host} command-line option to {@code docker run}.
*
* @param extraHosts the map of host names to IP addresses
*/
public DockerTemplateOptions extraHosts(Map<String, String> extraHosts) {
this.extraHosts = ImmutableMap.copyOf(checkNotNull(extraHosts, "extraHosts"));
return this;
}
public Map<String, String> getVolumes() { return volumes; }
public List<String> getDns() { return dns; }
@ -221,6 +240,8 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
public String getNetworkMode() { return networkMode; }
public Map<String, String> getExtraHosts() { return extraHosts; }
public static class Builder {
/**
@ -312,13 +333,21 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
}
/**
* @see DockerTemplateOptions#hostname(String)
* @see DockerTemplateOptions#networkMode(String)
*/
public static DockerTemplateOptions networkMode(@Nullable String networkMode) {
DockerTemplateOptions options = new DockerTemplateOptions();
return options.networkMode(networkMode);
}
/**
* @see DockerTemplateOptions#extraHosts(Map)
*/
public static DockerTemplateOptions extraHosts(Map<String, String> extraHosts) {
DockerTemplateOptions options = new DockerTemplateOptions();
return options.extraHosts(extraHosts);
}
/**
* @see TemplateOptions#inboundPorts(int...)
*/

View File

@ -142,6 +142,14 @@ public class DockerComputeServiceAdapter implements
hostConfigBuilder.dns(templateOptions.getDns());
}
if (!templateOptions.getExtraHosts().isEmpty()) {
List<String> extraHosts = Lists.newArrayList();
for (Map.Entry<String, String> entry : templateOptions.getExtraHosts().entrySet()) {
extraHosts.add(entry.getKey() + ":" + entry.getValue());
}
hostConfigBuilder.extraHosts(extraHosts);
}
if (!templateOptions.getVolumes().isEmpty()) {
for (Map.Entry<String, String> entry : templateOptions.getVolumes().entrySet()) {
hostConfigBuilder.binds(ImmutableList.of(entry.getKey() + ":" + entry.getValue()));

View File

@ -47,6 +47,8 @@ public abstract class HostConfig {
public abstract List<String> links();
public abstract List<String> extraHosts();
public abstract boolean publishAllPorts();
public abstract List<String> volumesFrom();
@ -58,12 +60,12 @@ public abstract class HostConfig {
}
@SerializedNames({ "ContainerIDFile", "Binds", "LxcConf", "Privileged", "Dns", "DnsSearch", "PortBindings",
"Links", "PublishAllPorts", "VolumesFrom", "NetworkMode" })
"Links", "ExtraHosts", "PublishAllPorts", "VolumesFrom", "NetworkMode" })
public static HostConfig create(String containerIDFile, List<String> binds, List<Map<String, String>> lxcConf,
boolean privileged, List<String> dns, String dnsSearch, Map<String, List<Map<String, String>>> portBindings,
List<String> links, boolean publishAllPorts, List<String> volumesFrom, String networkMode) {
List<String> links, List<String> extraHosts, boolean publishAllPorts, List<String> volumesFrom, String networkMode) {
return new AutoValue_HostConfig(containerIDFile, copyOf(binds), copyOf(lxcConf), privileged, copyOf(dns), dnsSearch,
copyOf(portBindings), copyOf(links), publishAllPorts, copyOf(volumesFrom), networkMode);
copyOf(portBindings), copyOf(links), copyOf(extraHosts), publishAllPorts, copyOf(volumesFrom), networkMode);
}
public static Builder builder() {
@ -84,6 +86,7 @@ public abstract class HostConfig {
private String dnsSearch;
private Map<String, List<Map<String, String>>> portBindings = Maps.newLinkedHashMap();
private List<String> links = Lists.newArrayList();
private List<String> extraHosts = Lists.newArrayList();
private boolean publishAllPorts;
private List<String> volumesFrom = Lists.newArrayList();
private String networkMode;
@ -123,6 +126,11 @@ public abstract class HostConfig {
return this;
}
public Builder extraHosts(List<String> extraHosts) {
this.extraHosts.addAll(checkNotNull(extraHosts, "extraHosts"));
return this;
}
public Builder portBindings(Map<String, List<Map<String, String>>> portBindings) {
this.portBindings.putAll(portBindings);
return this;
@ -145,14 +153,14 @@ public abstract class HostConfig {
public HostConfig build() {
return HostConfig.create(containerIDFile, binds, lxcConf, privileged, dns, dnsSearch, portBindings, links,
publishAllPorts, volumesFrom, networkMode);
extraHosts, publishAllPorts, volumesFrom, networkMode);
}
public Builder fromHostConfig(HostConfig in) {
return this.containerIDFile(in.containerIDFile()).binds(in.binds()).lxcConf(in.lxcConf())
.privileged(in.privileged()).dns(in.dns()).dnsSearch(in.dnsSearch()).links(in.links())
.portBindings(in.portBindings()).publishAllPorts(in.publishAllPorts()).volumesFrom(in.volumesFrom())
.networkMode(in.networkMode());
.extraHosts(in.extraHosts()).portBindings(in.portBindings()).publishAllPorts(in.publishAllPorts())
.volumesFrom(in.volumesFrom()).networkMode(in.networkMode());
}
}
}

View File

@ -83,6 +83,7 @@ public class ContainerParseTest extends BaseDockerParseTest<Container> {
"6783/udp", ImmutableList.<Map<String, String>>of(ImmutableMap.of("HostIp", "", "HostPort", "6783")))
)
.dns(ImmutableList.of("8.8.8.8", "8.8.4.4"))
.extraHosts(ImmutableList.<String>of("extra:169.254.0.1"))
.privileged(true)
.networkMode("bridge")
.build())

View File

@ -58,7 +58,7 @@
"Devices": [],
"Dns": [ "8.8.8.8", "8.8.4.4" ],
"DnsSearch": null,
"ExtraHosts": null,
"ExtraHosts": [ "extra:169.254.0.1" ],
"Links": null,
"LxcConf": [],
"NetworkMode": "bridge",