@@ -31,6 +31,8 @@ import (
31
31
refdocker "github.com/containerd/containerd/reference/docker"
32
32
remotesdocker "github.com/containerd/containerd/remotes/docker"
33
33
"github.com/docker/go-units"
34
+ "github.com/hashicorp/nomad/drivers/shared/hostnames"
35
+ "github.com/hashicorp/nomad/plugins/drivers"
34
36
specs "github.com/opencontainers/runtime-spec/specs-go"
35
37
)
36
38
@@ -114,7 +116,7 @@ func (d *Driver) pullImage(imageName, imagePullTimeout string, auth *RegistryAut
114
116
return d .client .Pull (ctxWithTimeout , named .String (), pullOpts ... )
115
117
}
116
118
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 ) {
118
120
if config .Command != "" && config .Entrypoint != nil {
119
121
return nil , fmt .Errorf ("Both command and entrypoint are set. Only one of them needs to be set." )
120
122
}
@@ -198,13 +200,6 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
198
200
opts = append (opts , oci .WithRootFSReadonly ())
199
201
}
200
202
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
-
208
203
// Add capabilities.
209
204
if len (config .CapAdd ) > 0 {
210
205
opts = append (opts , oci .WithAddedCapabilities (config .CapAdd ))
@@ -278,33 +273,37 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
278
273
mounts = append (mounts , allocMount )
279
274
}
280
275
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
286
277
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
296
282
}
297
283
if err := etchosts .AddExtraHosts (hostsFile , config .ExtraHosts ); err != nil {
298
284
return nil , err
299
285
}
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 {
303
299
if err := etchosts .BuildEtcHosts (hostsFile ); err != nil {
304
300
return nil , err
305
301
}
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 )
308
307
}
309
308
310
309
if len (mounts ) > 0 {
0 commit comments