Skip to content

Commit 298308d

Browse files
Fix: hostname not populated for /etc/hosts in bridge networks.
Signed-off-by: Shishir Mahajan <[email protected]>
1 parent 1f26157 commit 298308d

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

containerd/containerd.go

+26-27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
refdocker "github.com/containerd/containerd/reference/docker"
3232
remotesdocker "github.com/containerd/containerd/remotes/docker"
3333
"github.com/docker/go-units"
34+
"github.com/hashicorp/nomad/drivers/shared/hostnames"
35+
"github.com/hashicorp/nomad/plugins/drivers"
3436
specs "github.com/opencontainers/runtime-spec/specs-go"
3537
)
3638

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

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

201-
// Enable host network.
202-
// WithHostHostsFile bind-mounts the host's /etc/hosts into the container as readonly.
203-
// WithHostResolvconf bind-mounts the host's /etc/resolv.conf into the container as readonly.
204-
if config.HostNetwork {
205-
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
206-
}
207-
208203
// Add capabilities.
209204
if len(config.CapAdd) > 0 {
210205
opts = append(opts, oci.WithAddedCapabilities(config.CapAdd))
@@ -278,33 +273,37 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
278273
mounts = append(mounts, allocMount)
279274
}
280275

281-
// User will specify extra_hosts to be added to container's /etc/hosts.
282-
// If host_network=true, extra_hosts will be added to host's /etc/hosts.
283-
// If host_network=false, extra hosts will be added to the default /etc/hosts provided to the container.
284-
// If the user doesn't set anything (host_network, extra_hosts), a default /etc/hosts will be provided to the container.
285-
var extraHostsMount specs.Mount
276+
var etcHostMount specs.Mount
286277
hostsFile := containerConfig.TaskDirSrc + "/etc_hosts"
287-
if len(config.ExtraHosts) > 0 {
288-
if config.HostNetwork {
289-
if err := etchosts.CopyEtcHosts(hostsFile); err != nil {
290-
return nil, err
291-
}
292-
} else {
293-
if err := etchosts.BuildEtcHosts(hostsFile); err != nil {
294-
return nil, err
295-
}
278+
if config.HostNetwork {
279+
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
280+
if err := etchosts.CopyEtcHosts(hostsFile); err != nil {
281+
return nil, err
296282
}
297283
if err := etchosts.AddExtraHosts(hostsFile, config.ExtraHosts); err != nil {
298284
return nil, err
299285
}
300-
extraHostsMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
301-
mounts = append(mounts, extraHostsMount)
302-
} else if !config.HostNetwork {
286+
etcHostMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
287+
mounts = append(mounts, etcHostMount)
288+
} else if cfg.NetworkIsolation != nil {
289+
mountInfo, err := hostnames.GenerateEtcHostsMount(
290+
cfg.TaskDir().Dir, cfg.NetworkIsolation, config.ExtraHosts)
291+
if err != nil {
292+
return nil, fmt.Errorf("failed to build mount for /etc/hosts: %v", err)
293+
}
294+
if mountInfo != nil {
295+
etcHostMount = buildMountpoint("bind", mountInfo.TaskPath, mountInfo.HostPath, []string{"rbind", "rw"})
296+
mounts = append(mounts, etcHostMount)
297+
}
298+
} else {
303299
if err := etchosts.BuildEtcHosts(hostsFile); err != nil {
304300
return nil, err
305301
}
306-
extraHostsMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
307-
mounts = append(mounts, extraHostsMount)
302+
if err := etchosts.AddExtraHosts(hostsFile, config.ExtraHosts); err != nil {
303+
return nil, err
304+
}
305+
etcHostMount = buildMountpoint("bind", "/etc/hosts", hostsFile, []string{"rbind", "rw"})
306+
mounts = append(mounts, etcHostMount)
308307
}
309308

310309
if len(mounts) > 0 {

containerd/driver.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
469469
containerConfig.MemoryHardLimit = cfg.Resources.NomadResources.Memory.MemoryMaxMB * 1024 * 1024
470470
containerConfig.CPUShares = cfg.Resources.LinuxResources.CPUShares
471471

472-
container, err := d.createContainer(&containerConfig, &driverConfig)
472+
container, err := d.createContainer(&containerConfig, &driverConfig, cfg)
473473
if err != nil {
474474
return nil, nil, fmt.Errorf("Error in creating container: %v", err)
475475
}

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,6 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
676676
github.com/hashicorp/net-rpc-msgpackrpc v0.0.0-20151116020338-a14192a58a69/go.mod h1:/z+jUGRBlwVpUZfjute9jWaF6/HuhjuFQuL1YXzVD1Q=
677677
github.com/hashicorp/nomad v1.1.4 h1:ZhxrzLJhGzJq9EEG7XFlzhlHviqij1rEzX1Nd5lj3Lk=
678678
github.com/hashicorp/nomad v1.1.4/go.mod h1:zb5FH723Po1AP4letahIJCeoEq+2LvIgmY21W3kXz4g=
679-
github.com/hashicorp/nomad/api v0.0.0-20200529203653-c4416b26d3eb h1:gFssj9eV5on4ZYpwTQl+LTrkebu+qCxuKpISPcMCH88=
680679
github.com/hashicorp/nomad/api v0.0.0-20200529203653-c4416b26d3eb/go.mod h1:DCi2k47yuUDzf2qWAK8E1RVmWgz/lc0jZQeEnICTxmY=
681680
github.com/hashicorp/raft v1.1.1/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8=
682681
github.com/hashicorp/raft v1.1.2/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8=

0 commit comments

Comments
 (0)