Move setting up addresses for a container, into weaveutil-invoked function

This means we only change netns by execing nsenter: changing netns in
Go is unsafe for long-running processes like the proxy.
This commit is contained in:
Bryan Boreham committed 2017-06-16 13:19:08 +00:00
1 parent 97e0fcb765
commit 28aa181480
3 files changed
+40 -13

No files matched your search

+19 -13
View File
@@ -104,8 +104,6 @@ func interfaceExistsInNamespace(netNSPath string, ifName string) bool {
return err == nil return err == nil
} }
// NB: This function can be used only by a process that terminates immediately
// after calling the function as it changes netns via WithNetNSLinkUnsafe.
func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMulticastRoute bool, cidrs []*net.IPNet, keepTXOn bool, hairpinMode bool) error { func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMulticastRoute bool, cidrs []*net.IPNet, keepTXOn bool, hairpinMode bool) error {
ns, err := netns.GetFromPath(netNSPath) ns, err := netns.GetFromPath(netNSPath)
if err != nil { if err != nil {
@@ -113,11 +111,6 @@ func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMult
} }
defer ns.Close() defer ns.Close()
ipt, err := iptables.New()
if err != nil {
return err
}
if !interfaceExistsInNamespace(netNSPath, ifName) { if !interfaceExistsInNamespace(netNSPath, ifName) {
maxIDLen := IFNAMSIZ - 1 - len(vethPrefix+"pl") maxIDLen := IFNAMSIZ - 1 - len(vethPrefix+"pl")
if len(id) > maxIDLen { if len(id) > maxIDLen {
@@ -142,12 +135,30 @@ func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMult
} }
if err := WithNetNSLinkUnsafe(ns, ifName, func(veth netlink.Link) error { args := []string{ifName, fmt.Sprintf("%t", withMulticastRoute)}
for _, cidr := range cidrs {
args = append(args, cidr.String())
}
if _, err := WithNetNS(netNSPath, "setup-iface-addrs", args...); err != nil {
return fmt.Errorf("error setting up interface addresses: %s", err)
}
return nil
}
// SetupIfaceAddrs is the implementation of the 'setup-iface-addrs' call above,
// running in another process in the container's netns
func SetupIfaceAddrs(veth netlink.Link, withMulticastRoute bool, cidrs []*net.IPNet) error {
newAddresses, err := AddAddresses(veth, cidrs) newAddresses, err := AddAddresses(veth, cidrs)
if err != nil { if err != nil {
return err return err
} }
ifName := veth.Attrs().Name
ipt, err := iptables.New()
if err != nil {
return err
}
// Add multicast ACCEPT rules for new subnets // Add multicast ACCEPT rules for new subnets
for _, ipnet := range newAddresses { for _, ipnet := range newAddresses {
acceptRule := []string{"-i", ifName, "-s", subnet(ipnet), "-d", "224.0.0.0/4", "-j", "ACCEPT"} acceptRule := []string{"-i", ifName, "-s", subnet(ipnet), "-d", "224.0.0.0/4", "-j", "ACCEPT"}
@@ -185,11 +196,6 @@ func AttachContainer(netNSPath, id, ifName, bridgeName string, mtu int, withMult
return err return err
} }
} }
return nil
}); err != nil {
return err
}
return nil return nil
} }
+1
View File
@@ -32,6 +32,7 @@ func init() {
"check-iface": checkIface, "check-iface": checkIface,
"del-iface": delIface, "del-iface": delIface,
"setup-iface": setupIface, "setup-iface": setupIface,
"setup-iface-addrs": setupIfaceAddrs,
"list-netdevs": listNetDevs, "list-netdevs": listNetDevs,
"cni-net": cniNet, "cni-net": cniNet,
"cni-ipam": cniIPAM, "cni-ipam": cniIPAM,
+20
View File
@@ -50,6 +50,26 @@ func setupIface(args []string) error {
return weavenet.SetupIface(ifaceName, newIfName) return weavenet.SetupIface(ifaceName, newIfName)
} }
// setupIfaceAddrs sets up addresses on an interface. It expects to be called inside the container's netns.
func setupIfaceAddrs(args []string) error {
if len(args) < 1 {
cmdUsage("setup-iface-addrs", "<iface-name> <with-multicast> <cidr>...")
}
link, err := netlink.LinkByName(args[0])
if err != nil {
return err
}
withMulticastRoute, err := strconv.ParseBool(args[1])
if err != nil {
return err
}
cidrs, err := parseCIDRs(args[2:])
if err != nil {
return err
}
return weavenet.SetupIfaceAddrs(link, withMulticastRoute, cidrs)
}
func configureARP(args []string) error { func configureARP(args []string) error {
if len(args) != 2 { if len(args) != 2 {
cmdUsage("configure-arp", "<iface-name-prefix> <root-path>") cmdUsage("configure-arp", "<iface-name-prefix> <root-path>")