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
|
||||
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)
|
||||
}
|
||||
|
||||
@ -182,7 +182,7 @@ func createVethPair(hostName, containerName string) (netlink.Link, netlink.Link,
|
||||
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
|
||||
originalNS, err := netns.Get()
|
||||
if err != nil {
|
||||
@ -216,27 +216,23 @@ func configureContainerInterface(containerNS netns.NsHandle, ifName string, cont
|
||||
return err
|
||||
}
|
||||
|
||||
// Get host veth link-local address for routing
|
||||
hostVeth, err := netlink.LinkByName(hostVethName)
|
||||
if err == nil {
|
||||
hostAddrs, err := netlink.AddrList(hostVeth, netlink.FAMILY_V6)
|
||||
if err == nil {
|
||||
for _, addr := range hostAddrs {
|
||||
if addr.IP.IsLinkLocalUnicast() {
|
||||
// Add route to Mycelium network via host veth
|
||||
// Get host veth link-local address (it should be available now)
|
||||
hostLinkLocal, err := getHostVethLinkLocal(hostVeth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get host veth link-local address: %v", err)
|
||||
}
|
||||
|
||||
// Add route to Mycelium network via host veth link-local address
|
||||
route := &netlink.Route{
|
||||
Dst: &net.IPNet{
|
||||
IP: net.ParseIP("400::"),
|
||||
Mask: net.CIDRMask(7, 128),
|
||||
},
|
||||
Gw: addr.IP,
|
||||
Gw: hostLinkLocal,
|
||||
LinkIndex: link.Attrs().Index,
|
||||
}
|
||||
netlink.RouteAdd(route)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := netlink.RouteAdd(route); err != nil {
|
||||
return fmt.Errorf("failed to add route to 400::/7: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -254,3 +250,18 @@ func configureHostInterface(hostVeth netlink.Link, containerIP net.IP) error {
|
||||
|
||||
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