From aeca42e628d5ec00adf849597153b027b873f7d1 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Wed, 16 Jun 2021 11:04:14 -0400 Subject: [PATCH] address comments from code review --- CHANGELOG.md | 2 +- client/allocrunner/network_hook.go | 6 ++++++ drivers/shared/hostnames/mount.go | 11 ++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18a377949e04..202c219eab92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ IMPROVEMENTS: * cli: Added `-monitor` flag to `deployment status` command and automatically monitor deployments from `job run` command. [[GH-10661](https://github.com/hashicorp/nomad/pull/10661)] -* docker: Tasks using `network.mode = "bridge"` that don't set their `network_mode` will receive a `/etc/hosts` file that includes the pause container's hostname. [[GH-10766](https://github.com/hashicorp/nomad/issues/10766)] +* docker: Tasks using `network.mode = "bridge"` that don't set their `network_mode` will receive a `/etc/hosts` file that includes the pause container's hostname and any `extra_hosts`. [[GH-10766](https://github.com/hashicorp/nomad/issues/10766)] BUG FIXES: * quotas (Enterprise): Fixed a bug where quotas were evaluated before constraints, resulting in quota capacity being used up by filtered nodes. [[GH-10753](https://github.com/hashicorp/nomad/issues/10753)] diff --git a/client/allocrunner/network_hook.go b/client/allocrunner/network_hook.go index 7a901bfcf87c..70f82111c6d8 100644 --- a/client/allocrunner/network_hook.go +++ b/client/allocrunner/network_hook.go @@ -9,6 +9,9 @@ import ( "github.com/hashicorp/nomad/plugins/drivers" ) +// We create a pause container to own the network namespace, and the +// NetworkIsolationSpec we get back from CreateNetwork has this label set as +// the container ID. We'll use this to generate a hostname for the task. const dockerNetSpecLabelKey = "docker_sandbox_container_id" type networkIsolationSetter interface { @@ -110,6 +113,9 @@ func (h *networkHook) Prerun() error { } if hostname, ok := spec.Labels[dockerNetSpecLabelKey]; ok { if len(hostname) > 12 { + // the docker_sandbox_container_id is the full ID of the pause + // container, whereas we want the shortened name that dockerd + // sets as the pause container's hostname hostname = hostname[:12] } h.spec.HostsConfig = &drivers.HostsConfig{ diff --git a/drivers/shared/hostnames/mount.go b/drivers/shared/hostnames/mount.go index 358efc7b9e1a..fcf65176c3bd 100644 --- a/drivers/shared/hostnames/mount.go +++ b/drivers/shared/hostnames/mount.go @@ -24,7 +24,8 @@ func GenerateEtcHostsMount(taskDir string, conf *drivers.NetworkIsolationSpec, e return nil, nil } - content := fmt.Sprintf(`# this file was generated by Nomad + var content strings.Builder + fmt.Fprintf(&content, `# this file was generated by Nomad 127.0.0.1 localhost ::1 localhost ::1 ip6-localhost ip6-loopback @@ -40,7 +41,7 @@ ff02::3 ip6-allhosts `, hostsCfg.Address, hostsCfg.Hostname) if len(extraHosts) > 0 { - content += "\n# these entries are extra hosts added by the task config" + content.WriteString("\n# these entries are extra hosts added by the task config") for _, hostLine := range extraHosts { hostsEntry := strings.SplitN(hostLine, ":", 2) if len(hostsEntry) != 2 { @@ -49,13 +50,13 @@ ff02::3 ip6-allhosts if net.ParseIP(hostsEntry[1]) == nil { return nil, fmt.Errorf("invalid IP address %q", hostLine) } - content += fmt.Sprintf("\n%s %s", hostsEntry[1], hostsEntry[0]) + content.WriteString(fmt.Sprintf("\n%s %s", hostsEntry[1], hostsEntry[0])) } - content += "\n" + content.WriteString("\n") } path := filepath.Join(taskDir, "hosts") - err := ioutil.WriteFile(path, []byte(content), 0755) + err := ioutil.WriteFile(path, []byte(content.String()), 0755) if err != nil { return nil, err }