Skip to content

Commit

Permalink
address comments from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Jun 16, 2021
1 parent 23b0326 commit aeca42e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions client/allocrunner/network_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 6 additions & 5 deletions drivers/shared/hostnames/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit aeca42e

Please sign in to comment.