浏览代码

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 10 年之前
父节点
当前提交
a297d6ab8c
共有 4 个文件被更改,包括 4 次插入68 次删除
  1. 4 2
      daemon/daemon.go
  2. 0 2
      docs/sources/contributing/devenvironment.md
  3. 0 35
      utils/utils.go
  4. 0 29
      utils/utils_test.go

+ 4 - 2
daemon/daemon.go

@@ -1,6 +1,7 @@
 package daemon
 package daemon
 
 
 import (
 import (
+	"bytes"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
@@ -1045,8 +1046,9 @@ func (daemon *Daemon) checkLocaldns() error {
 		return err
 		return err
 	}
 	}
 	resolvConf = utils.RemoveLocalDns(resolvConf)
 	resolvConf = utils.RemoveLocalDns(resolvConf)
-	if len(daemon.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
-		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)
+
+	if len(daemon.config.Dns) == 0 && !bytes.Contains(resolvConf, []byte("nameserver")) {
+		log.Infof("No non localhost DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
 		daemon.config.Dns = DefaultDns
 		daemon.config.Dns = DefaultDns
 	}
 	}
 	return nil
 	return nil

+ 0 - 2
docs/sources/contributing/devenvironment.md

@@ -101,8 +101,6 @@ something like this
     --- PASS: TestParseRepositoryTag (0.00 seconds)
     --- PASS: TestParseRepositoryTag (0.00 seconds)
     === RUN TestGetResolvConf
     === RUN TestGetResolvConf
     --- PASS: TestGetResolvConf (0.00 seconds)
     --- PASS: TestGetResolvConf (0.00 seconds)
-    === RUN TestCheckLocalDns
-    --- PASS: TestCheckLocalDns (0.00 seconds)
     === RUN TestParseRelease
     === RUN TestParseRelease
     --- PASS: TestParseRelease (0.00 seconds)
     --- PASS: TestParseRelease (0.00 seconds)
     === RUN TestDependencyGraphCircular
     === RUN TestDependencyGraphCircular

+ 0 - 35
utils/utils.go

@@ -313,26 +313,6 @@ func IsGIT(str string) bool {
 	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@github.com:") || (strings.HasSuffix(str, ".git") && IsURL(str))
 	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@github.com:") || (strings.HasSuffix(str, ".git") && IsURL(str))
 }
 }
 
 
-// CheckLocalDns looks into the /etc/resolv.conf,
-// it returns true if there is a local nameserver or if there is no nameserver.
-func CheckLocalDns(resolvConf []byte) bool {
-	for _, line := range GetLines(resolvConf, []byte("#")) {
-		if !bytes.Contains(line, []byte("nameserver")) {
-			continue
-		}
-		for _, ip := range [][]byte{
-			[]byte("127.0.0.1"),
-			[]byte("127.0.1.1"),
-		} {
-			if bytes.Contains(line, ip) {
-				return true
-			}
-		}
-		return false
-	}
-	return true
-}
-
 var (
 var (
 	localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
 	localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
 )
 )
@@ -343,21 +323,6 @@ func RemoveLocalDns(resolvConf []byte) []byte {
 	return localHostRx.ReplaceAll(resolvConf, []byte{})
 	return localHostRx.ReplaceAll(resolvConf, []byte{})
 }
 }
 
 
-// GetLines parses input into lines and strips away comments.
-func GetLines(input []byte, commentMarker []byte) [][]byte {
-	lines := bytes.Split(input, []byte("\n"))
-	var output [][]byte
-	for _, currentLine := range lines {
-		var commentIndex = bytes.Index(currentLine, commentMarker)
-		if commentIndex == -1 {
-			output = append(output, currentLine)
-		} else {
-			output = append(output, currentLine[:commentIndex])
-		}
-	}
-	return output
-}
-
 // An StatusError reports an unsuccessful exit by a command.
 // An StatusError reports an unsuccessful exit by a command.
 type StatusError struct {
 type StatusError struct {
 	Status     string
 	Status     string

+ 0 - 29
utils/utils_test.go

@@ -5,35 +5,6 @@ import (
 	"testing"
 	"testing"
 )
 )
 
 
-func TestCheckLocalDns(t *testing.T) {
-	for resolv, result := range map[string]bool{`# Dynamic
-nameserver 10.0.2.3
-search docker.com`: false,
-		`# Dynamic
-#nameserver 127.0.0.1
-nameserver 10.0.2.3
-search docker.com`: false,
-		`# Dynamic
-nameserver 10.0.2.3 #not used 127.0.1.1
-search docker.com`: false,
-		`# Dynamic
-#nameserver 10.0.2.3
-#search docker.com`: true,
-		`# Dynamic
-nameserver 127.0.0.1
-search docker.com`: true,
-		`# Dynamic
-nameserver 127.0.1.1
-search docker.com`: true,
-		`# Dynamic
-`: true,
-		``: true,
-	} {
-		if CheckLocalDns([]byte(resolv)) != result {
-			t.Fatalf("Wrong local dns detection: {%s} should be %v", resolv, result)
-		}
-	}
-}
 func TestReplaceAndAppendEnvVars(t *testing.T) {
 func TestReplaceAndAppendEnvVars(t *testing.T) {
 	var (
 	var (
 		d = []string{"HOME=/"}
 		d = []string{"HOME=/"}