Skip to content

Commit

Permalink
populate resolv.conf with dnsname responses when in usernamespace
Browse files Browse the repository at this point in the history
when using usernamespace, dnsname respondes from cni were not making it into the containers /etc/resolv.conf because of a timing issue.  this corrects that behavior.

Fixes: containers#5256

Signed-off-by: Brent Baude <bbaude@redhat.com>
  • Loading branch information
baude committed Feb 20, 2020
1 parent cf8e34c commit 921f29c
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ func (c *Container) checkDependenciesRunning() ([]string, error) {
}

func (c *Container) completeNetworkSetup() error {
var outResolvConf []string
netDisabled, err := c.NetworkDisabled()
if err != nil {
return err
Expand All @@ -927,7 +928,37 @@ func (c *Container) completeNetworkSetup() error {
if c.config.NetMode == "slirp4netns" {
return c.runtime.setupRootlessNetNS(c)
}
return c.runtime.setupNetNS(c)
if err := c.runtime.setupNetNS(c); err != nil {
return err
}
state := c.state
// collect any dns servers that cni tells us to use (dnsname)
for _, cni := range state.NetworkStatus {
if cni.DNS.Nameservers != nil {
for _, server := range cni.DNS.Nameservers {
outResolvConf = append(outResolvConf, fmt.Sprintf("nameserver %s", server))
}
}
}
// check if we have a bindmount for resolv.conf
resolvBindMount := state.BindMounts["/etc/resolv.conf"]
if len(outResolvConf) < 1 || resolvBindMount == "" || len(c.config.NetNsCtr) > 0 {
return nil
}
// read the existing resolv.conf
b, err := ioutil.ReadFile(resolvBindMount)
if err != nil {
return err
}
for _, line := range strings.Split(string(b), "\n") {
// only keep things that dont start with nameserver from the old
// resolv.conf file
if !strings.HasPrefix(line, "nameserver") {
outResolvConf = append([]string{line}, outResolvConf...)
}
}
// write and return
return ioutil.WriteFile(resolvBindMount, []byte(strings.Join(outResolvConf, "\n")), 0644)
}

// Initialize a container, creating it in the runtime
Expand Down

0 comments on commit 921f29c

Please sign in to comment.