Skip to content

Commit e8f32ac

Browse files
committed
fix encap ip when the tunnel interface has multiple addresses (#2340)
1 parent c0c9c71 commit e8f32ac

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

pkg/daemon/config.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string)
198198
klog.Errorf("failed to find iface %s, %v", tunnelNic, err)
199199
return err
200200
}
201+
srcIPs, err := getSrcIPsByRoutes(iface)
202+
if err != nil {
203+
return fmt.Errorf("failed to get src IPs by routes on interface %s: %v", iface.Name, err)
204+
}
201205
addrs, err := iface.Addrs()
202206
if err != nil {
203207
return fmt.Errorf("failed to get iface addr. %v", err)
@@ -207,14 +211,16 @@ func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string)
207211
if ip := net.ParseIP(ipStr); ip == nil || ip.IsLinkLocalUnicast() {
208212
continue
209213
}
210-
encapIP = ipStr
211-
break
214+
if len(srcIPs) == 0 || util.ContainsString(srcIPs, ipStr) {
215+
encapIP = ipStr
216+
break
217+
}
212218
}
213219
if len(encapIP) == 0 {
214220
return fmt.Errorf("iface %s has no valid IP address", tunnelNic)
215221
}
216222

217-
klog.Infof("use %s as tunnel interface", iface.Name)
223+
klog.Infof("use %s on %s as tunnel address", encapIP, iface.Name)
218224
mtu = iface.MTU
219225
config.tunnelIface = iface.Name
220226
}

pkg/daemon/config_linux.go

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ import (
1010

1111
const defaultBindSocket = "/run/openvswitch/kube-ovn-daemon.sock"
1212

13+
func getSrcIPsByRoutes(iface *net.Interface) ([]string, error) {
14+
link, err := netlink.LinkByName(iface.Name)
15+
if err != nil {
16+
return nil, fmt.Errorf("failed to get link %s: %v", iface.Name, err)
17+
}
18+
routes, err := netlink.RouteList(link, netlink.FAMILY_ALL)
19+
if err != nil {
20+
return nil, fmt.Errorf("failed to get routes on link %s: %v", iface.Name, err)
21+
}
22+
23+
srcIPs := make([]string, 0, 2)
24+
for _, r := range routes {
25+
if r.Src != nil && r.Scope == netlink.SCOPE_LINK {
26+
srcIPs = append(srcIPs, r.Src.String())
27+
}
28+
}
29+
return srcIPs, nil
30+
}
31+
1332
func getIfaceByIP(ip string) (string, int, error) {
1433
links, err := netlink.LinkList()
1534
if err != nil {

pkg/daemon/config_windows.go

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package daemon
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"strings"
78

89
"github.com/Microsoft/hcsshim"
@@ -16,6 +17,11 @@ import (
1617

1718
const defaultBindSocket = util.WindowsListenPipe
1819

20+
func getSrcIPsByRoutes(iface *net.Interface) ([]string, error) {
21+
// to be implemented in the future
22+
return nil, nil
23+
}
24+
1925
func getIfaceByIP(ip string) (string, int, error) {
2026
iface, err := util.GetInterfaceByIP(ip)
2127
if err != nil {

0 commit comments

Comments
 (0)