diff --git a/libnetwork/libnetwork_linux_test.go b/libnetwork/libnetwork_linux_test.go index 9de694e33b..9c6b3b1d1c 100644 --- a/libnetwork/libnetwork_linux_test.go +++ b/libnetwork/libnetwork_linux_test.go @@ -658,6 +658,7 @@ func TestResolvConfHost(t *testing.T) { defer os.Remove(resolvConfPath) sb, err := controller.NewSandbox(containerID, + libnetwork.OptionUseDefaultSandbox(), libnetwork.OptionResolvConfPath(resolvConfPath), libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf")) if err != nil { diff --git a/libnetwork/osl/kernel/knobs.go b/libnetwork/osl/kernel/knobs.go index 5088f0e7f9..a7cd7dbb72 100644 --- a/libnetwork/osl/kernel/knobs.go +++ b/libnetwork/osl/kernel/knobs.go @@ -2,7 +2,7 @@ package kernel type conditionalCheck func(val1, val2 string) bool -// OSValue represents a tuple, value defired, check function when to apply the value +// OSValue represents a tuple, value defined, check function when to apply the value type OSValue struct { Value string CheckFn conditionalCheck diff --git a/libnetwork/resolvconf/resolvconf.go b/libnetwork/resolvconf/resolvconf.go index 5cb251b131..23caf7f120 100644 --- a/libnetwork/resolvconf/resolvconf.go +++ b/libnetwork/resolvconf/resolvconf.go @@ -14,6 +14,11 @@ import ( "github.com/sirupsen/logrus" ) +const ( + // DefaultResolvConf points to the default file used for dns configuration on a linux machine + DefaultResolvConf = "/etc/resolv.conf" +) + var ( // Note: the default IPv4 & IPv6 resolvers are set to Google's Public DNS defaultIPv4Dns = []string{"nameserver 8.8.8.8", "nameserver 8.8.4.4"} @@ -50,15 +55,7 @@ type File struct { // Get returns the contents of /etc/resolv.conf and its hash func Get() (*File, error) { - resolv, err := ioutil.ReadFile("/etc/resolv.conf") - if err != nil { - return nil, err - } - hash, err := ioutils.HashData(bytes.NewReader(resolv)) - if err != nil { - return nil, err - } - return &File{Content: resolv, Hash: hash}, nil + return GetSpecific(DefaultResolvConf) } // GetSpecific returns the contents of the user specified resolv.conf file and its hash diff --git a/libnetwork/sandbox.go b/libnetwork/sandbox.go index acbc243974..947a75dda7 100644 --- a/libnetwork/sandbox.go +++ b/libnetwork/sandbox.go @@ -1098,8 +1098,8 @@ func OptionDNSOptions(options string) SandboxOption { } } -// OptionUseDefaultSandbox function returns an option setter for using default sandbox to -// be passed to container Create method. +// OptionUseDefaultSandbox function returns an option setter for using default sandbox +// (host namespace) to be passed to container Create method. func OptionUseDefaultSandbox() SandboxOption { return func(sb *sandbox) { sb.config.useDefaultSandBox = true diff --git a/libnetwork/sandbox_dns_unix.go b/libnetwork/sandbox_dns_unix.go index ed94ee7a34..db1b66b190 100644 --- a/libnetwork/sandbox_dns_unix.go +++ b/libnetwork/sandbox_dns_unix.go @@ -81,7 +81,9 @@ func (sb *sandbox) buildHostsFile() error { } // This is for the host mode networking - if sb.config.originHostsPath != "" { + if sb.config.useDefaultSandBox && len(sb.config.extraHosts) == 0 { + // We are working under the assumption that the origin file option had been properly expressed by the upper layer + // if not here we are going to error out if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) { return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err) } @@ -190,8 +192,13 @@ func (sb *sandbox) setupDNS() error { return err } - // This is for the host mode networking - if sb.config.originResolvConfPath != "" { + // When the user specify a conainter in the host namespace and do no have any dns option specified + // we just copy the host resolv.conf from the host itself + if sb.config.useDefaultSandBox && + len(sb.config.dnsList) == 0 && len(sb.config.dnsSearchList) == 0 && len(sb.config.dnsOptionsList) == 0 { + + // We are working under the assumption that the origin file option had been properly expressed by the upper layer + // if not here we are going to error out if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil { if !os.IsNotExist(err) { return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err) @@ -204,7 +211,12 @@ func (sb *sandbox) setupDNS() error { return nil } - currRC, err := resolvconf.Get() + originResolvConfPath := sb.config.originResolvConfPath + if originResolvConfPath == "" { + // if not specified fallback to default /etc/resolv.conf + originResolvConfPath = resolvconf.DefaultResolvConf + } + currRC, err := resolvconf.GetSpecific(originResolvConfPath) if err != nil { if !os.IsNotExist(err) { return err @@ -271,7 +283,7 @@ func (sb *sandbox) updateDNS(ipv6Enabled bool) error { ) // This is for the host mode networking - if sb.config.originResolvConfPath != "" { + if sb.config.useDefaultSandBox { return nil }