diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index b26d803230..e8cf08aaba 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -781,21 +781,37 @@ func GetResolvConf() ([]byte, error) { // CheckLocalDns looks into the /etc/resolv.conf, // it returns true if there is a local nameserver or if there is no nameserver. func CheckLocalDns(resolvConf []byte) bool { - if !bytes.Contains(resolvConf, []byte("nameserver")) { + var parsedResolvConf = StripComments(resolvConf, []byte("#")) + if !bytes.Contains(parsedResolvConf, []byte("nameserver")) { return true } - for _, ip := range [][]byte{ []byte("127.0.0.1"), []byte("127.0.1.1"), } { - if bytes.Contains(resolvConf, ip) { + if bytes.Contains(parsedResolvConf, ip) { return true } } return false } +// StripComments parses input into lines and strips away comments. +func StripComments(input []byte, commentMarker []byte) []byte { + lines := bytes.Split(input, []byte("\n")) + var output []byte + for _, currentLine := range lines { + var commentIndex = bytes.Index(currentLine, commentMarker) + if ( commentIndex == -1 ) { + output = append(output, currentLine...) + } else { + output = append(output, currentLine[:commentIndex]...) + } + output = append(output, []byte("\n")...) + } + return output +} + func ParseHost(host string, port int, addr string) string { if strings.HasPrefix(addr, "unix://") { return addr diff --git a/components/engine/utils/utils_test.go b/components/engine/utils/utils_test.go index 20a5820dd5..3341650860 100644 --- a/components/engine/utils/utils_test.go +++ b/components/engine/utils/utils_test.go @@ -323,6 +323,16 @@ func TestCheckLocalDns(t *testing.T) { nameserver 10.0.2.3 search dotcloud.net`: false, `# Dynamic +#nameserver 127.0.0.1 +nameserver 10.0.2.3 +search dotcloud.net`: false, + `# Dynamic +nameserver 10.0.2.3 #not used 127.0.1.1 +search dotcloud.net`: false, + `# Dynamic +#nameserver 10.0.2.3 +#search dotcloud.net`: true, + `# Dynamic nameserver 127.0.0.1 search dotcloud.net`: true, `# Dynamic