Skip to content

Commit

Permalink
Fix: hostname not populated for /etc/hosts in bridge networks.
Browse files Browse the repository at this point in the history
Signed-off-by: Shishir Mahajan <smahajan@roblox.com>
  • Loading branch information
shishir-a412ed committed Sep 13, 2021
1 parent 1f26157 commit 298308d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
53 changes: 26 additions & 27 deletions containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
refdocker "github.com/containerd/containerd/reference/docker"
remotesdocker "github.com/containerd/containerd/remotes/docker"
"github.com/docker/go-units"
"github.com/hashicorp/nomad/drivers/shared/hostnames"
"github.com/hashicorp/nomad/plugins/drivers"
specs "github.com/opencontainers/runtime-spec/specs-go"
)

Expand Down Expand Up @@ -114,7 +116,7 @@ func (d *Driver) pullImage(imageName, imagePullTimeout string, auth *RegistryAut
return d.client.Pull(ctxWithTimeout, named.String(), pullOpts...)
}

func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskConfig) (containerd.Container, error) {
func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskConfig, cfg *drivers.TaskConfig) (containerd.Container, error) {
if config.Command != "" && config.Entrypoint != nil {
return nil, fmt.Errorf("Both command and entrypoint are set. Only one of them needs to be set.")
}
Expand Down Expand Up @@ -198,13 +200,6 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
opts = append(opts, oci.WithRootFSReadonly())
}

// Enable host network.
// WithHostHostsFile bind-mounts the host's /etc/hosts into the container as readonly.
// WithHostResolvconf bind-mounts the host's /etc/resolv.conf into the container as readonly.
if config.HostNetwork {
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
}

// Add capabilities.
if len(config.CapAdd) > 0 {
opts = append(opts, oci.WithAddedCapabilities(config.CapAdd))
Expand Down Expand Up @@ -278,33 +273,37 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
mounts = append(mounts, allocMount)
}

// User will specify extra_hosts to be added to container's /etc/hosts.
// If host_network=true, extra_hosts will be added to host's /etc/hosts.
// If host_network=false, extra hosts will be added to the default /etc/hosts provided to the container.
// If the user doesn't set anything (host_network, extra_hosts), a default /etc/hosts will be provided to the container.
var extraHostsMount specs.Mount
var etcHostMount specs.Mount
hostsFile := containerConfig.TaskDirSrc + "/etc_hosts"
if len(config.ExtraHosts) > 0 {
if config.HostNetwork {
if err := etchosts.CopyEtcHosts(hostsFile); err != nil {
return nil, err
}
} else {
if err := etchosts.BuildEtcHosts(hostsFile); err != nil {
return nil, err
}
if config.HostNetwork {
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
if err := etchosts.CopyEtcHosts(hostsFile); err != nil {
return nil, err
}
if err := etchosts.AddExtraHosts(hostsFile, config.ExtraHosts); err != nil {
return nil, err
}
extraHostsMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
mounts = append(mounts, extraHostsMount)
} else if !config.HostNetwork {
etcHostMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
mounts = append(mounts, etcHostMount)
} else if cfg.NetworkIsolation != nil {
mountInfo, err := hostnames.GenerateEtcHostsMount(
cfg.TaskDir().Dir, cfg.NetworkIsolation, config.ExtraHosts)
if err != nil {
return nil, fmt.Errorf("failed to build mount for /etc/hosts: %v", err)
}
if mountInfo != nil {
etcHostMount = buildMountpoint("bind", mountInfo.TaskPath, mountInfo.HostPath, []string{"rbind", "rw"})
mounts = append(mounts, etcHostMount)
}
} else {
if err := etchosts.BuildEtcHosts(hostsFile); err != nil {
return nil, err
}
extraHostsMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
mounts = append(mounts, extraHostsMount)
if err := etchosts.AddExtraHosts(hostsFile, config.ExtraHosts); err != nil {
return nil, err
}
etcHostMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
mounts = append(mounts, etcHostMount)
}

if len(mounts) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion containerd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
containerConfig.MemoryHardLimit = cfg.Resources.NomadResources.Memory.MemoryMaxMB * 1024 * 1024
containerConfig.CPUShares = cfg.Resources.LinuxResources.CPUShares

container, err := d.createContainer(&containerConfig, &driverConfig)
container, err := d.createContainer(&containerConfig, &driverConfig, cfg)
if err != nil {
return nil, nil, fmt.Errorf("Error in creating container: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
github.com/hashicorp/net-rpc-msgpackrpc v0.0.0-20151116020338-a14192a58a69/go.mod h1:/z+jUGRBlwVpUZfjute9jWaF6/HuhjuFQuL1YXzVD1Q=
github.com/hashicorp/nomad v1.1.4 h1:ZhxrzLJhGzJq9EEG7XFlzhlHviqij1rEzX1Nd5lj3Lk=
github.com/hashicorp/nomad v1.1.4/go.mod h1:zb5FH723Po1AP4letahIJCeoEq+2LvIgmY21W3kXz4g=
github.com/hashicorp/nomad/api v0.0.0-20200529203653-c4416b26d3eb h1:gFssj9eV5on4ZYpwTQl+LTrkebu+qCxuKpISPcMCH88=
github.com/hashicorp/nomad/api v0.0.0-20200529203653-c4416b26d3eb/go.mod h1:DCi2k47yuUDzf2qWAK8E1RVmWgz/lc0jZQeEnICTxmY=
github.com/hashicorp/raft v1.1.1/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8=
github.com/hashicorp/raft v1.1.2/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8=
Expand Down

0 comments on commit 298308d

Please sign in to comment.