Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: hostname not populated for /etc/hosts in bridge networks. #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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