Skip to content

Commit

Permalink
fix IP/route transfer on node reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Jul 3, 2021
1 parent 5406d70 commit feedaca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 54 deletions.
42 changes: 17 additions & 25 deletions pkg/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
42 changes: 13 additions & 29 deletions pkg/daemon/ovs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit feedaca

Please sign in to comment.