From 0832f5efa6e7c601a695f85621bd1ace664c6604 Mon Sep 17 00:00:00 2001 From: zhangzujian Date: Thu, 27 Jan 2022 10:53:52 +0800 Subject: [PATCH] set up tunnel correctly in hybrid mode --- cmd/daemon/cniserver.go | 5 +++-- pkg/daemon/config.go | 18 ++++++++++++------ pkg/daemon/init.go | 18 ++++++++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cmd/daemon/cniserver.go b/cmd/daemon/cniserver.go index 26cc51ff39c..2008765a028 100644 --- a/cmd/daemon/cniserver.go +++ b/cmd/daemon/cniserver.go @@ -28,11 +28,12 @@ func CmdMain() { klog.Infof(versions.String()) daemon.InitMetrics() - if err := daemon.InitOVSBridges(); err != nil { + nicBridgeMappings, err := daemon.InitOVSBridges() + if err != nil { klog.Fatalf("failed to initialize OVS bridges: %v", err) } - config, err := daemon.ParseFlags() + config, err := daemon.ParseFlags(nicBridgeMappings) if err != nil { klog.Fatalf("parse config failed %v", err) } diff --git a/pkg/daemon/config.go b/pkg/daemon/config.go index 292c12e9363..943178e7a35 100644 --- a/pkg/daemon/config.go +++ b/pkg/daemon/config.go @@ -48,7 +48,7 @@ type Configuration struct { // ParseFlags will parse cmd args then init kubeClient and configuration // TODO: validate configuration -func ParseFlags() (*Configuration, error) { +func ParseFlags(nicBridgeMappings map[string]string) (*Configuration, error) { var ( argIface = pflag.String("iface", "", "The iface used to inter-host pod communication, can be a nic name or a group of regex separated by comma, default: the default route iface") argMTU = pflag.Int("mtu", 0, "The MTU used by pod iface in overlay networks, default: iface MTU - 100") @@ -115,7 +115,7 @@ func ParseFlags() (*Configuration, error) { return nil, err } - if err := config.initNicConfig(); err != nil { + if err := config.initNicConfig(nicBridgeMappings); err != nil { return nil, err } @@ -123,7 +123,7 @@ func ParseFlags() (*Configuration, error) { return config, nil } -func (config *Configuration) initNicConfig() error { +func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string) error { var ( iface *net.Interface err error @@ -153,9 +153,15 @@ func (config *Configuration) initNicConfig() error { } encapIP = podIP } else { - iface, err = findInterface(config.Iface) + tunnelNic := config.Iface + if brName := nicBridgeMappings[tunnelNic]; brName != "" { + klog.Infof("nic %s has been bridged to %s, use %s as the tunnel interface instead", tunnelNic, brName, brName) + tunnelNic = brName + } + + iface, err = findInterface(tunnelNic) if err != nil { - klog.Errorf("failed to find iface %s, %v", config.Iface, err) + klog.Errorf("failed to find iface %s, %v", tunnelNic, err) return err } addrs, err := iface.Addrs() @@ -163,7 +169,7 @@ func (config *Configuration) initNicConfig() error { return fmt.Errorf("failed to get iface addr. %v", err) } if len(addrs) == 0 { - return fmt.Errorf("iface %s has no ip address", config.Iface) + return fmt.Errorf("iface %s has no ip address", tunnelNic) } encapIP = strings.Split(addrs[0].String(), "/")[0] } diff --git a/pkg/daemon/init.go b/pkg/daemon/init.go index da4a5f3513e..253da7762bc 100644 --- a/pkg/daemon/init.go +++ b/pkg/daemon/init.go @@ -16,42 +16,44 @@ import ( ) // InitOVSBridges initializes OVS bridges -func InitOVSBridges() error { +func InitOVSBridges() (map[string]string, error) { bridges, err := ovs.Bridges() if err != nil { - return err + return nil, err } + mappings := make(map[string]string) for _, brName := range bridges { bridge, err := netlink.LinkByName(brName) if err != nil { - return fmt.Errorf("failed to get bridge by name %s: %v", brName, err) + return nil, fmt.Errorf("failed to get bridge by name %s: %v", brName, err) } if err = netlink.LinkSetUp(bridge); err != nil { - return fmt.Errorf("failed to set OVS bridge %s up: %v", brName, err) + return nil, fmt.Errorf("failed to set OVS bridge %s up: %v", brName, err) } output, err := ovs.Exec("list-ports", brName) if err != nil { - return fmt.Errorf("failed to list ports of OVS birdge %s, %v: %q", brName, err, output) + return nil, fmt.Errorf("failed to list ports of OVS bridge %s, %v: %q", brName, err, output) } if output != "" { for _, port := range strings.Split(output, "\n") { ok, err := ovs.ValidatePortVendor(port) if err != nil { - return fmt.Errorf("failed to check vendor of port %s: %v", port, err) + return nil, fmt.Errorf("failed to check vendor of port %s: %v", port, err) } if ok { if _, err = configProviderNic(port, brName); err != nil { - return err + return nil, err } + mappings[port] = brName } } } } - return nil + return mappings, nil } // InitNodeGateway init ovn0