Browse code

Replace utils.CheckLocalDns with bytes.Contains line

Since RemoveLocalDns patch will remove all localhost entries
from resolv.conf we no longer need anything more then
!bytes.Contains(resolvConf, []byte("nameserver")

To check for no nameserver entry in dns config.

Docker-DCO-1.1-Signed-off-by: Dan Walsh <dwalsh@redhat.com> (github: rhatdan)

Dan Walsh authored on 2014/09/25 03:19:55
Showing 4 changed files
... ...
@@ -1,6 +1,7 @@
1 1
 package daemon
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"fmt"
5 6
 	"io"
6 7
 	"io/ioutil"
... ...
@@ -1045,8 +1046,9 @@ func (daemon *Daemon) checkLocaldns() error {
1045 1045
 		return err
1046 1046
 	}
1047 1047
 	resolvConf = utils.RemoveLocalDns(resolvConf)
1048
-	if len(daemon.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
1049
-		log.Infof("Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
1048
+
1049
+	if len(daemon.config.Dns) == 0 && !bytes.Contains(resolvConf, []byte("nameserver")) {
1050
+		log.Infof("No non localhost DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
1050 1051
 		daemon.config.Dns = DefaultDns
1051 1052
 	}
1052 1053
 	return nil
... ...
@@ -101,8 +101,6 @@ something like this
101 101
     --- PASS: TestParseRepositoryTag (0.00 seconds)
102 102
     === RUN TestGetResolvConf
103 103
     --- PASS: TestGetResolvConf (0.00 seconds)
104
-    === RUN TestCheckLocalDns
105
-    --- PASS: TestCheckLocalDns (0.00 seconds)
106 104
     === RUN TestParseRelease
107 105
     --- PASS: TestParseRelease (0.00 seconds)
108 106
     === RUN TestDependencyGraphCircular
... ...
@@ -313,26 +313,6 @@ func IsGIT(str string) bool {
313 313
 	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@github.com:") || (strings.HasSuffix(str, ".git") && IsURL(str))
314 314
 }
315 315
 
316
-// CheckLocalDns looks into the /etc/resolv.conf,
317
-// it returns true if there is a local nameserver or if there is no nameserver.
318
-func CheckLocalDns(resolvConf []byte) bool {
319
-	for _, line := range GetLines(resolvConf, []byte("#")) {
320
-		if !bytes.Contains(line, []byte("nameserver")) {
321
-			continue
322
-		}
323
-		for _, ip := range [][]byte{
324
-			[]byte("127.0.0.1"),
325
-			[]byte("127.0.1.1"),
326
-		} {
327
-			if bytes.Contains(line, ip) {
328
-				return true
329
-			}
330
-		}
331
-		return false
332
-	}
333
-	return true
334
-}
335
-
336 316
 var (
337 317
 	localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
338 318
 )
... ...
@@ -343,21 +323,6 @@ func RemoveLocalDns(resolvConf []byte) []byte {
343 343
 	return localHostRx.ReplaceAll(resolvConf, []byte{})
344 344
 }
345 345
 
346
-// GetLines parses input into lines and strips away comments.
347
-func GetLines(input []byte, commentMarker []byte) [][]byte {
348
-	lines := bytes.Split(input, []byte("\n"))
349
-	var output [][]byte
350
-	for _, currentLine := range lines {
351
-		var commentIndex = bytes.Index(currentLine, commentMarker)
352
-		if commentIndex == -1 {
353
-			output = append(output, currentLine)
354
-		} else {
355
-			output = append(output, currentLine[:commentIndex])
356
-		}
357
-	}
358
-	return output
359
-}
360
-
361 346
 // An StatusError reports an unsuccessful exit by a command.
362 347
 type StatusError struct {
363 348
 	Status     string
... ...
@@ -5,35 +5,6 @@ import (
5 5
 	"testing"
6 6
 )
7 7
 
8
-func TestCheckLocalDns(t *testing.T) {
9
-	for resolv, result := range map[string]bool{`# Dynamic
10
-nameserver 10.0.2.3
11
-search docker.com`: false,
12
-		`# Dynamic
13
-#nameserver 127.0.0.1
14
-nameserver 10.0.2.3
15
-search docker.com`: false,
16
-		`# Dynamic
17
-nameserver 10.0.2.3 #not used 127.0.1.1
18
-search docker.com`: false,
19
-		`# Dynamic
20
-#nameserver 10.0.2.3
21
-#search docker.com`: true,
22
-		`# Dynamic
23
-nameserver 127.0.0.1
24
-search docker.com`: true,
25
-		`# Dynamic
26
-nameserver 127.0.1.1
27
-search docker.com`: true,
28
-		`# Dynamic
29
-`: true,
30
-		``: true,
31
-	} {
32
-		if CheckLocalDns([]byte(resolv)) != result {
33
-			t.Fatalf("Wrong local dns detection: {%s} should be %v", resolv, result)
34
-		}
35
-	}
36
-}
37 8
 func TestReplaceAndAppendEnvVars(t *testing.T) {
38 9
 	var (
39 10
 		d = []string{"HOME=/"}