From c179722f7eb2eb84268aebec423fd9970ebfffba Mon Sep 17 00:00:00 2001 From: VZ Cambria Date: Wed, 15 Feb 2017 12:13:57 -0500 Subject: [PATCH] Add kernel route on uplink --- .../vz-local/backend/allocator/allocator.go | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/plugins/ipam/vz-local/backend/allocator/allocator.go b/plugins/ipam/vz-local/backend/allocator/allocator.go index c43235e..abf9951 100644 --- a/plugins/ipam/vz-local/backend/allocator/allocator.go +++ b/plugins/ipam/vz-local/backend/allocator/allocator.go @@ -23,6 +23,10 @@ import ( "github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/types/current" "github.com/containernetworking/cni/plugins/ipam/host-local/backend" + + "github.com/vishvananda/netlink" + "os" + "syscall" ) type IPAllocator struct { @@ -166,6 +170,43 @@ func (a *IPAllocator) Get(id string) (*current.IPConfig, []*types.Route, error) } if reserved { + + var uplink types.UnmarshallableString + if a.conf.Args != nil { + uplink = a.conf.Args.UPLINK + log.Println("AdvRoute: BGP Advertises /32", requestedIP, uplink) + + dst := &net.IPNet{ + IP: requestedIP, + Mask: net.CIDRMask(32, 32), + } + + link, err := netlink.LinkByName(string(uplink)) + if err != nil { + log.Println("Can't obtain link index for: ", uplink) + return nil, nil, err + } + + //gw := advertise_route + route := netlink.Route{ + Dst: dst, + LinkIndex: link.Attrs().Index, + // Gw: gw + } + + if err := netlink.RouteAdd(&route); err != nil { + fmt.Fprintln(os.Stderr, "There was an error adding netlink route: ", err) + if (err == syscall.EAGAIN) { + log.Println("ERRNO: eagain") + } else if (err == syscall.EEXIST) { + log.Println("ERRNO: route already exists") + } else { + log.Println("ERRNO: value is: ", (int(err.(syscall.Errno)))) + } + return nil, nil, err + } + } + ipConfig := ¤t.IPConfig{ Version: "4", Address: net.IPNet{IP: requestedIP, Mask: a.conf.Subnet.Mask}, @@ -210,6 +251,56 @@ func (a *IPAllocator) Release(id string) error { a.store.Lock() defer a.store.Unlock() + //var advertise_route string + //var advertise_route net.IP + var requestedIP net.IP + + if a.conf.Args != nil { + requestedIP = a.conf.Args.IP + } + + if requestedIP != nil { + + //var uplink string + var uplink types.UnmarshallableString + if a.conf.Args != nil { + uplink = a.conf.Args.UPLINK + log.Println("AdvRoute: BGP Advertises /32", requestedIP, uplink) + + dst := &net.IPNet{ + // IP: net.IPv4(99, 44, 69, 3), + IP: requestedIP, + Mask: net.CIDRMask(32, 32), + } + + link, err := netlink.LinkByName(string(uplink)) + if err != nil { + log.Println("Can't obtain link index for: ", uplink) + return err + } + + // gw := advertise_route + route := netlink.Route{ + Dst: dst, + LinkIndex: link.Attrs().Index, + //Gw: gw + } + + if err := netlink.RouteDel(&route); err != nil { + fmt.Fprintln(os.Stderr, "There was an error removing netlink route: ", err) + if (err == syscall.EAGAIN) { + log.Println("ERRNO: eagain") + } else if (err == syscall.ESRCH) { + log.Println("ERRNO: route doesn't exists") + } else { + log.Println("ERRNO: value is: ", (int(err.(syscall.Errno)))) + } + // os.Exit(1) + return err + } + } + } + return a.store.ReleaseByID(id) } -- 2.44.0