Browse code

Fix handling of the resolv.conf

Leverage what is it passed from the daemon
Fix check about the host networking

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>

Flavio Crisciani authored on 2018/07/17 09:35:03
Showing 5 changed files
... ...
@@ -658,6 +658,7 @@ func TestResolvConfHost(t *testing.T) {
658 658
 	defer os.Remove(resolvConfPath)
659 659
 
660 660
 	sb, err := controller.NewSandbox(containerID,
661
+		libnetwork.OptionUseDefaultSandbox(),
661 662
 		libnetwork.OptionResolvConfPath(resolvConfPath),
662 663
 		libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
663 664
 	if err != nil {
... ...
@@ -2,7 +2,7 @@ package kernel
2 2
 
3 3
 type conditionalCheck func(val1, val2 string) bool
4 4
 
5
-// OSValue represents a tuple, value defired, check function when to apply the value
5
+// OSValue represents a tuple, value defined, check function when to apply the value
6 6
 type OSValue struct {
7 7
 	Value   string
8 8
 	CheckFn conditionalCheck
... ...
@@ -14,6 +14,11 @@ import (
14 14
 	"github.com/sirupsen/logrus"
15 15
 )
16 16
 
17
+const (
18
+	// DefaultResolvConf points to the default file used for dns configuration on a linux machine
19
+	DefaultResolvConf = "/etc/resolv.conf"
20
+)
21
+
17 22
 var (
18 23
 	// Note: the default IPv4 & IPv6 resolvers are set to Google's Public DNS
19 24
 	defaultIPv4Dns = []string{"nameserver 8.8.8.8", "nameserver 8.8.4.4"}
... ...
@@ -50,15 +55,7 @@ type File struct {
50 50
 
51 51
 // Get returns the contents of /etc/resolv.conf and its hash
52 52
 func Get() (*File, error) {
53
-	resolv, err := ioutil.ReadFile("/etc/resolv.conf")
54
-	if err != nil {
55
-		return nil, err
56
-	}
57
-	hash, err := ioutils.HashData(bytes.NewReader(resolv))
58
-	if err != nil {
59
-		return nil, err
60
-	}
61
-	return &File{Content: resolv, Hash: hash}, nil
53
+	return GetSpecific(DefaultResolvConf)
62 54
 }
63 55
 
64 56
 // GetSpecific returns the contents of the user specified resolv.conf file and its hash
... ...
@@ -1098,8 +1098,8 @@ func OptionDNSOptions(options string) SandboxOption {
1098 1098
 	}
1099 1099
 }
1100 1100
 
1101
-// OptionUseDefaultSandbox function returns an option setter for using default sandbox to
1102
-// be passed to container Create method.
1101
+// OptionUseDefaultSandbox function returns an option setter for using default sandbox
1102
+// (host namespace) to be passed to container Create method.
1103 1103
 func OptionUseDefaultSandbox() SandboxOption {
1104 1104
 	return func(sb *sandbox) {
1105 1105
 		sb.config.useDefaultSandBox = true
... ...
@@ -81,7 +81,9 @@ func (sb *sandbox) buildHostsFile() error {
81 81
 	}
82 82
 
83 83
 	// This is for the host mode networking
84
-	if sb.config.originHostsPath != "" {
84
+	if sb.config.useDefaultSandBox && len(sb.config.extraHosts) == 0 {
85
+		// We are working under the assumption that the origin file option had been properly expressed by the upper layer
86
+		// if not here we are going to error out
85 87
 		if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) {
86 88
 			return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err)
87 89
 		}
... ...
@@ -190,8 +192,13 @@ func (sb *sandbox) setupDNS() error {
190 190
 		return err
191 191
 	}
192 192
 
193
-	// This is for the host mode networking
194
-	if sb.config.originResolvConfPath != "" {
193
+	// When the user specify a conainter in the host namespace and do no have any dns option specified
194
+	// we just copy the host resolv.conf from the host itself
195
+	if sb.config.useDefaultSandBox &&
196
+		len(sb.config.dnsList) == 0 && len(sb.config.dnsSearchList) == 0 && len(sb.config.dnsOptionsList) == 0 {
197
+
198
+		// We are working under the assumption that the origin file option had been properly expressed by the upper layer
199
+		// if not here we are going to error out
195 200
 		if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil {
196 201
 			if !os.IsNotExist(err) {
197 202
 				return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err)
... ...
@@ -204,7 +211,12 @@ func (sb *sandbox) setupDNS() error {
204 204
 		return nil
205 205
 	}
206 206
 
207
-	currRC, err := resolvconf.Get()
207
+	originResolvConfPath := sb.config.originResolvConfPath
208
+	if originResolvConfPath == "" {
209
+		// if not specified fallback to default /etc/resolv.conf
210
+		originResolvConfPath = resolvconf.DefaultResolvConf
211
+	}
212
+	currRC, err := resolvconf.GetSpecific(originResolvConfPath)
208 213
 	if err != nil {
209 214
 		if !os.IsNotExist(err) {
210 215
 			return err
... ...
@@ -271,7 +283,7 @@ func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
271 271
 	)
272 272
 
273 273
 	// This is for the host mode networking
274
-	if sb.config.originResolvConfPath != "" {
274
+	if sb.config.useDefaultSandBox {
275 275
 		return nil
276 276
 	}
277 277