Merge pull request #8047 from rhatdan/RemoveLocalDns

Remove nameserver 127.0.0.1 line rather then dumping resolv.conf
This commit is contained in:
Alexandr Morozov 2014-09-29 11:58:27 -07:00
commit 6cd8602827
5 changed files with 44 additions and 65 deletions

View File

@ -1,6 +1,7 @@
package daemon package daemon
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -1044,8 +1045,10 @@ func (daemon *Daemon) checkLocaldns() error {
if err != nil { if err != nil {
return err return err
} }
if len(daemon.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) { resolvConf = utils.RemoveLocalDns(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

View File

@ -27,3 +27,34 @@ func TestMergeLxcConfig(t *testing.T) {
t.Fatalf("expected %s got %s", expected, cpuset) t.Fatalf("expected %s got %s", expected, cpuset)
} }
} }
func TestRemoveLocalDns(t *testing.T) {
ns0 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\n"
if result := utils.RemoveLocalDns([]byte(ns0)); result != nil {
if ns0 != string(result) {
t.Fatalf("Failed No Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
}
}
ns1 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\nnameserver 127.0.0.1\n"
if result := utils.RemoveLocalDns([]byte(ns1)); result != nil {
if ns0 != string(result) {
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
}
}
ns1 = "nameserver 10.16.60.14\nnameserver 127.0.0.1\nnameserver 10.16.60.21\n"
if result := utils.RemoveLocalDns([]byte(ns1)); result != nil {
if ns0 != string(result) {
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
}
}
ns1 = "nameserver 127.0.1.1\nnameserver 10.16.60.14\nnameserver 10.16.60.21\n"
if result := utils.RemoveLocalDns([]byte(ns1)); result != nil {
if ns0 != string(result) {
t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
}
}
}

View File

@ -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

View File

@ -13,6 +13,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -312,39 +313,14 @@ 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, var (
// it returns true if there is a local nameserver or if there is no nameserver. localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
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
}
// GetLines parses input into lines and strips away comments. // RemoveLocalDns looks into the /etc/resolv.conf,
func GetLines(input []byte, commentMarker []byte) [][]byte { // and removes any local nameserver entries.
lines := bytes.Split(input, []byte("\n")) func RemoveLocalDns(resolvConf []byte) []byte {
var output [][]byte return localHostRx.ReplaceAll(resolvConf, []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.

View File

@ -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=/"}