From feedaca88ce7344abf15b2a828ad170cd74e4762 Mon Sep 17 00:00:00 2001 From: zhangzujian Date: Thu, 1 Jul 2021 16:58:38 +0800 Subject: [PATCH] fix IP/route transfer on node reboot --- pkg/daemon/init.go | 42 +++++++++++++++++------------------------- pkg/daemon/ovs.go | 42 +++++++++++++----------------------------- 2 files changed, 30 insertions(+), 54 deletions(-) diff --git a/pkg/daemon/init.go b/pkg/daemon/init.go index 7ef7c8f49fd..52a8554a16b 100644 --- a/pkg/daemon/init.go +++ b/pkg/daemon/init.go @@ -64,36 +64,28 @@ func InitVlan(config *Configuration) error { panic("provider should not be empty") } - //create patch port - exists, err := providerBridgeExists() - if err != nil { - errMsg := fmt.Errorf("check provider bridge exists failed, %v", err) + ifName := config.getInterfaceName() + if ifName == "" { + errMsg := fmt.Errorf("failed get host nic to add ovs %s", util.UnderlayBridge) klog.Error(errMsg) - return err + return errMsg } - if !exists { - //create br-provider - if err = configProviderPort(config.DefaultProviderName); err != nil { - errMsg := fmt.Errorf("configure patch port %s failed %v", util.UnderlayBridge, err) - klog.Error(errMsg) - return errMsg - } - - //add a host nic to br-provider - ifName := config.getInterfaceName() - if ifName == "" { - errMsg := fmt.Errorf("failed get host nic to add ovs %s", util.UnderlayBridge) - klog.Error(errMsg) - return errMsg - } + // create and configure external bridge + brName := util.UnderlayBridge + if err := configExternalBridge(config.DefaultProviderName, brName); err != nil { + errMsg := fmt.Errorf("failed to create and configure external bridge %s: %v", brName, err) + klog.Error(errMsg) + return errMsg + } - if err = configProviderNic(ifName); err != nil { - errMsg := fmt.Errorf("add nic %s to port %s failed %v", ifName, util.UnderlayBridge, err) - klog.Error(errMsg) - return errMsg - } + // add host nic to the external bridge + if err := configProviderNic(ifName, brName); err != nil { + errMsg := fmt.Errorf("failed to add nic %s to external bridge %s: %v", ifName, brName, err) + klog.Error(errMsg) + return errMsg } + return nil } diff --git a/pkg/daemon/ovs.go b/pkg/daemon/ovs.go index f789b61d6ee..7eeb883c56d 100644 --- a/pkg/daemon/ovs.go +++ b/pkg/daemon/ovs.go @@ -482,49 +482,33 @@ func configureNic(link, ip string, macAddr net.HardwareAddr, mtu int) error { return nil } -func configProviderPort(providerInterfaceName string) error { - output, err := ovs.Exec(ovs.MayExist, "add-br", util.UnderlayBridge) +func configExternalBridge(provider, bridge string) error { + output, err := ovs.Exec(ovs.MayExist, "add-br", bridge) if err != nil { - return fmt.Errorf("failed to create bridge %s, %v: %q", util.UnderlayBridge, err, output) + return fmt.Errorf("failed to create bridge %s, %v: %q", bridge, err, output) } output, err = ovs.Exec(ovs.IfExists, "get", "open", ".", "external-ids:ovn-bridge-mappings") if err != nil { return fmt.Errorf("failed to get external-ids, %v", err) } - bridgeMappings := fmt.Sprintf("%s:%s", providerInterfaceName, util.UnderlayBridge) - if output != "" && !util.IsStringIn(bridgeMappings, strings.Split(output, ",")) { + + bridgeMappings := fmt.Sprintf("%s:%s", provider, bridge) + if util.IsStringIn(bridgeMappings, strings.Split(output, ",")) { + return nil + } + if output != "" { bridgeMappings = fmt.Sprintf("%s,%s", output, bridgeMappings) } - - output, err = ovs.Exec("set", "open", ".", fmt.Sprintf("external-ids:ovn-bridge-mappings=%s", bridgeMappings)) - if err != nil { + if output, err = ovs.Exec("set", "open", ".", "external-ids:ovn-bridge-mappings="+bridgeMappings); err != nil { return fmt.Errorf("failed to set bridge-mappings, %v: %q", err, output) } return nil } -func providerBridgeExists() (bool, error) { - output, err := ovs.Exec("list-br") - if err != nil { - klog.Errorf("failed to list bridge %v", err) - return false, err - } - - lines := strings.Split(output, "\n") - for _, l := range lines { - if l == util.UnderlayBridge { - return true, nil - } - } - - return false, nil -} - -// Add host nic to br-provider -// MAC, MTU, IP addresses & routes will be copied/transferred to br-provider -func configProviderNic(nicName string) error { - brName := util.UnderlayBridge +// Add host nic to external bridge +// Mac address, MTU, IP addresses & routes will be copied/transferred to the external bridge +func configProviderNic(nicName, brName string) error { nic, err := netlink.LinkByName(nicName) if err != nil { return fmt.Errorf("failed to get nic by name %s: %v", nicName, err)