mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #10713 from swagiaal/pickup-created-resolvconf
Update resolv.conf in containers on file creation.
This commit is contained in:
commit
2380004d30
2 changed files with 52 additions and 3 deletions
|
@ -441,7 +441,9 @@ func (daemon *Daemon) setupResolvconfWatcher() error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-watcher.Events:
|
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
|
// verify a real change happened before we go further--a file write may have happened
|
||||||
// without an actual change to the file
|
// without an actual change to the file
|
||||||
updatedResolvConf, newResolvConfHash, err := resolvconf.GetIfChanged()
|
updatedResolvConf, newResolvConfHash, err := resolvconf.GetIfChanged()
|
||||||
|
@ -474,7 +476,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 err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1600,6 +1600,14 @@ func TestRunResolvconfUpdater(t *testing.T) {
|
||||||
t.Fatal(err)
|
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
|
//cleanup
|
||||||
defer func() {
|
defer func() {
|
||||||
deleteAllContainers()
|
deleteAllContainers()
|
||||||
|
@ -1609,7 +1617,7 @@ func TestRunResolvconfUpdater(t *testing.T) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//1. test that a non-running container gets an updated resolv.conf
|
//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 {
|
if _, err := runCommand(cmd); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -1732,6 +1740,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))
|
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()
|
//cleanup, restore original resolv.conf happens in defer func()
|
||||||
logDone("run - resolv.conf updater")
|
logDone("run - resolv.conf updater")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue