Skip to content

Commit

Permalink
fix: failed to create pod in two-nic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclinder committed Mar 8, 2023
1 parent 04f0c79 commit d5dcb6f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pkg/networking/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func AddrsToString(addrs []netlink.Addr) []string {
} else {
addr.IPNet.Mask = net.CIDRMask(128, 128)
}
addrStrings = append(addrStrings, addr.String())
addrStrings = append(addrStrings, addr.IPNet.String())
}
return addrStrings
}
Expand Down
68 changes: 38 additions & 30 deletions plugins/veth/veth.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func cmdAdd(args *skel.CmdArgs) error {
logger.Error(err.Error())
return err
}
return nil
}

if err = networking.SysctlRPFilter(netns, conf.RPFilter); err != nil {
Expand All @@ -197,23 +196,24 @@ func cmdCheck(args *skel.CmdArgs) error {
// setupVeth sets up a pair of virtual ethernet devices. move one to the host and other
// one to container.
func setupVeth(netns ns.NetNS, firstInvoke bool, containerID string) (string, error) {
if !firstInvoke {
return getHostVethName(containerID), nil
}
var hostInterface, containerInterface net.Interface
err := netns.Do(func(hostNS ns.NetNS) error {
var err error
if firstInvoke {
hostInterface, containerInterface, err = ip.SetupVethWithName(defaultConVeth, getHostVethName(containerID), defaultMtu, "", hostNS)
if err != nil {
return err
}
hostInterface, containerInterface, err = ip.SetupVethWithName(defaultConVeth, getHostVethName(containerID), defaultMtu, "", hostNS)
if err != nil {
return err
}

link, err := netlink.LinkByName(containerInterface.Name)
if err != nil {
return err
}
link, err := netlink.LinkByName(containerInterface.Name)
if err != nil {
return err
}

if err := netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q UP: %v", containerInterface.Name, err)
}
if err := netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q UP: %v", containerInterface.Name, err)
}
return nil
})
Expand All @@ -234,31 +234,33 @@ func setupNeighborhood(logger *zap.Logger, netns ns.NetNS, hostVethPairName stri
return err
}

logger.Debug("setupNeighborhood",
zap.String("hostVethPairName", hostVethPairName),
zap.String("hostVethHwAddress", hostVethHwAddress.String()),
zap.String("containerVethHwAddress", containerVethHwAddress.String()))

for _, ipAddr := range preInterfaceIPAddress {
if err = networking.AddNeighborTable(hostVethPairName, ipAddr.IP, containerVethHwAddress); err != nil {
logger.Error(err.Error())
return err
}
}

if isfirstInterface {
err = netns.Do(func(_ ns.NetNS) error {
for _, ipAddr := range ipAddressOnNode {
if err := networking.AddNeighborTable(defaultConVeth, ipAddr.IP, hostVethHwAddress); err != nil {
return err
}
if !isfirstInterface {
return nil
}

logger.Debug("setupNeighborhood",
zap.String("hostVethPairName", hostVethPairName),
zap.String("hostVethHwAddress", hostVethHwAddress.String()),
zap.String("containerVethHwAddress", containerVethHwAddress.String()))

err = netns.Do(func(_ ns.NetNS) error {
for _, ipAddr := range ipAddressOnNode {
if err := networking.AddNeighborTable(defaultConVeth, ipAddr.IP, hostVethHwAddress); err != nil {
return err
}
return nil
})
if err != nil {
logger.Error(err.Error())
return err
}
return nil
})
if err != nil {
logger.Error(err.Error())
return err
}

return err
Expand All @@ -268,9 +270,10 @@ func setupNeighborhood(logger *zap.Logger, netns ns.NetNS, hostVethPairName stri
// equivalent to: `ip route add $route`
func setupRoutes(logger *zap.Logger, netns ns.NetNS, ruleTable int, hostVethPairName string, ipAddressOnNode, preInterfaceIPAddress []netlink.Addr, conf *ptypes.Veth) error {
err := netns.Do(func(_ ns.NetNS) error {
var err error
// traffic sent to the node is forwarded via veth0
// eq: "ip r add <ipAddressOnNode> dev veth0 table <ruleTable> "
if err := networking.AddRouteTable(logger, ruleTable, defaultConVeth, networking.AddrsToString(ipAddressOnNode)); err != nil {
if err = networking.AddRouteTable(logger, ruleTable, defaultConVeth, networking.AddrsToString(ipAddressOnNode)); err != nil {
logger.Error("failed to AddRouteTable for ipAddressOnNode", zap.Error(err))
return fmt.Errorf("failed to AddRouteTable for ipAddressOnNode: %v", err)
}
Expand All @@ -296,6 +299,11 @@ func setupRoutes(logger *zap.Logger, netns ns.NetNS, ruleTable int, hostVethPair
logger.Debug("AddRouteTable for localCIDRs successfully", zap.Strings("localCIDRs", localCIDRs))
return nil
})

if err != nil {
return err
}

// set routes for host
// equivalent: ip add <chainedIPs> dev veth-peer on host
if err = networking.AddRouteTable(logger, unix.RT_TABLE_MAIN, hostVethPairName, networking.AddrsToString(preInterfaceIPAddress)); err != nil {
Expand Down

0 comments on commit d5dcb6f

Please sign in to comment.