Merge pull request #443 from hashicorp/f-docker-dns-config

Change docker DNS configuration to lists
This commit is contained in:
Chris Bednarski 2015-11-18 10:31:27 -08:00
commit b3be339e19
3 changed files with 73 additions and 29 deletions

View file

@ -33,17 +33,17 @@ type DockerDriverAuth struct {
}
type DockerDriverConfig struct {
ImageName string `mapstructure:"image"` // Container's Image Name
Command string `mapstructure:"command"` // The Command/Entrypoint to run when the container starts up
Args string `mapstructure:"args"` // The arguments to the Command/Entrypoint
NetworkMode string `mapstructure:"network_mode"` // The network mode of the container - host, net and none
PortMap []map[string]int `mapstructure:"port_map"` // A map of host port labels and the ports exposed on the container
Privileged bool `mapstructure:"privileged"` // Flag to run the container in priviledged mode
DNS string `mapstructure:"dns_server"` // DNS Server for containers
SearchDomains string `mapstructure:"search_domains"` // DNS Search domains for containers
Hostname string `mapstructure:"hostname"` // Hostname for containers
Labels []map[string]string `mapstructure:"labels"` // Labels to set when the container starts up
Auth []DockerDriverAuth `mapstructure:"auth"` // Authentication credentials for a private Docker registry
ImageName string `mapstructure:"image"` // Container's Image Name
Command string `mapstructure:"command"` // The Command/Entrypoint to run when the container starts up
Args string `mapstructure:"args"` // The arguments to the Command/Entrypoint
NetworkMode string `mapstructure:"network_mode"` // The network mode of the container - host, net and none
PortMap []map[string]int `mapstructure:"port_map"` // A map of host port labels and the ports exposed on the container
Privileged bool `mapstructure:"privileged"` // Flag to run the container in priviledged mode
DNSServers []string `mapstructure:"dns_servers"` // DNS Server for containers
DNSSearchDomains []string `mapstructure:"dns_search_domains"` // DNS Search domains for containers
Hostname string `mapstructure:"hostname"` // Hostname for containers
Labels []map[string]string `mapstructure:"labels"` // Labels to set when the container starts up
Auth []DockerDriverAuth `mapstructure:"auth"` // Authentication credentials for a private Docker registry
}
func (c *DockerDriverConfig) Validate() error {
@ -216,22 +216,17 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
hostConfig.Privileged = hostPrivileged
// set DNS servers
if driverConfig.DNS != "" {
for _, v := range strings.Split(driverConfig.DNS, ",") {
ip := strings.TrimSpace(v)
if net.ParseIP(ip) != nil {
hostConfig.DNS = append(hostConfig.DNS, ip)
} else {
d.logger.Printf("[ERR] driver.docker: invalid ip address for container dns server: %s", ip)
}
for _, ip := range driverConfig.DNSServers {
if net.ParseIP(ip) != nil {
hostConfig.DNS = append(hostConfig.DNS, ip)
} else {
d.logger.Printf("[ERR] driver.docker: invalid ip address for container dns server: %s", ip)
}
}
// set DNS search domains
if driverConfig.SearchDomains != "" {
for _, v := range strings.Split(driverConfig.SearchDomains, ",") {
hostConfig.DNSSearch = append(hostConfig.DNSSearch, strings.TrimSpace(v))
}
for _, domain := range driverConfig.DNSSearchDomains {
hostConfig.DNSSearch = append(hostConfig.DNSSearch, domain)
}
hostConfig.NetworkMode = driverConfig.NetworkMode

View file

@ -467,6 +467,7 @@ func TestDockerLabels(t *testing.T) {
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
client, err := docker.NewClientFromEnv()
if err != nil {
@ -493,6 +494,54 @@ func TestDockerLabels(t *testing.T) {
if want, got := "value1", container.Config.Labels["label1"]; want != got {
t.Errorf("Wrong label value docker job. Expect: %s, got: %s", want, got)
}
defer handle.Kill()
}
func TestDockerDNS(t *testing.T) {
if !dockerIsConnected(t) {
t.SkipNow()
}
task := taskTemplate()
task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"}
task.Config["dns_search_domains"] = []string{"example.com", "example.org", "example.net"}
driverCtx := testDockerDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)
handle, err := d.Start(ctx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
client, err := docker.NewClientFromEnv()
if err != nil {
t.Fatalf("err: %v", err)
}
// don't know if is queriable in a clean way
parts := strings.SplitN(handle.ID(), ":", 2)
var pid dockerPID
err = json.Unmarshal([]byte(parts[1]), &pid)
if err != nil {
t.Fatalf("err: %v", err)
}
container, err := client.InspectContainer(pid.ContainerID)
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(task.Config["dns_servers"], container.HostConfig.DNS) {
t.Errorf("DNS Servers don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["dns_servers"], container.HostConfig.DNS)
}
if !reflect.DeepEqual(task.Config["dns_search_domains"], container.HostConfig.DNSSearch) {
t.Errorf("DNS Servers don't match.\nExpected:\n%s\nGot:\n%s\n", task.Config["dns_search_domains"], container.HostConfig.DNSSearch)
}
}

View file

@ -39,11 +39,11 @@ specification:
Tasks with `privileged` set can only run on Nomad Agents with
`docker.privileged.enabled = "true"`.
* `dns_servers` - (Optional) A comma separated list of DNS servers for the
container to use (e.g. "8.8.8.8,8.8.4.4"). *Docker API v1.10 and above only*
* `dns_servers` - (Optional) A list of DNS servers for the container to use
(e.g. ["8.8.8.8", "8.8.4.4"]). *Docker API v1.10 and above only*
* `search_domains` - (Optional) A comma separated list of DNS search domains
for the container to use.
* `search_domains` - (Optional) A list of DNS search domains for the container
to use.
* `hostname` - (Optional) The hostname to assign to the container. When
launching more than one of a task (using `count`) with this option set, every