update route handling
This commit is contained in:
parent
88ad00ae93
commit
e55f5694a7
43
main.go
43
main.go
@ -62,7 +62,7 @@ func cmdAdd(args *skel.CmdArgs) error {
|
|||||||
|
|
||||||
// Configure container interface
|
// Configure container interface
|
||||||
containerIP := generateContainerIP(myceliumIP, args.ContainerID)
|
containerIP := generateContainerIP(myceliumIP, args.ContainerID)
|
||||||
if err := configureContainerInterface(containerNS, containerVethName, containerIP, hostVethName); err != nil {
|
if err := configureContainerInterface(containerNS, containerVethName, containerIP, hostVeth); err != nil {
|
||||||
return fmt.Errorf("failed to configure container interface: %v", err)
|
return fmt.Errorf("failed to configure container interface: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,7 +182,7 @@ func createVethPair(hostName, containerName string) (netlink.Link, netlink.Link,
|
|||||||
return hostVeth, containerVeth, nil
|
return hostVeth, containerVeth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureContainerInterface(containerNS netns.NsHandle, ifName string, containerIP net.IP, hostVethName string) error {
|
func configureContainerInterface(containerNS netns.NsHandle, ifName string, containerIP net.IP, hostVeth netlink.Link) error {
|
||||||
// Switch to container namespace
|
// Switch to container namespace
|
||||||
originalNS, err := netns.Get()
|
originalNS, err := netns.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -216,27 +216,23 @@ func configureContainerInterface(containerNS netns.NsHandle, ifName string, cont
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get host veth link-local address for routing
|
// Get host veth link-local address (it should be available now)
|
||||||
hostVeth, err := netlink.LinkByName(hostVethName)
|
hostLinkLocal, err := getHostVethLinkLocal(hostVeth)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
hostAddrs, err := netlink.AddrList(hostVeth, netlink.FAMILY_V6)
|
return fmt.Errorf("failed to get host veth link-local address: %v", err)
|
||||||
if err == nil {
|
}
|
||||||
for _, addr := range hostAddrs {
|
|
||||||
if addr.IP.IsLinkLocalUnicast() {
|
// Add route to Mycelium network via host veth link-local address
|
||||||
// Add route to Mycelium network via host veth
|
|
||||||
route := &netlink.Route{
|
route := &netlink.Route{
|
||||||
Dst: &net.IPNet{
|
Dst: &net.IPNet{
|
||||||
IP: net.ParseIP("400::"),
|
IP: net.ParseIP("400::"),
|
||||||
Mask: net.CIDRMask(7, 128),
|
Mask: net.CIDRMask(7, 128),
|
||||||
},
|
},
|
||||||
Gw: addr.IP,
|
Gw: hostLinkLocal,
|
||||||
LinkIndex: link.Attrs().Index,
|
LinkIndex: link.Attrs().Index,
|
||||||
}
|
}
|
||||||
netlink.RouteAdd(route)
|
if err := netlink.RouteAdd(route); err != nil {
|
||||||
break
|
return fmt.Errorf("failed to add route to 400::/7: %v", err)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -254,3 +250,18 @@ func configureHostInterface(hostVeth netlink.Link, containerIP net.IP) error {
|
|||||||
|
|
||||||
return netlink.RouteAdd(route)
|
return netlink.RouteAdd(route)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getHostVethLinkLocal(hostVeth netlink.Link) (net.IP, error) {
|
||||||
|
addrs, err := netlink.AddrList(hostVeth, netlink.FAMILY_V6)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get addresses for host veth: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if addr.IP.IsLinkLocalUnicast() {
|
||||||
|
return addr.IP, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("no link-local address found on host veth")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user