@@ -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 {
0 commit comments