зеркало из https://github.com/microsoft/docker.git
Merge pull request #1389 from dotcloud/1373-improve_checklocaldns
Consider empty /etc/resolv.conf as local dns + add unit test
This commit is contained in:
Коммит
030cc8d5cc
7
api.go
7
api.go
|
@ -488,7 +488,12 @@ func postContainersCreate(srv *Server, version float64, w http.ResponseWriter, r
|
|||
return err
|
||||
}
|
||||
|
||||
if len(config.Dns) == 0 && len(srv.runtime.Dns) == 0 && utils.CheckLocalDns() {
|
||||
resolvConf, err := utils.GetResolvConf()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(config.Dns) == 0 && len(srv.runtime.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
|
||||
out.Warnings = append(out.Warnings, fmt.Sprintf("Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns))
|
||||
config.Dns = defaultDns
|
||||
}
|
||||
|
|
|
@ -80,7 +80,12 @@ func (builder *Builder) Create(config *Config) (*Container, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if len(config.Dns) == 0 && len(builder.runtime.Dns) == 0 && utils.CheckLocalDns() {
|
||||
resolvConf, err := utils.GetResolvConf()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(config.Dns) == 0 && len(builder.runtime.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
|
||||
//"WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns
|
||||
builder.runtime.Dns = defaultDns
|
||||
}
|
||||
|
|
|
@ -709,17 +709,29 @@ func IsGIT(str string) bool {
|
|||
return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/")
|
||||
}
|
||||
|
||||
func CheckLocalDns() bool {
|
||||
// GetResolvConf opens and read the content of /etc/resolv.conf.
|
||||
// It returns it as byte slice.
|
||||
func GetResolvConf() ([]byte, error) {
|
||||
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
if err != nil {
|
||||
Debugf("Error openning resolv.conf: %s", err)
|
||||
return false
|
||||
return nil, err
|
||||
}
|
||||
for _, ip := range []string{
|
||||
"127.0.0.1",
|
||||
"127.0.1.1",
|
||||
return resolv, nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if !bytes.Contains(resolvConf, []byte("nameserver")) {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, ip := range [][]byte{
|
||||
[]byte("127.0.0.1"),
|
||||
[]byte("127.0.1.1"),
|
||||
} {
|
||||
if strings.Contains(string(resolv), ip) {
|
||||
if bytes.Contains(resolvConf, ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -303,3 +303,37 @@ func TestParseRepositoryTag(t *testing.T) {
|
|||
t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "tag", repo, tag)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetResolvConf(t *testing.T) {
|
||||
resolvConfUtils, err := GetResolvConf()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(resolvConfUtils) != string(resolvConfSystem) {
|
||||
t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheclLocalDns(t *testing.T) {
|
||||
for resolv, result := range map[string]bool{`# Dynamic
|
||||
nameserver 10.0.2.3
|
||||
search dotcloud.net`: false,
|
||||
`# Dynamic
|
||||
nameserver 127.0.0.1
|
||||
search dotcloud.net`: true,
|
||||
`# Dynamic
|
||||
nameserver 127.0.1.1
|
||||
search dotcloud.net`: true,
|
||||
`# Dynamic
|
||||
`: true,
|
||||
``: true,
|
||||
} {
|
||||
if CheckLocalDns([]byte(resolv)) != result {
|
||||
t.Fatalf("Wrong local dns detection: {%s} should be %v", resolv, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче