Add support for IPV6 in dns resolvers

This commit is contained in:
Manuel de Brito Fontes 2016-12-22 00:00:27 -03:00
parent 8e90fc0290
commit 99209ad33d
10 changed files with 153 additions and 16 deletions

View file

@ -18,15 +18,18 @@ package dns
import (
"io/ioutil"
"net"
"strings"
"github.com/golang/glog"
)
var defResolvConf = "/etc/resolv.conf"
// GetSystemNameServers returns the list of nameservers located in the file /etc/resolv.conf
func GetSystemNameServers() ([]string, error) {
var nameservers []string
file, err := ioutil.ReadFile("/etc/resolv.conf")
func GetSystemNameServers() ([]net.IP, error) {
var nameservers []net.IP
file, err := ioutil.ReadFile(defResolvConf)
if err != nil {
return nameservers, err
}
@ -43,10 +46,13 @@ func GetSystemNameServers() ([]string, error) {
continue
}
if fields[0] == "nameserver" {
nameservers = append(nameservers, fields[1:]...)
ip := net.ParseIP(fields[1])
if ip != nil {
nameservers = append(nameservers, ip)
}
}
}
glog.V(3).Infof("nameservers to use: %v", nameservers)
glog.V(3).Infof("nameservers IP address/es to use: %v", nameservers)
return nameservers, nil
}

View file

@ -17,6 +17,9 @@ limitations under the License.
package dns
import (
"io/ioutil"
"net"
"os"
"testing"
)
@ -28,4 +31,31 @@ func TestGetDNSServers(t *testing.T) {
if len(s) < 1 {
t.Error("expected at least 1 nameserver in /etc/resolv.conf")
}
file, err := ioutil.TempFile("", "fw")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer file.Close()
defer os.Remove(file.Name())
ioutil.WriteFile(file.Name(), []byte(`
nameserver 2001:4860:4860::8844
nameserver 2001:4860:4860::8888
nameserver 8.8.8.8
`), 0644)
defResolvConf = file.Name()
s, err = GetSystemNameServers()
if err != nil {
t.Fatalf("unexpected error reading /etc/resolv.conf file: %v", err)
}
if len(s) < 3 {
t.Errorf("expected at 3 nameservers but %v returned", len(s))
}
eip := net.ParseIP("2001:4860:4860::8844")
if !s[0].Equal(eip) {
t.Errorf("expected %v as nameservers but %v returned", eip, s[0])
}
}

30
core/pkg/net/net.go Normal file
View file

@ -0,0 +1,30 @@
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net
import (
_net "net"
"strings"
)
// IsIPV6 checks if the input contains a valid IPV6 address
func IsIPV6(ip _net.IP) bool {
if dp := strings.Index(ip.String(), ":"); dp != -1 {
return true
}
return false
}

43
core/pkg/net/net_test.go Normal file
View file

@ -0,0 +1,43 @@
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net
import (
"net"
"testing"
)
func TestIsIPV6(t *testing.T) {
tests := []struct {
in net.IP
isIPV6 bool
}{
{net.ParseIP("2001:4860:4860::8844"), true},
{net.ParseIP("2001:4860:4860::8888"), true},
{net.ParseIP("0:0:0:0:0:ffff:c868:8165"), true},
{net.ParseIP("2001:db8:85a3::8a2e:370:7334"), true},
{net.ParseIP("::1"), true},
{net.ParseIP("8.8.8.8"), false},
}
for _, test := range tests {
isIPV6 := IsIPV6(test.in)
if isIPV6 && !test.isIPV6 {
t.Fatalf("%v expected %v but returned %v", test.in, test.isIPV6, isIPV6)
}
}
}