Merge pull request #385 from eyakubovich/network-routes

net: minimal support for routes
This commit is contained in:
Eugene Yakubovich
2015-01-16 11:26:41 -08:00
3 changed files with 44 additions and 39 deletions
+26 -20
View File
@@ -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
}
+14 -16
View File
@@ -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
}
+4 -3
View File
@@ -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 {