mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 03:03:59 +00:00
Merge pull request #46138 from pmichali/issue44848b
Automatic merge from submit-queue (batch tested with PRs 48196, 42783, 48507, 47719, 46138) IPv6 support for getting IP from default route This is another part of the effort to update ChoseHostInterface() to support IPv6. In particular, this focuses on the call path, starting from chooseHostInterfaceFromRoute(), which attempts to find the node IP by using default route information. In the original code, routes are collected, and examined to find default routes. For a default route, the IPs for the associated interface are checked to see if there is one that is a V4 address, and is not a loopback, link local, or multicast address. If found, that IP will be used for the node IP. With this PR, there are some slight changes to prepare for allowing IPs from IPv6 default routes. The routes (IPv4 at this time - a subsequent PR will handle IPv6) are collected as before. If the route is a default route AND it's GW address is a global unicast address, then the IPs for the associated interface are checked. This time though, we just pick the IP that is on the same subnet as the gateway IP. This ensures it is not a link local, loopback, or multicast address. It saves time, by nt checking IPs for interfaces that don't have a "global" default route. It also will ensure the right IP is used, when using both IPv4 and IPv6 addresses. For example, if we have eth0 with global IPv4 and IPv6 addresses, and an IPv6 default route, we want to select the IPv6 address, as it is associated with the default route. Another case is that same interface, along with eth1 containing an IPv4 address with a default route. We want to select eth1's IPv4 address, and not the IPv4 on eth0. This change adds more UT coverage to several methods, and removes UTs that are redundantly testing at a higher level. Coverage is slightly improved. **What this PR does / why we need it**: **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: xref #44848 **Special notes for your reviewer**: This goes along with PR 46044, and will have another PR to the next part. **Release note**: ```release-noteNONE ```
This commit is contained in:
commit
1591975808
@ -43,6 +43,7 @@ type Route struct {
|
|||||||
// TODO: add more fields here if needed
|
// TODO: add more fields here if needed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getRoutes obtains the IPv4 routes, and filters out non-default routes.
|
||||||
func getRoutes(input io.Reader) ([]Route, error) {
|
func getRoutes(input io.Reader) ([]Route, error) {
|
||||||
routes := []Route{}
|
routes := []Route{}
|
||||||
if input == nil {
|
if input == nil {
|
||||||
@ -59,24 +60,30 @@ func getRoutes(input io.Reader) ([]Route, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
routes = append(routes, Route{})
|
dest, err := parseHexToIPv4(fields[1])
|
||||||
route := &routes[len(routes)-1]
|
|
||||||
route.Interface = fields[0]
|
|
||||||
ip, err := parseIP(fields[1])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
route.Destination = ip
|
gw, err := parseHexToIPv4(fields[2])
|
||||||
ip, err = parseIP(fields[2])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
route.Gateway = ip
|
if !dest.Equal(net.IPv4zero) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
routes = append(routes, Route{
|
||||||
|
Interface: fields[0],
|
||||||
|
Destination: dest,
|
||||||
|
Gateway: gw,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return routes, nil
|
return routes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIP(str string) (net.IP, error) {
|
// parseHexToIPv4 takes the hex IP address string from route file and converts it
|
||||||
|
// from little endian to big endian for creation of a net.IP address.
|
||||||
|
// a net.IP, using big endian ordering.
|
||||||
|
func parseHexToIPv4(str string) (net.IP, error) {
|
||||||
if str == "" {
|
if str == "" {
|
||||||
return nil, fmt.Errorf("input is nil")
|
return nil, fmt.Errorf("input is nil")
|
||||||
}
|
}
|
||||||
@ -84,12 +91,10 @@ func parseIP(str string) (net.IP, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//TODO add ipv6 support
|
|
||||||
if len(bytes) != net.IPv4len {
|
if len(bytes) != net.IPv4len {
|
||||||
return nil, fmt.Errorf("only IPv4 is supported")
|
return nil, fmt.Errorf("invalid IPv4 address in route")
|
||||||
}
|
}
|
||||||
bytes[0], bytes[1], bytes[2], bytes[3] = bytes[3], bytes[2], bytes[1], bytes[0]
|
return net.IP([]byte{bytes[3], bytes[2], bytes[1], bytes[0]}), nil
|
||||||
return net.IP(bytes), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInterfaceUp(intf *net.Interface) bool {
|
func isInterfaceUp(intf *net.Interface) bool {
|
||||||
@ -107,10 +112,18 @@ func isLoopbackOrPointToPoint(intf *net.Interface) bool {
|
|||||||
return intf.Flags&(net.FlagLoopback|net.FlagPointToPoint) != 0
|
return intf.Flags&(net.FlagLoopback|net.FlagPointToPoint) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//getFinalIP method receives all the IP addrs of a Interface
|
func inFamily(ip net.IP, expectedFamily AddressFamily) bool {
|
||||||
//and returns a nil if the address is Loopback, Ipv6, link-local or nil.
|
ipFamily := familyIPv4
|
||||||
//It returns a valid IPv4 if an Ipv4 address is found in the array.
|
if ip.To4() == nil {
|
||||||
func getFinalIP(addrs []net.Addr) (net.IP, error) {
|
ipFamily = familyIPv6
|
||||||
|
}
|
||||||
|
return ipFamily == expectedFamily
|
||||||
|
}
|
||||||
|
|
||||||
|
// getMatchingGlobalIP method checks all the IP addresses of a Interface looking
|
||||||
|
// for a valid non-loopback/link-local address of the requested family and returns
|
||||||
|
// it, if found.
|
||||||
|
func getMatchingGlobalIP(addrs []net.Addr, family AddressFamily) (net.IP, error) {
|
||||||
if len(addrs) > 0 {
|
if len(addrs) > 0 {
|
||||||
for i := range addrs {
|
for i := range addrs {
|
||||||
glog.V(4).Infof("Checking addr %s.", addrs[i].String())
|
glog.V(4).Infof("Checking addr %s.", addrs[i].String())
|
||||||
@ -118,17 +131,15 @@ func getFinalIP(addrs []net.Addr) (net.IP, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//Only IPv4
|
if inFamily(ip, family) {
|
||||||
//TODO : add IPv6 support
|
if ip.IsGlobalUnicast() {
|
||||||
if ip.To4() != nil {
|
|
||||||
if !ip.IsLoopback() && !ip.IsLinkLocalMulticast() && !ip.IsLinkLocalUnicast() {
|
|
||||||
glog.V(4).Infof("IP found %v", ip)
|
glog.V(4).Infof("IP found %v", ip)
|
||||||
return ip, nil
|
return ip, nil
|
||||||
} else {
|
} else {
|
||||||
glog.V(4).Infof("Loopback/link-local found %v", ip)
|
glog.V(4).Infof("non-global IP found %v", ip)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
glog.V(4).Infof("%v is not a valid IPv4 address", ip)
|
glog.V(4).Infof("%v is not an IPv%d address", ip, int(family))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -136,7 +147,7 @@ func getFinalIP(addrs []net.Addr) (net.IP, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIPFromInterface(intfName string, nw networkInterfacer) (net.IP, error) {
|
func getIPFromInterface(intfName string, forFamily AddressFamily, nw networkInterfacer) (net.IP, error) {
|
||||||
intf, err := nw.InterfaceByName(intfName)
|
intf, err := nw.InterfaceByName(intfName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -147,16 +158,15 @@ func getIPFromInterface(intfName string, nw networkInterfacer) (net.IP, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
glog.V(4).Infof("Interface %q has %d addresses :%v.", intfName, len(addrs), addrs)
|
glog.V(4).Infof("Interface %q has %d addresses :%v.", intfName, len(addrs), addrs)
|
||||||
finalIP, err := getFinalIP(addrs)
|
matchingIP, err := getMatchingGlobalIP(addrs, forFamily)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if finalIP != nil {
|
if matchingIP != nil {
|
||||||
glog.V(4).Infof("valid IPv4 address for interface %q found as %v.", intfName, finalIP)
|
glog.V(4).Infof("Found valid IPv%d address %v for interface %q.", int(forFamily), matchingIP, intfName)
|
||||||
return finalIP, nil
|
return matchingIP, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,27 +278,30 @@ func chooseHostInterfaceFromRoute(inFile io.Reader, nw networkInterfacer) (net.I
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
zero := net.IP{0, 0, 0, 0}
|
if len(routes) == 0 {
|
||||||
var finalIP net.IP
|
return nil, fmt.Errorf("No default routes.")
|
||||||
for i := range routes {
|
}
|
||||||
//find interface with gateway
|
// TODO: append IPv6 routes for processing - currently only have IPv4 routes
|
||||||
if routes[i].Destination.Equal(zero) {
|
for _, family := range []AddressFamily{familyIPv4, familyIPv6} {
|
||||||
glog.V(4).Infof("Default route transits interface %q", routes[i].Interface)
|
glog.V(4).Infof("Looking for default routes with IPv%d addresses", uint(family))
|
||||||
finalIP, err := getIPFromInterface(routes[i].Interface, nw)
|
for _, route := range routes {
|
||||||
|
// TODO: When have IPv6 routes, filter here to speed up processing
|
||||||
|
// if route.Family != family {
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
glog.V(4).Infof("Default route transits interface %q", route.Interface)
|
||||||
|
finalIP, err := getIPFromInterface(route.Interface, family, nw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if finalIP != nil {
|
if finalIP != nil {
|
||||||
glog.V(4).Infof("Choosing IP %v ", finalIP)
|
glog.V(4).Infof("Found active IP %v ", finalIP)
|
||||||
return finalIP, nil
|
return finalIP, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
glog.V(4).Infof("No valid IP found")
|
glog.V(4).Infof("No active IP found by looking at default routes")
|
||||||
if finalIP == nil {
|
return nil, fmt.Errorf("unable to select an IP from default routes.")
|
||||||
return nil, fmt.Errorf("Unable to select an IP.")
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If bind-address is usable, return it directly
|
// If bind-address is usable, return it directly
|
||||||
|
@ -48,13 +48,13 @@ virbr0 007AA8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
|
|||||||
`
|
`
|
||||||
const nothing = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
const nothing = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
||||||
`
|
`
|
||||||
const gatewayfirstIpv6_1 = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
const badDestination = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
||||||
eth3 00000000 0100FE0A 0003 0 0 1024 00000000 0 0 0
|
eth3 00000000 0100FE0A 0003 0 0 1024 00000000 0 0 0
|
||||||
eth3 0000FE0AA1 00000000 0001 0 0 0 0080FFFF 0 0 0
|
eth3 0000FE0AA1 00000000 0001 0 0 0 0080FFFF 0 0 0
|
||||||
docker0 000011AC 00000000 0001 0 0 0 0000FFFF 0 0 0
|
docker0 000011AC 00000000 0001 0 0 0 0000FFFF 0 0 0
|
||||||
virbr0 007AA8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
|
virbr0 007AA8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
|
||||||
`
|
`
|
||||||
const gatewayfirstIpv6_2 = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
const badGateway = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
||||||
eth3 00000000 0100FE0AA1 0003 0 0 1024 00000000 0 0 0
|
eth3 00000000 0100FE0AA1 0003 0 0 1024 00000000 0 0 0
|
||||||
eth3 0000FE0A 00000000 0001 0 0 0 0080FFFF 0 0 0
|
eth3 0000FE0A 00000000 0001 0 0 0 0080FFFF 0 0 0
|
||||||
docker0 000011AC 00000000 0001 0 0 0 0000FFFF 0 0 0
|
docker0 000011AC 00000000 0001 0 0 0 0000FFFF 0 0 0
|
||||||
@ -97,25 +97,42 @@ var (
|
|||||||
upIntf = makeIntf(1, "eth3", flagUp)
|
upIntf = makeIntf(1, "eth3", flagUp)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ipv4Route = Route{Interface: "eth3", Gateway: net.ParseIP("10.254.0.1")}
|
||||||
|
)
|
||||||
|
|
||||||
func TestGetRoutes(t *testing.T) {
|
func TestGetRoutes(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
tcase string
|
tcase string
|
||||||
route string
|
route string
|
||||||
expected int
|
count int
|
||||||
|
expected *Route
|
||||||
|
errStrFrag string
|
||||||
}{
|
}{
|
||||||
{"gatewayfirst", gatewayfirst, 4},
|
{"gatewayfirst", gatewayfirst, 1, &ipv4Route, ""},
|
||||||
{"gatewaymiddle", gatewaymiddle, 4},
|
{"gatewaymiddle", gatewaymiddle, 1, &ipv4Route, ""},
|
||||||
{"gatewaylast", gatewaylast, 4},
|
{"gatewaylast", gatewaylast, 1, &ipv4Route, ""},
|
||||||
{"nothing", nothing, 0},
|
{"no routes", nothing, 0, nil, ""},
|
||||||
{"gatewayfirstIpv6_1", gatewayfirstIpv6_1, 0},
|
{"badDestination", badDestination, 0, nil, "invalid IPv4"},
|
||||||
{"gatewayfirstIpv6_2", gatewayfirstIpv6_2, 0},
|
{"badGateway", badGateway, 0, nil, "invalid IPv4"},
|
||||||
{"route_Invalidhex", route_Invalidhex, 0},
|
{"route_Invalidhex", route_Invalidhex, 0, nil, "odd length hex string"},
|
||||||
|
{"no default routes", noInternetConnection, 0, nil, ""},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
r := strings.NewReader(tc.route)
|
r := strings.NewReader(tc.route)
|
||||||
routes, err := getRoutes(r)
|
routes, err := getRoutes(r)
|
||||||
if len(routes) != tc.expected {
|
if err != nil {
|
||||||
t.Errorf("case[%v]: expected %v, got %v .err : %v", tc.tcase, tc.expected, len(routes), err)
|
if !strings.Contains(err.Error(), tc.errStrFrag) {
|
||||||
|
t.Errorf("case[%s]: Error string %q does not contain %q", tc.tcase, err, tc.errStrFrag)
|
||||||
|
}
|
||||||
|
} else if tc.errStrFrag != "" {
|
||||||
|
t.Errorf("case[%s]: Error %q expected, but not seen", tc.tcase, tc.errStrFrag)
|
||||||
|
} else {
|
||||||
|
if tc.count != len(routes) {
|
||||||
|
t.Errorf("case[%s]: expected %d routes, have %v", tc.tcase, tc.count, routes)
|
||||||
|
} else if tc.count == 1 && !tc.expected.Gateway.Equal(routes[0].Gateway) {
|
||||||
|
t.Errorf("case[%s]: expected %v, got %v .err : %v", tc.tcase, tc.expected, routes, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,7 +153,7 @@ func TestParseIP(t *testing.T) {
|
|||||||
{"valid", "12345678", true, net.IP{120, 86, 52, 18}},
|
{"valid", "12345678", true, net.IP{120, 86, 52, 18}},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
ip, err := parseIP(tc.ip)
|
ip, err := parseHexToIPv4(tc.ip)
|
||||||
if !ip.Equal(tc.expected) {
|
if !ip.Equal(tc.expected) {
|
||||||
t.Errorf("case[%v]: expected %q, got %q . err : %v", tc.tcase, tc.expected, ip, err)
|
t.Errorf("case[%v]: expected %q, got %q . err : %v", tc.tcase, tc.expected, ip, err)
|
||||||
}
|
}
|
||||||
@ -146,15 +163,15 @@ func TestParseIP(t *testing.T) {
|
|||||||
func TestIsInterfaceUp(t *testing.T) {
|
func TestIsInterfaceUp(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
tcase string
|
tcase string
|
||||||
intf net.Interface
|
intf *net.Interface
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"up", net.Interface{Index: 0, MTU: 0, Name: "eth3", HardwareAddr: nil, Flags: net.FlagUp}, true},
|
{"up", &net.Interface{Index: 0, MTU: 0, Name: "eth3", HardwareAddr: nil, Flags: net.FlagUp}, true},
|
||||||
{"down", net.Interface{Index: 0, MTU: 0, Name: "eth3", HardwareAddr: nil, Flags: 0}, false},
|
{"down", &net.Interface{Index: 0, MTU: 0, Name: "eth3", HardwareAddr: nil, Flags: 0}, false},
|
||||||
{"nothing", net.Interface{}, false},
|
{"no interface", nil, false},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
it := isInterfaceUp(&tc.intf)
|
it := isInterfaceUp(tc.intf)
|
||||||
if it != tc.expected {
|
if it != tc.expected {
|
||||||
t.Errorf("case[%v]: expected %v, got %v .", tc.tcase, tc.expected, it)
|
t.Errorf("case[%v]: expected %v, got %v .", tc.tcase, tc.expected, it)
|
||||||
}
|
}
|
||||||
@ -174,17 +191,24 @@ func TestFinalIP(t *testing.T) {
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
tcase string
|
tcase string
|
||||||
addr []net.Addr
|
addr []net.Addr
|
||||||
|
family AddressFamily
|
||||||
expected net.IP
|
expected net.IP
|
||||||
}{
|
}{
|
||||||
{"ipv6", []net.Addr{addrStruct{val: "fe80::2f7:6fff:fe6e:2956/64"}}, nil},
|
{"no ipv4", []net.Addr{addrStruct{val: "2001::5/64"}}, familyIPv4, nil},
|
||||||
{"invalidCIDR", []net.Addr{addrStruct{val: "fe80::2f7:67fff:fe6e:2956/64"}}, nil},
|
{"no ipv6", []net.Addr{addrStruct{val: "10.128.0.4/32"}}, familyIPv6, nil},
|
||||||
{"loopback", []net.Addr{addrStruct{val: "127.0.0.1/24"}}, nil},
|
{"invalidV4CIDR", []net.Addr{addrStruct{val: "10.20.30.40.50/24"}}, familyIPv4, nil},
|
||||||
{"ip4", []net.Addr{addrStruct{val: "10.254.12.132/17"}}, net.ParseIP("10.254.12.132")},
|
{"invalidV6CIDR", []net.Addr{addrStruct{val: "fe80::2f7:67fff:fe6e:2956/64"}}, familyIPv6, nil},
|
||||||
|
{"loopback", []net.Addr{addrStruct{val: "127.0.0.1/24"}}, familyIPv4, nil},
|
||||||
|
{"loopbackv6", []net.Addr{addrStruct{val: "::1/128"}}, familyIPv6, nil},
|
||||||
|
{"link local v4", []net.Addr{addrStruct{val: "169.254.1.10/16"}}, familyIPv4, nil},
|
||||||
|
{"link local v6", []net.Addr{addrStruct{val: "fe80::2f7:6fff:fe6e:2956/64"}}, familyIPv6, nil},
|
||||||
|
{"ip4", []net.Addr{addrStruct{val: "10.254.12.132/17"}}, familyIPv4, net.ParseIP("10.254.12.132")},
|
||||||
|
{"ip6", []net.Addr{addrStruct{val: "2001::5/64"}}, familyIPv6, net.ParseIP("2001::5")},
|
||||||
|
|
||||||
{"nothing", []net.Addr{}, nil},
|
{"no addresses", []net.Addr{}, familyIPv4, nil},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
ip, err := getFinalIP(tc.addr)
|
ip, err := getMatchingGlobalIP(tc.addr, tc.family)
|
||||||
if !ip.Equal(tc.expected) {
|
if !ip.Equal(tc.expected) {
|
||||||
t.Errorf("case[%v]: expected %v, got %v .err : %v", tc.tcase, tc.expected, ip, err)
|
t.Errorf("case[%v]: expected %v, got %v .err : %v", tc.tcase, tc.expected, ip, err)
|
||||||
}
|
}
|
||||||
@ -203,40 +227,23 @@ func TestAddrs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Has a valid IPv4 address (IPv6 is LLA)
|
||||||
type validNetworkInterface struct {
|
type validNetworkInterface struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ validNetworkInterface) InterfaceByName(intfName string) (*net.Interface, error) {
|
func (_ validNetworkInterface) InterfaceByName(intfName string) (*net.Interface, error) {
|
||||||
c := net.Interface{Index: 0, MTU: 0, Name: "eth3", HardwareAddr: nil, Flags: net.FlagUp}
|
return &upIntf, nil
|
||||||
return &c, nil
|
|
||||||
}
|
}
|
||||||
func (_ validNetworkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
func (_ validNetworkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
||||||
var ifat []net.Addr
|
var ifat []net.Addr
|
||||||
ifat = []net.Addr{
|
ifat = []net.Addr{
|
||||||
addrStruct{val: "2001::200/64"}, addrStruct{val: "10.254.71.145/17"}}
|
addrStruct{val: "fe80::2f7:6fff:fe6e:2956/64"}, addrStruct{val: "10.254.71.145/17"}}
|
||||||
return ifat, nil
|
return ifat, nil
|
||||||
}
|
}
|
||||||
func (_ validNetworkInterface) Interfaces() ([]net.Interface, error) {
|
func (_ validNetworkInterface) Interfaces() ([]net.Interface, error) {
|
||||||
return []net.Interface{upIntf}, nil
|
return []net.Interface{upIntf}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// First is link-local, second is not.
|
|
||||||
type validNetworkInterfaceWithLinkLocal struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (_ validNetworkInterfaceWithLinkLocal) InterfaceByName(intfName string) (*net.Interface, error) {
|
|
||||||
c := net.Interface{Index: 0, MTU: 0, Name: "eth3", HardwareAddr: nil, Flags: net.FlagUp}
|
|
||||||
return &c, nil
|
|
||||||
}
|
|
||||||
func (_ validNetworkInterfaceWithLinkLocal) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
|
||||||
var ifat []net.Addr
|
|
||||||
ifat = []net.Addr{addrStruct{val: "169.254.162.166/16"}, addrStruct{val: "45.55.47.146/19"}}
|
|
||||||
return ifat, nil
|
|
||||||
}
|
|
||||||
func (_ validNetworkInterfaceWithLinkLocal) Interfaces() ([]net.Interface, error) {
|
|
||||||
return []net.Interface{upIntf}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interface with only IPv6 address
|
// Interface with only IPv6 address
|
||||||
type ipv6NetworkInterface struct {
|
type ipv6NetworkInterface struct {
|
||||||
}
|
}
|
||||||
@ -249,6 +256,7 @@ func (_ ipv6NetworkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
|||||||
ifat = []net.Addr{addrStruct{val: "2001::200/64"}}
|
ifat = []net.Addr{addrStruct{val: "2001::200/64"}}
|
||||||
return ifat, nil
|
return ifat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ ipv6NetworkInterface) Interfaces() ([]net.Interface, error) {
|
func (_ ipv6NetworkInterface) Interfaces() ([]net.Interface, error) {
|
||||||
return []net.Interface{upIntf}, nil
|
return []net.Interface{upIntf}, nil
|
||||||
}
|
}
|
||||||
@ -288,7 +296,7 @@ type noNetworkInterface struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (_ noNetworkInterface) InterfaceByName(intfName string) (*net.Interface, error) {
|
func (_ noNetworkInterface) InterfaceByName(intfName string) (*net.Interface, error) {
|
||||||
return nil, fmt.Errorf("unable get Interface")
|
return nil, fmt.Errorf("no such network interface")
|
||||||
}
|
}
|
||||||
func (_ noNetworkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
func (_ noNetworkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -397,16 +405,29 @@ func TestGetIPFromInterface(t *testing.T) {
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
tcase string
|
tcase string
|
||||||
nwname string
|
nwname string
|
||||||
|
family AddressFamily
|
||||||
nw networkInterfacer
|
nw networkInterfacer
|
||||||
expected net.IP
|
expected net.IP
|
||||||
|
errStrFrag string
|
||||||
}{
|
}{
|
||||||
{"valid", "eth3", validNetworkInterface{}, net.ParseIP("10.254.71.145")},
|
{"ipv4", "eth3", familyIPv4, validNetworkInterface{}, net.ParseIP("10.254.71.145"), ""},
|
||||||
{"ipv6", "eth3", ipv6NetworkInterface{}, nil},
|
{"ipv6", "eth3", familyIPv6, ipv6NetworkInterface{}, net.ParseIP("2001::200"), ""},
|
||||||
{"nothing", "eth3", noNetworkInterface{}, nil},
|
{"no ipv4", "eth3", familyIPv4, ipv6NetworkInterface{}, nil, ""},
|
||||||
|
{"no ipv6", "eth3", familyIPv6, validNetworkInterface{}, nil, ""},
|
||||||
|
{"I/F down", "eth3", familyIPv4, downNetworkInterface{}, nil, ""},
|
||||||
|
{"I/F get fail", "eth3", familyIPv4, noNetworkInterface{}, nil, "no such network interface"},
|
||||||
|
{"fail get addr", "eth3", familyIPv4, networkInterfaceFailGetAddrs{}, nil, "unable to get Addrs"},
|
||||||
|
{"bad addr", "eth3", familyIPv4, networkInterfaceWithInvalidAddr{}, nil, "invalid CIDR"},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
ip, err := getIPFromInterface(tc.nwname, tc.nw)
|
ip, err := getIPFromInterface(tc.nwname, tc.family, tc.nw)
|
||||||
if !ip.Equal(tc.expected) {
|
if err != nil {
|
||||||
|
if !strings.Contains(err.Error(), tc.errStrFrag) {
|
||||||
|
t.Errorf("case[%s]: Error string %q does not contain %q", tc.tcase, err, tc.errStrFrag)
|
||||||
|
}
|
||||||
|
} else if tc.errStrFrag != "" {
|
||||||
|
t.Errorf("case[%s]: Error %q expected, but not seen", tc.tcase, tc.errStrFrag)
|
||||||
|
} else if !ip.Equal(tc.expected) {
|
||||||
t.Errorf("case[%v]: expected %v, got %+v .err : %v", tc.tcase, tc.expected, ip, err)
|
t.Errorf("case[%v]: expected %v, got %+v .err : %v", tc.tcase, tc.expected, ip, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -419,17 +440,14 @@ func TestChooseHostInterfaceFromRoute(t *testing.T) {
|
|||||||
nw networkInterfacer
|
nw networkInterfacer
|
||||||
expected net.IP
|
expected net.IP
|
||||||
}{
|
}{
|
||||||
{"valid_routefirst", strings.NewReader(gatewayfirst), validNetworkInterface{}, net.ParseIP("10.254.71.145")},
|
{"ipv4", strings.NewReader(gatewayfirst), validNetworkInterface{}, net.ParseIP("10.254.71.145")},
|
||||||
{"valid_routelast", strings.NewReader(gatewaylast), validNetworkInterface{}, net.ParseIP("10.254.71.145")},
|
{"ipv6", strings.NewReader(gatewaymiddle), ipv6NetworkInterface{}, net.ParseIP("2001::200")},
|
||||||
{"valid_routemiddle", strings.NewReader(gatewaymiddle), validNetworkInterface{}, net.ParseIP("10.254.71.145")},
|
{"no non-link-local ip", strings.NewReader(gatewaymiddle), networkInterfaceWithOnlyLinkLocals{}, nil},
|
||||||
{"valid_routemiddle_ipv6", strings.NewReader(gatewaymiddle), ipv6NetworkInterface{}, nil},
|
{"no routes", strings.NewReader(nothing), validNetworkInterface{}, nil},
|
||||||
{"no internet connection", strings.NewReader(noInternetConnection), validNetworkInterface{}, nil},
|
|
||||||
{"1st ip link-local", strings.NewReader(gatewayfirstLinkLocal), validNetworkInterfaceWithLinkLocal{}, net.ParseIP("45.55.47.146")},
|
|
||||||
{"no route", strings.NewReader(nothing), validNetworkInterface{}, nil},
|
|
||||||
{"no route file", nil, validNetworkInterface{}, nil},
|
{"no route file", nil, validNetworkInterface{}, nil},
|
||||||
{"no interfaces", nil, noNetworkInterface{}, nil},
|
{"no interfaces", nil, noNetworkInterface{}, nil},
|
||||||
{"no interface Addrs", strings.NewReader(gatewaymiddle), networkInterfaceWithNoAddrs{}, nil},
|
{"no interface addrs", strings.NewReader(gatewaymiddle), networkInterfaceWithNoAddrs{}, nil},
|
||||||
{"Invalid Addrs", strings.NewReader(gatewaymiddle), ipv6NetworkInterface{}, nil},
|
{"fail get addrs", strings.NewReader(gatewaymiddle), networkInterfaceFailGetAddrs{}, nil},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
ip, err := chooseHostInterfaceFromRoute(tc.inFile, tc.nw)
|
ip, err := chooseHostInterfaceFromRoute(tc.inFile, tc.nw)
|
||||||
|
Loading…
Reference in New Issue
Block a user