Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resolve.conf in containers on file creation. #10713

Merged
merged 1 commit into from Mar 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions daemon/daemon.go
Expand Up @@ -440,7 +440,9 @@ func (daemon *Daemon) setupResolvconfWatcher() error {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
if event.Name == "/etc/resolv.conf" &&
(event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Create == fsnotify.Create) {
// verify a real change happened before we go further--a file write may have happened
// without an actual change to the file
updatedResolvConf, newResolvConfHash, err := resolvconf.GetIfChanged()
Expand Down Expand Up @@ -473,7 +475,7 @@ func (daemon *Daemon) setupResolvconfWatcher() error {
}
}()

if err := watcher.Add("/etc/resolv.conf"); err != nil {
if err := watcher.Add("/etc"); err != nil {
return err
}
return nil
Expand Down
49 changes: 48 additions & 1 deletion integration-cli/docker_cli_run_test.go
Expand Up @@ -1598,6 +1598,14 @@ func TestRunResolvconfUpdater(t *testing.T) {
t.Fatal(err)
}

// This test case is meant to test monitoring resolv.conf when it is
// a regular file not a bind mount. So we unmount resolv.conf and replace
// it with a file containing the original settings.
cmd := exec.Command("umount", "/etc/resolv.conf")
if _, err = runCommand(cmd); err != nil {
t.Fatal(err)
}

//cleanup
defer func() {
deleteAllContainers()
Expand All @@ -1607,7 +1615,7 @@ func TestRunResolvconfUpdater(t *testing.T) {
}()

//1. test that a non-running container gets an updated resolv.conf
cmd := exec.Command(dockerBinary, "run", "--name='first'", "busybox", "true")
cmd = exec.Command(dockerBinary, "run", "--name='first'", "busybox", "true")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1730,6 +1738,45 @@ func TestRunResolvconfUpdater(t *testing.T) {
t.Fatalf("Container does not have cleaned/replaced DNS in resolv.conf; expected %q, got %q", expected, string(containerResolv))
}

//6. Test that replacing (as opposed to modifying) resolv.conf triggers an update
// of containers' resolv.conf.

// Restore the original resolv.conf
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
t.Fatal(err)
}

// Run the container so it picks up the old settings
cmd = exec.Command(dockerBinary, "run", "--name='third'", "busybox", "true")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}
containerID3, err := getIDByName("third")
if err != nil {
t.Fatal(err)
}

// Create a modified resolv.conf.aside and override resolv.conf with it
bytesResolvConf = []byte(tmpResolvConf)
if err := ioutil.WriteFile("/etc/resolv.conf.aside", bytesResolvConf, 0644); err != nil {
t.Fatal(err)
}

err = os.Rename("/etc/resolv.conf.aside", "/etc/resolv.conf")
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Second / 2)
// check for update in container
containerResolv, err = readContainerFile(containerID3, "resolv.conf")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(containerResolv, bytesResolvConf) {
t.Fatalf("Stopped container does not have updated resolv.conf; expected\n%q\n got\n%q", tmpResolvConf, string(containerResolv))
}

//cleanup, restore original resolv.conf happens in defer func()
logDone("run - resolv.conf updater")
}
Expand Down