From b267c9d75198eea5195a026c484aafaacca04c7a Mon Sep 17 00:00:00 2001 From: Eugene Yakubovich Date: Thu, 15 Jan 2015 16:34:18 -0800 Subject: [PATCH] net: minimal support for routes This also sorts nets on filename during load so routes get installed in well defined order. Additionally, it no longer hard codes default net and assumes something like 99-network.conf present to define default network. --- networking/net.go | 46 +++++++++++++++++++-------------- networking/plugins/veth/veth.go | 30 ++++++++++----------- networking/util/net.go | 7 ++--- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/networking/net.go b/networking/net.go index 961a37c..dd1e9de 100644 --- a/networking/net.go +++ b/networking/net.go @@ -15,10 +15,11 @@ package networking import ( + "fmt" "io/ioutil" - "log" "os" "path" + "sort" "github.com/coreos/rocket/networking/util" ) @@ -29,17 +30,8 @@ type Net struct { } const RktNetPath = "/etc/rkt-net.conf.d" -const DefaultIPNet = "172.16.28.0/24" -var defaultNet = Net{ - Net: util.Net{ - Name: "default", - Type: "veth", - }, - args: "default,iprange=" + DefaultIPNet, -} - -func LoadNets() ([]Net, error) { +func listFiles(dir string) ([]string, error) { dirents, err := ioutil.ReadDir(RktNetPath) switch { case err == nil: @@ -49,23 +41,37 @@ func LoadNets() ([]Net, error) { return nil, err } - var nets []Net - + files := []string{} for _, dent := range dirents { if dent.IsDir() { continue } - nf := path.Join(RktNetPath, dent.Name()) + files = append(files, dent.Name()) + } + + return files, nil +} + +func LoadNets() ([]Net, error) { + files, err := listFiles(RktNetPath) + if err != nil { + return nil, err + } + + sort.Strings(files) + + nets := make([]Net, 0, len(files)) + + for _, filename := range files { + filepath := path.Join(RktNetPath, filename) n := Net{} - if err := util.LoadNet(nf, &n); err != nil { - log.Printf("Error loading %v: %v", nf, err) - continue + if err := util.LoadNet(filepath, &n); err != nil { + return nil, fmt.Errorf("error loading %v: %v", filepath, err) } nets = append(nets, n) } - nets = append(nets, defaultNet) - - return nets, nil } + return nets, nil +} diff --git a/networking/plugins/veth/veth.go b/networking/plugins/veth/veth.go index 5d6ec13..43796b7 100644 --- a/networking/plugins/veth/veth.go +++ b/networking/plugins/veth/veth.go @@ -20,7 +20,6 @@ import ( "net" "os" "runtime" - "strings" "github.com/appc/spec/schema/types" "github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink" @@ -36,16 +35,6 @@ func init() { runtime.LockOSThread() } -func argsHasDefault(args string) bool { - argv := strings.Split(args, ",") - for _, a := range argv { - if a == "default" { - return true - } - } - return false -} - func cmdAdd(contID, netns, netConf, ifName, args string) error { var hostVethName string @@ -54,6 +43,11 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error { return fmt.Errorf("error parsing ContainerID: %v", err) } + conf := util.Net{} + 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, args) if err != nil { return err @@ -67,9 +61,14 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error { return err } - if argsHasDefault(args) { - if err = util.AddDefaultRoute(gw, contVeth); err != nil { - return fmt.Errorf("failed to add default route: %v", err) + for _, r := range conf.Routes { + dst, err := util.ParseCIDR(r) + if err != nil { + return err + } + + if err = util.AddRoute(dst, gw, contVeth); err != nil { + return fmt.Errorf("failed to add route %q: %v", dst, err) } } @@ -83,7 +82,6 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error { return fmt.Errorf("failed to lookup %q: %v", hostVeth.Attrs().Name, err) } - // On the host we route traffic for the allocated IP to the container ipn.Mask = net.CIDRMask(32, 32) @@ -91,7 +89,7 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error { return fmt.Errorf("failed to add route on host: %v", err) } - os.Stdout.Write([]byte(ipn.String())) + fmt.Print(ipn.String()) return nil } diff --git a/networking/util/net.go b/networking/util/net.go index b58f922..98c2310 100644 --- a/networking/util/net.go +++ b/networking/util/net.go @@ -22,12 +22,13 @@ import ( type Net struct { Filename string - Name string `json:"name,omitempty"` - Type string `json:"type,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` IPAlloc struct { Type string `json:"type,omitempty"` Subnet string `json:"subnet,omitempty"` - } `json:"ipAlloc"` + } `json:"ipAlloc,omitempty"` + Routes []string `json:"routes,omitempty"` } func LoadNet(path string, n interface{}) error {