Skip to content

Commit

Permalink
set up tunnel correctly in hybrid mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Jan 27, 2022
1 parent 175d54d commit 0832f5e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
5 changes: 3 additions & 2 deletions cmd/daemon/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -115,15 +115,15 @@ func ParseFlags() (*Configuration, error) {
return nil, err
}

if err := config.initNicConfig(); err != nil {
if err := config.initNicConfig(nicBridgeMappings); err != nil {
return nil, err
}

klog.Infof("daemon config: %v", config)
return config, nil
}

func (config *Configuration) initNicConfig() error {
func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string) error {
var (
iface *net.Interface
err error
Expand Down Expand Up @@ -153,17 +153,23 @@ 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()
if err != nil {
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]
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0832f5e

Please sign in to comment.