cleanup resolve.conf code
Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)
This commit is contained in:
parent
acd511786e
commit
dbe6c6651e
2 changed files with 39 additions and 19 deletions
|
@ -920,6 +920,7 @@ func (container *Container) setupContainerDns() error {
|
|||
}
|
||||
|
||||
if config.NetworkMode != "host" {
|
||||
// check configurations for any container/daemon dns settings
|
||||
if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
|
||||
var (
|
||||
dns = resolvconf.GetNameservers(resolvConf)
|
||||
|
@ -936,15 +937,15 @@ func (container *Container) setupContainerDns() error {
|
|||
dnsSearch = daemon.config.DnsSearch
|
||||
}
|
||||
return resolvconf.Build(container.ResolvConfPath, dns, dnsSearch)
|
||||
} else {
|
||||
}
|
||||
|
||||
// replace any localhost/127.* nameservers
|
||||
resolvConf = utils.RemoveLocalDns(resolvConf)
|
||||
// if the resulting resolvConf is empty, use DefaultDns
|
||||
if !bytes.Contains(resolvConf, []byte("nameserver")) {
|
||||
for _, dns := range DefaultDns {
|
||||
log.Infof("No non localhost DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
|
||||
resolvConf = append(append(resolvConf, []byte("\nnameserver ")...), dns...)
|
||||
}
|
||||
resolvConf = append(resolvConf, []byte("\n")...)
|
||||
}
|
||||
// prefix the default dns options with nameserver
|
||||
resolvConf = append(resolvConf, []byte("\nnameserver "+strings.Join(DefaultDns, "\nnameserver "))...)
|
||||
}
|
||||
}
|
||||
return ioutil.WriteFile(container.ResolvConfPath, resolvConf, 0644)
|
||||
|
|
|
@ -1266,20 +1266,39 @@ func TestRunWithVolumesIsRecursive(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRunDnsDefaultOptions(t *testing.T) {
|
||||
cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
|
||||
|
||||
actual, _, err := runCommandWithOutput(cmd)
|
||||
if err != nil {
|
||||
t.Fatal(err, actual)
|
||||
}
|
||||
|
||||
resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
// ci server has default resolv.conf
|
||||
// so rewrite it for the test
|
||||
origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
if os.IsNotExist(err) {
|
||||
t.Fatalf("/etc/resolv.conf does not exist")
|
||||
}
|
||||
|
||||
if actual != string(resolvConf) {
|
||||
t.Fatalf("expected resolv.conf is not the same of actual")
|
||||
// test with file
|
||||
tmpResolvConf := []byte("nameserver 127.0.0.1")
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// put the old resolvconf back
|
||||
defer func() {
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
|
||||
|
||||
actual, _, err := runCommandWithOutput(cmd)
|
||||
if err != nil {
|
||||
t.Error(err, actual)
|
||||
return
|
||||
}
|
||||
|
||||
// check that the actual defaults are there
|
||||
// if we ever change the defaults from google dns, this will break
|
||||
expected := "\nnameserver 8.8.8.8\nnameserver 8.8.4.4"
|
||||
if actual != expected {
|
||||
t.Errorf("expected resolv.conf be: %q, but was: %q", expected, actual)
|
||||
return
|
||||
}
|
||||
|
||||
deleteAllContainers()
|
||||
|
|
Loading…
Reference in a new issue