-
Notifications
You must be signed in to change notification settings - Fork 2k
/
mount.go
83 lines (71 loc) · 1.66 KB
/
mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package resolvconf
import (
"io"
"os"
"path/filepath"
dresolvconf "github.com/docker/libnetwork/resolvconf"
"github.com/docker/libnetwork/types"
"github.com/hashicorp/nomad/plugins/drivers"
)
func GenerateDNSMount(taskDir string, conf *drivers.DNSConfig) (*drivers.MountConfig, error) {
var nSearches, nServers, nOptions int
path := filepath.Join(taskDir, "resolv.conf")
mount := &drivers.MountConfig{
TaskPath: "/etc/resolv.conf",
HostPath: path,
Readonly: true,
PropagationMode: "private",
}
if conf != nil {
nServers = len(conf.Servers)
nSearches = len(conf.Searches)
nOptions = len(conf.Options)
}
// Use system dns if no configuration is given
if nServers == 0 && nSearches == 0 && nOptions == 0 {
if err := copySystemDNS(path); err != nil {
return nil, err
}
return mount, nil
}
currRC, err := dresolvconf.Get()
if err != nil {
return nil, err
}
var (
dnsList = dresolvconf.GetNameservers(currRC.Content, types.IP)
dnsSearchList = dresolvconf.GetSearchDomains(currRC.Content)
dnsOptionsList = dresolvconf.GetOptions(currRC.Content)
)
if nServers > 0 {
dnsList = conf.Servers
}
if nSearches > 0 {
dnsSearchList = conf.Searches
}
if nOptions > 0 {
dnsOptionsList = conf.Options
}
_, err = dresolvconf.Build(path, dnsList, dnsSearchList, dnsOptionsList)
if err != nil {
return nil, err
}
return mount, nil
}
func copySystemDNS(dest string) error {
in, err := os.Open(dresolvconf.Path())
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
defer func() {
out.Sync()
out.Close()
}()
_, err = io.Copy(out, in)
return err
}