mirror of
https://github.com/clearlinux/rkt.git
synced 2026-11-25 21:42:50 +00:00
Merge pull request #380 from eyakubovich/network-bridge
net: make bridge plugin use ipam module; bug fixes
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"syscall"
|
||||
|
||||
"github.com/appc/spec/schema/types"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink"
|
||||
|
||||
"github.com/coreos/rocket/networking/ipam"
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
)
|
||||
|
||||
@@ -97,96 +97,86 @@ func ensureBridge(brName string, ipn *net.IPNet) (*netlink.Bridge, error) {
|
||||
return br, nil
|
||||
}
|
||||
|
||||
func loadConf(path string) (*Net, error) {
|
||||
conf, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func setupVeth(contID types.UUID, netns string, br *netlink.Bridge, ipn *net.IPNet, ifName string) error {
|
||||
var hostVethName string
|
||||
|
||||
n := &Net{
|
||||
BrName: defaultBrName,
|
||||
}
|
||||
|
||||
err = json.Unmarshal(conf, n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func parseCIDR(s string) (*net.IPNet, error) {
|
||||
ip, ipn, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipn.IP = ip
|
||||
return ipn, nil
|
||||
}
|
||||
|
||||
func cmdAdd(contID, netns, netConf, ifName string) error {
|
||||
conf, err := loadConf(netConf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to load %q: %v", netConf, err)
|
||||
}
|
||||
|
||||
// create bridge if necessary
|
||||
var ipn *net.IPNet
|
||||
if conf.IsGW && conf.IPAlloc.Type == "static" {
|
||||
ipn, err = parseCIDR(conf.IPAlloc.Subnet)
|
||||
err := util.WithNetNSPath(netns, func(hostNS *os.File) error {
|
||||
// create the veth pair in the container and move host end into host netns
|
||||
hostVeth, _, err := util.SetupVeth(contID.String(), ifName, ipn, hostNS)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error parsing ipAlloc.Subnet: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
br, err := ensureBridge(conf.BrName, ipn)
|
||||
|
||||
hostVethName = hostVeth.Attrs().Name
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create bridge %q: %v", conf.BrName, err)
|
||||
}
|
||||
|
||||
// save a handle to current (host) network namespace
|
||||
thisNS, err := os.Open("/proc/self/ns/net")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to open /proc/self/ns/net: %v", err)
|
||||
}
|
||||
|
||||
// switch to the container namespace
|
||||
contNS, err := os.Open(netns)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to open %v: %v", netns, err)
|
||||
}
|
||||
|
||||
if err = util.SetNS(contNS, syscall.CLONE_NEWNET); err != nil {
|
||||
return fmt.Errorf("Error switching to ns %v: %v", netns, err)
|
||||
}
|
||||
|
||||
// create the veth pair in the container and move host end into host netns
|
||||
hostVeth, _, err := util.SetupVeth(contID, ifName, nil, thisNS)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error: %v", err)
|
||||
}
|
||||
|
||||
// switch back to host netns and plug the host veth end into the bridge
|
||||
if err = util.SetNS(thisNS, syscall.CLONE_NEWNET); err != nil {
|
||||
return fmt.Errorf("Error switching to host netns: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// need to lookup hostVeth again as its index has changed during ns move
|
||||
hostVeth, err = netlink.LinkByName(hostVeth.Attrs().Name)
|
||||
hostVeth, err := netlink.LinkByName(hostVethName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to lookup %q: %v", hostVeth.Attrs().Name, err)
|
||||
return fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
|
||||
}
|
||||
|
||||
// connect host veth end to the bridge
|
||||
if err = netlink.LinkSetMaster(hostVeth, br); err != nil {
|
||||
return fmt.Errorf("Failed to connect %q to bridge %q: %v", hostVeth.Attrs().Name, conf.BrName, err)
|
||||
return fmt.Errorf("failed to connect %q to bridge: %v", hostVethName, br.Attrs().Name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdAdd(contID, netns, netConf, ifName string) error {
|
||||
cid, err := types.NewUUID(contID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing ContainerID: %v", err)
|
||||
}
|
||||
|
||||
conf := Net{
|
||||
BrName: defaultBrName,
|
||||
}
|
||||
if err := util.LoadNet(netConf, &conf); err != nil {
|
||||
return fmt.Errorf("failed to load %q: %v", netConf, err)
|
||||
}
|
||||
|
||||
ipn, gw, err := ipam.AllocIP(*cid, netConf, ifName, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var gwn *net.IPNet
|
||||
if conf.IsGW && gw != nil {
|
||||
gwn = &net.IPNet{
|
||||
IP: gw,
|
||||
Mask: ipn.Mask,
|
||||
}
|
||||
}
|
||||
|
||||
// create bridge if necessary
|
||||
br, err := ensureBridge(conf.BrName, gwn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create bridge %q: %v", conf.BrName, err)
|
||||
}
|
||||
|
||||
if err = setupVeth(*cid, netns, br, ipn, ifName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// print to stdout the assigned IP for rkt
|
||||
// TODO(eyakubovich): this will need to be JSON per latest proposal
|
||||
if _, err = fmt.Print(ipn.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdDel(contID, netns, netConf, ifName string) error {
|
||||
// TODO
|
||||
return nil
|
||||
return util.WithNetNSPath(netns, func(hostNS *os.File) error {
|
||||
return util.DelLinkByName(ifName)
|
||||
})
|
||||
}
|
||||
|
||||
func usage() int {
|
||||
@@ -213,7 +203,7 @@ func main() {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
log.Printf("%v: %v", os.Args[1], err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/appc/spec/schema/types"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink"
|
||||
@@ -52,7 +51,7 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error {
|
||||
|
||||
cid, err := types.NewUUID(contID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error parsing ContainerID: %v", err)
|
||||
return fmt.Errorf("error parsing ContainerID: %v", err)
|
||||
}
|
||||
|
||||
ipn, gw, err := ipam.AllocIP(*cid, netConf, ifName, args)
|
||||
@@ -98,26 +97,9 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error {
|
||||
}
|
||||
|
||||
func cmdDel(contID, netns, netConf, ifName, args string) error {
|
||||
// switch to the container namespace
|
||||
contNS, err := os.Open(netns)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to open %v: %v", netns, err)
|
||||
}
|
||||
|
||||
if err = util.SetNS(contNS, syscall.CLONE_NEWNET); err != nil {
|
||||
return fmt.Errorf("Error switching to ns %v: %v", netns, err)
|
||||
}
|
||||
|
||||
iface, err := netlink.LinkByName(ifName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to lookup %q: %v", ifName, err)
|
||||
}
|
||||
|
||||
if err = netlink.LinkDel(iface); err != nil {
|
||||
return fmt.Errorf("Failed to delete %q: %v", ifName, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return util.WithNetNSPath(netns, func(hostNS *os.File) error {
|
||||
return util.DelLinkByName(ifName)
|
||||
})
|
||||
}
|
||||
|
||||
func usage() int {
|
||||
@@ -144,7 +126,7 @@ func main() {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
log.Printf("%v: %v", os.Args[1], err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,3 +79,16 @@ func SetupVeth(entropy, contVethName string, ipn *net.IPNet, hostNS *os.File) (h
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func DelLinkByName(ifName string) error {
|
||||
iface, err := netlink.LinkByName(ifName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to lookup %q: %v", ifName, err)
|
||||
}
|
||||
|
||||
if err = netlink.LinkDel(iface); err != nil {
|
||||
return fmt.Errorf("Failed to delete %q: %v", ifName, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -27,7 +27,7 @@ type Net struct {
|
||||
IPAlloc struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Subnet string `json:"subnet,omitempty"`
|
||||
}
|
||||
} `json:"ipAlloc"`
|
||||
}
|
||||
|
||||
func LoadNet(path string, n interface{}) error {
|
||||
|
||||
+5
-1
@@ -136,7 +136,10 @@ func stage1() int {
|
||||
env = append(env, "LD_LIBRARY_PATH="+filepath.Join(path.Stage1RootfsPath(c.Root), "usr/lib"))
|
||||
|
||||
if privNet {
|
||||
n, err := networking.Setup(c.Manifest.UUID)
|
||||
// careful not to make another local err variable.
|
||||
// cmd.Run sets the one from parent scope
|
||||
var n *networking.Networking
|
||||
n, err = networking.Setup(c.Manifest.UUID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to setup network: %v\n", err)
|
||||
return 6
|
||||
@@ -149,6 +152,7 @@ func stage1() int {
|
||||
}
|
||||
|
||||
cmd := exec.Cmd{
|
||||
Path: args[0],
|
||||
Args: args,
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
|
||||
Reference in New Issue
Block a user