mirror of
https://github.com/clearlinux/rkt.git
synced 2026-11-26 06:01:12 +00:00
Merge pull request #458 from eyakubovich/ipam-static
net: cnvt ipmanager to ipam plugin; make use of ipam plugins
This commit is contained in:
@@ -14,10 +14,12 @@ export GOPATH=${GOPATH:-}:${PWD}/gopath
|
||||
eval $(go env)
|
||||
|
||||
echo "Building network plugins"
|
||||
for d in networking/plugins/*; do
|
||||
plugin=$(basename $d)
|
||||
echo " " $plugin
|
||||
go install ${REPO_PATH}/$d
|
||||
for d in networking/net/* networking/ipam/*; do
|
||||
if [ -d $d ]; then
|
||||
plugin=$(basename $d)
|
||||
echo " " $plugin
|
||||
go install ${REPO_PATH}/$d
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Building rkt (stage0)..."
|
||||
@@ -29,9 +31,13 @@ if [[ "$OSTYPE" == "linux-gnu" ]]; then
|
||||
|
||||
# symlink plugin binaries into stage1 rootfs
|
||||
mkdir -p stage1/rootfs/net-plugins/bin
|
||||
for d in networking/plugins/*; do
|
||||
plugin=$(basename $d)
|
||||
ln -sf ${GOBIN}/$plugin stage1/rootfs/net-plugins/bin
|
||||
# make sure the bin dir doesn't have old links
|
||||
rm -f stage1/rootfs/net-plugins/bin/*
|
||||
for d in networking/net/* networking/ipam/*; do
|
||||
if [ -d $d ]; then
|
||||
plugin=$(basename $d)
|
||||
ln -sf ${GOBIN}/$plugin stage1/rootfs/net-plugins/bin
|
||||
fi
|
||||
done
|
||||
|
||||
# symlink init into stage1 rootfs
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
|
||||
"github.com/coreos/rocket/ipmanager/backend"
|
||||
)
|
||||
|
||||
type Allocation struct {
|
||||
DomainName string `json:"domain-name,omitempty"`
|
||||
DomainNameServers []string `json:"domain-name-servers,omitempty"`
|
||||
IP string `json:"ip"`
|
||||
Routers []string `json:"routers,omitempty"`
|
||||
}
|
||||
|
||||
type IPAllocator struct {
|
||||
start net.IP
|
||||
end net.IP
|
||||
last net.IP
|
||||
ipnet *net.IPNet
|
||||
netconf *NetworkConfig
|
||||
store backend.Store
|
||||
}
|
||||
|
||||
func NewIPAllocator(netconf *NetworkConfig, store backend.Store) (*IPAllocator, error) {
|
||||
var (
|
||||
start net.IP
|
||||
end net.IP
|
||||
err error
|
||||
)
|
||||
_, ipnet, err := net.ParseCIDR(netconf.Subnet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
start, end = networkRange(ipnet)
|
||||
start = nextIP(start)
|
||||
end = prevIP(end)
|
||||
|
||||
if netconf.RangeStart != "" {
|
||||
start, err = validateRangeIP(netconf.RangeStart, ipnet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if netconf.RangeEnd != "" {
|
||||
end, err = validateRangeIP(netconf.RangeEnd, ipnet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &IPAllocator{start, end, prevIP(start), ipnet, netconf, store}, nil
|
||||
}
|
||||
|
||||
func validateRangeIP(s string, ipnet *net.IPNet) (net.IP, error) {
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("invalid ip address: %s", s)
|
||||
}
|
||||
if !ipnet.Contains(ip) {
|
||||
return nil, fmt.Errorf("%s not in network: %s", s, ipnet)
|
||||
}
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
func (a *IPAllocator) Get(id string) (*Allocation, error) {
|
||||
ip := a.nextIP()
|
||||
seen := newIP(ip)
|
||||
for {
|
||||
reserved, err := a.store.Reserve(a.netconf.Name, id, ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reserved {
|
||||
break
|
||||
}
|
||||
ip = a.nextIP()
|
||||
if seen.Equal(ip) {
|
||||
return nil, fmt.Errorf("no ip addresses available in network: %s", a.netconf.Name)
|
||||
}
|
||||
}
|
||||
|
||||
ipnet := net.IPNet{ip, a.ipnet.Mask}
|
||||
alloc := &Allocation{
|
||||
IP: ipnet.String(),
|
||||
Routers: a.netconf.Routers,
|
||||
DomainNameServers: a.netconf.DomainNameServers,
|
||||
DomainName: a.netconf.DomainName,
|
||||
}
|
||||
return alloc, nil
|
||||
}
|
||||
|
||||
func (a *IPAllocator) nextIP() net.IP {
|
||||
if a.last.Equal(a.end) {
|
||||
a.last = a.start
|
||||
return a.start
|
||||
}
|
||||
a.last = nextIP(a.last)
|
||||
return a.last
|
||||
}
|
||||
|
||||
func ipToInt(ip net.IP) *big.Int {
|
||||
if v := ip.To4(); v != nil {
|
||||
return big.NewInt(0).SetBytes(v)
|
||||
}
|
||||
return big.NewInt(0).SetBytes(ip.To16())
|
||||
}
|
||||
|
||||
func intToIP(i *big.Int) net.IP {
|
||||
return net.IP(i.Bytes())
|
||||
}
|
||||
|
||||
func newIP(ip net.IP) net.IP {
|
||||
n := make(net.IP, len(ip))
|
||||
copy(n, ip)
|
||||
return n
|
||||
}
|
||||
|
||||
func nextIP(ip net.IP) net.IP {
|
||||
i := ipToInt(ip)
|
||||
return intToIP(i.Add(i, big.NewInt(1)))
|
||||
}
|
||||
|
||||
func prevIP(ip net.IP) net.IP {
|
||||
i := ipToInt(ip)
|
||||
return intToIP(i.Sub(i, big.NewInt(1)))
|
||||
}
|
||||
|
||||
func networkRange(ipnet *net.IPNet) (net.IP, net.IP) {
|
||||
var end net.IP
|
||||
for i := 0; i < len(ipnet.IP); i++ {
|
||||
end = append(end, ipnet.IP[i]|^ipnet.Mask[i])
|
||||
}
|
||||
return ipnet.IP, end
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package backend
|
||||
|
||||
import "net"
|
||||
|
||||
type Store interface {
|
||||
Reserve(network, id string, ip net.IP) (bool, error)
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var defaultConfigDir = "/etc/rkt/net.d"
|
||||
|
||||
// NetworkConfig represents an ipmanager network configuration.
|
||||
type NetworkConfig struct {
|
||||
DomainName string `json:"domain-name"`
|
||||
DomainNameServers []string `json:"domain-name-servers"`
|
||||
Name string `json:"name"`
|
||||
RangeStart string `json:"range-start"`
|
||||
RangeEnd string `json:"range-end"`
|
||||
Routers []string `json:"routers"`
|
||||
Subnet string `json:"subnet"`
|
||||
}
|
||||
|
||||
// NewNetworkConfig creates a NetworkConfig from the given network name.
|
||||
func NewNetworkConfig(name string) (*NetworkConfig, error) {
|
||||
p := filepath.Join(defaultConfigDir, fmt.Sprintf("%s.conf", name))
|
||||
var c NetworkConfig
|
||||
data, err := ioutil.ReadFile(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(data, &c)
|
||||
return &c, err
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
|
||||
"github.com/coreos/rocket/ipmanager/backend"
|
||||
"github.com/coreos/rocket/ipmanager/backend/disk"
|
||||
)
|
||||
|
||||
var (
|
||||
containerID string
|
||||
networkName string
|
||||
store backend.Store
|
||||
)
|
||||
|
||||
func init() {
|
||||
// this ensures that main runs only on main thread (thread group leader).
|
||||
// since namespace ops (unshare, setns) are done for a single thread, we
|
||||
// must ensure that the goroutine does not jump from OS thread to thread
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
flag.StringVar(&containerID, "c", "", "container id")
|
||||
flag.StringVar(&networkName, "n", "default", "network name")
|
||||
flag.Parse()
|
||||
conf, err := NewNetworkConfig(networkName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
store, err = disk.New()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
allocator, err := NewIPAllocator(conf, store)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
allocation, err := allocator.Get(containerID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
data, err := json.MarshalIndent(&allocation, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
+106
-137
@@ -1,169 +1,138 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ipam
|
||||
|
||||
// ATTN: This is mostly throw away code. It'll be replaced
|
||||
// by proper ip mgt plugins.
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/appc/spec/schema/types"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink"
|
||||
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
ipRange *net.IPNet
|
||||
ip net.IP
|
||||
// L3 config value for interface
|
||||
type IPConfig struct {
|
||||
IP *net.IPNet
|
||||
Gateway net.IP
|
||||
Routes []net.IPNet
|
||||
}
|
||||
|
||||
func ipAdd(ip net.IP, val uint) net.IP {
|
||||
n := binary.BigEndian.Uint32(ip.To4())
|
||||
n += uint32(val)
|
||||
|
||||
nip := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(nip, n)
|
||||
return net.IP(nip)
|
||||
type ipConfig struct {
|
||||
IP string `json:"ip"`
|
||||
Gateway string `json:"gateway,omitempty"`
|
||||
Routes []string `json:"routes,omitempty"`
|
||||
}
|
||||
|
||||
func allocIP(ipn *net.IPNet) (net.IP, error) {
|
||||
ones, bits := ipn.Mask.Size()
|
||||
zeros := bits - ones
|
||||
rng := (1 << uint(zeros)) - 2 // (reduce for gw, bcast)
|
||||
func (c *IPConfig) UnmarshalJSON(data []byte) error {
|
||||
ipc := ipConfig{}
|
||||
if err := json.Unmarshal(data, &ipc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(rng)))
|
||||
ip, err := util.ParseCIDR(ipc.IP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var gw net.IP
|
||||
if ipc.Gateway != "" {
|
||||
if gw = net.ParseIP(ipc.Gateway); gw == nil {
|
||||
return fmt.Errorf("error parsing Gateway")
|
||||
}
|
||||
}
|
||||
|
||||
routes := []net.IPNet{}
|
||||
|
||||
for _, r := range ipc.Routes {
|
||||
dst, err := util.ParseCIDR(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
routes = append(routes, *dst)
|
||||
}
|
||||
|
||||
c.IP = ip
|
||||
c.Gateway = gw
|
||||
c.Routes = routes
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *IPConfig) MarshalJSON() ([]byte, error) {
|
||||
ipc := ipConfig{
|
||||
IP: c.IP.String(),
|
||||
}
|
||||
|
||||
if c.Gateway != nil {
|
||||
ipc.Gateway = c.Gateway.String()
|
||||
}
|
||||
|
||||
for _, dst := range c.Routes {
|
||||
ipc.Routes = append(ipc.Routes, dst.String())
|
||||
}
|
||||
|
||||
return json.Marshal(ipc)
|
||||
}
|
||||
|
||||
func findIPAMPlugin(plugin string) string {
|
||||
// try 3rd-party path first
|
||||
paths := strings.Split(os.Getenv("RKT_NETPLUGIN_IPAMPATH"), ":")
|
||||
|
||||
for _, p := range paths {
|
||||
fullname := filepath.Join(p, plugin)
|
||||
if fi, err := os.Stat(fullname); err == nil && fi.Mode().IsRegular() {
|
||||
return fullname
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func ExecPlugin(plugin string) (*IPConfig, error) {
|
||||
pluginPath := findIPAMPlugin(plugin)
|
||||
if pluginPath == "" {
|
||||
return nil, fmt.Errorf("could not find %q plugin", plugin)
|
||||
}
|
||||
|
||||
stdout := &bytes.Buffer{}
|
||||
|
||||
c := exec.Cmd{
|
||||
Path: pluginPath,
|
||||
Args: []string{pluginPath},
|
||||
Stdout: stdout,
|
||||
Stderr: os.Stderr,
|
||||
}
|
||||
if err := c.Run(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
offset := uint(n.Uint64() + 1)
|
||||
return ipAdd(ipn.IP, offset), nil
|
||||
ipConf := &IPConfig{}
|
||||
err := json.Unmarshal(stdout.Bytes(), ipConf)
|
||||
return ipConf, err
|
||||
}
|
||||
|
||||
func deallocIP(ip net.IP) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func splitArg(arg string) (k, v string) {
|
||||
parts := strings.SplitN(arg, "=", 2)
|
||||
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
k = parts[0]
|
||||
case 2:
|
||||
k, v = parts[0], parts[1]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func parseArgs(args string) (*options, error) {
|
||||
argv := strings.Split(args, ",")
|
||||
|
||||
var err error
|
||||
opts := &options{}
|
||||
|
||||
for _, arg := range argv {
|
||||
k, v := splitArg(arg)
|
||||
switch k {
|
||||
case "iprange":
|
||||
opts.ipRange, err = util.ParseCIDR(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse iprange arg (%q): %v", v, err)
|
||||
}
|
||||
|
||||
case "ip":
|
||||
opts.ip = net.ParseIP(v)
|
||||
if opts.ip == nil {
|
||||
return nil, fmt.Errorf("failed to parse ip arg (%q)", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
// AllocIP allocates an IP in a given range.
|
||||
func AllocIP(contID types.UUID, netConf, ifName, args string) (*net.IPNet, net.IP, error) {
|
||||
opts, err := parseArgs(args)
|
||||
func ApplyIPConfig(ifName string, ipConf *IPConfig) error {
|
||||
link, err := netlink.LinkByName(ifName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.ipRange != nil {
|
||||
ip, err := allocIP(opts.ipRange)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error allocating IP in %v: %v", opts.ipRange, err)
|
||||
}
|
||||
|
||||
return &net.IPNet{
|
||||
IP: ip,
|
||||
Mask: opts.ipRange.Mask,
|
||||
}, nil, nil
|
||||
}
|
||||
|
||||
n := util.Net{}
|
||||
if err := util.LoadNet(netConf, &n); err != nil {
|
||||
return nil, nil, err
|
||||
addr := &netlink.Addr{IPNet: ipConf.IP, Label: ""}
|
||||
if err = netlink.AddrAdd(link, addr); err != nil {
|
||||
return fmt.Errorf("failed to add IP addr to %q: %v", ifName, err)
|
||||
}
|
||||
|
||||
switch n.IPAlloc.Type {
|
||||
case "static":
|
||||
_, rng, err := net.ParseCIDR(n.IPAlloc.Subnet)
|
||||
if err != nil {
|
||||
// TODO: cleanup
|
||||
return nil, nil, fmt.Errorf("error parsing %q conf: ipAlloc.Subnet: %v", netConf, err)
|
||||
for _, dst := range ipConf.Routes {
|
||||
if err = util.AddRoute(&dst, ipConf.Gateway, link); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ip, err := allocIP(rng)
|
||||
if err != nil {
|
||||
// TODO: cleanup
|
||||
return nil, nil, fmt.Errorf("error allocating IP in %v: %v", rng, err)
|
||||
}
|
||||
|
||||
return &net.IPNet{
|
||||
IP: ip,
|
||||
Mask: rng.Mask,
|
||||
}, nil, nil
|
||||
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("unsupported IP allocation type")
|
||||
}
|
||||
}
|
||||
|
||||
// AllocPtP allocates a /31 for point-to-point links.
|
||||
func AllocPtP(contID types.UUID, netConf, ifName, args string) ([2]net.IP, error) {
|
||||
ipn, _, err := AllocIP(contID, netConf, ifName, args)
|
||||
if err != nil {
|
||||
return [2]net.IP{nil, nil}, err
|
||||
}
|
||||
|
||||
mask := net.CIDRMask(31, 32)
|
||||
first := ipn.IP.Mask(mask)
|
||||
second := ipAdd(first, 1)
|
||||
|
||||
return [2]net.IP{first, second}, nil
|
||||
}
|
||||
|
||||
// DeallocIP is a no-op.
|
||||
func DeallocIP(contID types.UUID, netConf, ifName string, ipn *net.IPNet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# ipmanager
|
||||
# static IP address manager
|
||||
|
||||
ipmanager allocates static IPv4 and IPv6 addresses for containers.
|
||||
static IPAM allocates static IPv4 and IPv6 addresses for containers.
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -20,7 +20,10 @@ Given the following network configuration:
|
||||
#### Using the command line interface
|
||||
|
||||
```
|
||||
$ ipmanager -c f81d4fae-7dec-11d0-a765-00a0c91e6bf6
|
||||
$ export RKT_NETPLUGIN_COMMAND=ADD
|
||||
$ export RKT_NETPLUGIN_CONTID=f81d4fae-7dec-11d0-a765-00a0c91e6bf6
|
||||
$ export RKT_NETPLUGIN_NETCONF=/etc/rkt/net.d/default.conf
|
||||
$ ./static
|
||||
```
|
||||
|
||||
```
|
||||
@@ -29,29 +32,19 @@ $ ipmanager -c f81d4fae-7dec-11d0-a765-00a0c91e6bf6
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
$ ipmanager -c 1e8df09a-756f-4f3d-9f0f-3000e46598e7
|
||||
```
|
||||
|
||||
```
|
||||
{
|
||||
"ip": "203.0.113.2/24"
|
||||
}
|
||||
```
|
||||
|
||||
## Backends
|
||||
|
||||
By default ipmanager stores IP allocations on the local filesystem using the IP address as the file name and the container ID as contents. For example:
|
||||
|
||||
```
|
||||
$ ls /var/lib/ipmanager/networks/default
|
||||
$ ls /var/lib/rkt/networks/default
|
||||
```
|
||||
```
|
||||
203.0.113.1 203.0.113.2
|
||||
```
|
||||
|
||||
```
|
||||
$ cat /var/lib/ipmanager/networks/default/203.0.113.1
|
||||
$ cat /var/lib/rkt/networks/default/203.0.113.1
|
||||
```
|
||||
```
|
||||
f81d4fae-7dec-11d0-a765-00a0c91e6bf6
|
||||
@@ -68,13 +61,8 @@ f81d4fae-7dec-11d0-a765-00a0c91e6bf6
|
||||
"subnet": "3ffe:ffff:0:01ff::/64",
|
||||
"range-start": "3ffe:ffff:0:01ff::0010",
|
||||
"range-end": "3ffe:ffff:0:01ff::0020",
|
||||
"routers": [
|
||||
"routes": [
|
||||
"3ffe:ffff:0:01ff::1/64"
|
||||
],
|
||||
"domain-name": "example.com",
|
||||
"domain-name-servers": [
|
||||
"2001:4860:4860::8888",
|
||||
"2001:4860:4860::8844"
|
||||
]
|
||||
}
|
||||
```
|
||||
@@ -87,13 +75,8 @@ f81d4fae-7dec-11d0-a765-00a0c91e6bf6
|
||||
"subnet": "203.0.113.1/24",
|
||||
"range-start": "203.0.113.10",
|
||||
"range-end": "203.0.113.20",
|
||||
"routers": [
|
||||
"203.0.113.1"
|
||||
],
|
||||
"domain-name": "example.com",
|
||||
"domain-name-servers": [
|
||||
"8.8.8.8",
|
||||
"8.8.4.4"
|
||||
"routes": [
|
||||
"203.0.113.0/24"
|
||||
]
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,213 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/coreos/rocket/networking/ipam"
|
||||
"github.com/coreos/rocket/networking/ipam/static/backend"
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
)
|
||||
|
||||
type IPAllocator struct {
|
||||
start net.IP
|
||||
end net.IP
|
||||
last net.IP
|
||||
ipnet *net.IPNet
|
||||
conf *IPAMConfig
|
||||
store backend.Store
|
||||
}
|
||||
|
||||
func NewIPAllocator(conf *IPAMConfig, store backend.Store) (*IPAllocator, error) {
|
||||
var (
|
||||
start net.IP
|
||||
end net.IP
|
||||
err error
|
||||
)
|
||||
_, ipnet, err := net.ParseCIDR(conf.Subnet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
start, end = networkRange(ipnet)
|
||||
start = util.NextIP(start)
|
||||
end = util.PrevIP(end)
|
||||
|
||||
if conf.RangeStart != "" {
|
||||
start, err = validateRangeIP(conf.RangeStart, ipnet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if conf.RangeEnd != "" {
|
||||
end, err = validateRangeIP(conf.RangeEnd, ipnet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &IPAllocator{start, end, util.PrevIP(start), ipnet, conf, store}, nil
|
||||
}
|
||||
|
||||
func validateRangeIP(s string, ipnet *net.IPNet) (net.IP, error) {
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("invalid ip address: %s", s)
|
||||
}
|
||||
if !ipnet.Contains(ip) {
|
||||
return nil, fmt.Errorf("%s not in network: %s", s, ipnet)
|
||||
}
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
// Returns newly allocated IP along with its config
|
||||
func (a *IPAllocator) Get(id string) (*ipam.IPConfig, error) {
|
||||
a.store.Lock()
|
||||
defer a.store.Unlock()
|
||||
|
||||
var gw net.IP
|
||||
if a.conf.Gateway != "" {
|
||||
gw = net.ParseIP(a.conf.Gateway)
|
||||
}
|
||||
|
||||
routes, err := parseIPNets(a.conf.Routes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ip := a.nextIP()
|
||||
seen := newIP(ip)
|
||||
for {
|
||||
// don't allocate gateway IP
|
||||
if gw != nil && ip.Equal(gw) {
|
||||
continue
|
||||
}
|
||||
|
||||
reserved, err := a.store.Reserve(id, ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reserved {
|
||||
break
|
||||
}
|
||||
ip = a.nextIP()
|
||||
if seen.Equal(ip) {
|
||||
return nil, fmt.Errorf("no ip addresses available in network: %s", a.conf.Name)
|
||||
}
|
||||
}
|
||||
|
||||
ipnet := net.IPNet{ip, a.ipnet.Mask}
|
||||
alloc := &ipam.IPConfig{
|
||||
IP: &ipnet,
|
||||
Gateway: gw,
|
||||
Routes: routes,
|
||||
}
|
||||
return alloc, nil
|
||||
}
|
||||
|
||||
// Allocates both an IP and the Gateway IP, i.e. a /31
|
||||
// This is used for Point-to-Point links
|
||||
func (a *IPAllocator) GetPtP(id string) (*ipam.IPConfig, error) {
|
||||
a.store.Lock()
|
||||
defer a.store.Unlock()
|
||||
|
||||
routes, err := parseIPNets(a.conf.Routes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gw := a.nextIP()
|
||||
ip := net.IP{}
|
||||
|
||||
seen := newIP(gw)
|
||||
|
||||
for {
|
||||
if evenIP(gw) {
|
||||
// we're looking for unreserved even, odd pair
|
||||
reserved, err := a.store.Reserve(id, gw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reserved {
|
||||
ip = a.nextIP()
|
||||
reserved, err := a.store.Reserve(id, ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if reserved {
|
||||
// found them both!
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gw = a.nextIP()
|
||||
if seen.Equal(gw) {
|
||||
return nil, fmt.Errorf("no ip addresses available in network: %s", a.conf.Name)
|
||||
}
|
||||
}
|
||||
|
||||
_, bits := a.ipnet.Mask.Size()
|
||||
mask := net.CIDRMask(bits-1, bits)
|
||||
|
||||
alloc := &ipam.IPConfig{
|
||||
IP: &net.IPNet{ip, mask},
|
||||
Gateway: gw,
|
||||
Routes: routes,
|
||||
}
|
||||
return alloc, nil
|
||||
}
|
||||
|
||||
// Releases all IPs allocated for the container with given ID
|
||||
func (a *IPAllocator) Release(id string) error {
|
||||
a.store.Lock()
|
||||
defer a.store.Unlock()
|
||||
|
||||
return a.store.ReleaseByContainerID(id)
|
||||
}
|
||||
|
||||
func (a *IPAllocator) nextIP() net.IP {
|
||||
if a.last.Equal(a.end) {
|
||||
a.last = a.start
|
||||
return a.start
|
||||
}
|
||||
a.last = util.NextIP(a.last)
|
||||
return a.last
|
||||
}
|
||||
|
||||
func newIP(ip net.IP) net.IP {
|
||||
n := make(net.IP, len(ip))
|
||||
copy(n, ip)
|
||||
return n
|
||||
}
|
||||
|
||||
func networkRange(ipnet *net.IPNet) (net.IP, net.IP) {
|
||||
var end net.IP
|
||||
for i := 0; i < len(ipnet.IP); i++ {
|
||||
end = append(end, ipnet.IP[i]|^ipnet.Mask[i])
|
||||
}
|
||||
return ipnet.IP, end
|
||||
}
|
||||
|
||||
func evenIP(ip net.IP) bool {
|
||||
i := ip.To4()
|
||||
if i == nil {
|
||||
i = ip.To16()
|
||||
if i == nil {
|
||||
panic("IP is not v4 or v6")
|
||||
}
|
||||
}
|
||||
|
||||
return i[len(i)-1]%2 == 0
|
||||
}
|
||||
|
||||
func parseIPNets(ipnets []string) ([]net.IPNet, error) {
|
||||
n := []net.IPNet{}
|
||||
for _, s := range ipnets {
|
||||
ipn, err := util.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n = append(n, *ipn)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
+21
-12
@@ -5,27 +5,36 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/coreos/rocket/pkg/lock"
|
||||
)
|
||||
|
||||
var defaultDataDir = "/var/lib/ipmanager/networks"
|
||||
var defaultDataDir = "/var/lib/rkt/networks"
|
||||
|
||||
type Store struct {
|
||||
lock.DirLock
|
||||
dataDir string
|
||||
}
|
||||
|
||||
func New() (*Store, error) {
|
||||
if err := os.MkdirAll(defaultDataDir, 0644); err != nil {
|
||||
func New(network string) (*Store, error) {
|
||||
dir := filepath.Join(defaultDataDir, network)
|
||||
if err := os.MkdirAll(dir, 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{defaultDataDir}, nil
|
||||
|
||||
lk, err := lock.NewLock(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{*lk, dir}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Reserve(network, id string, ip net.IP) (bool, error) {
|
||||
dst := filepath.Join(s.dataDir, network)
|
||||
if err := os.MkdirAll(dst, 0644); err != nil {
|
||||
return false, err
|
||||
}
|
||||
fname := filepath.Join(dst, ip.String())
|
||||
func (s *Store) Lock() error {
|
||||
return s.ExclusiveLock()
|
||||
}
|
||||
|
||||
func (s *Store) Reserve(id string, ip net.IP) (bool, error) {
|
||||
fname := filepath.Join(s.dataDir, ip.String())
|
||||
f, err := os.OpenFile(fname, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
|
||||
if os.IsExist(err) {
|
||||
return false, nil
|
||||
@@ -45,11 +54,11 @@ func (s *Store) Reserve(network, id string, ip net.IP) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *Store) Release(network string, ip net.IP) error {
|
||||
func (s *Store) Release(ip net.IP) error {
|
||||
return os.Remove(filepath.Join(s.dataDir, ip.String()))
|
||||
}
|
||||
|
||||
func (s *Store) ReleaseByContainerID(network string, id string) error {
|
||||
func (s *Store) ReleaseByContainerID(id string) error {
|
||||
err := filepath.Walk(s.dataDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -0,0 +1,12 @@
|
||||
package backend
|
||||
|
||||
import "net"
|
||||
|
||||
type Store interface {
|
||||
Lock() error
|
||||
Unlock() error
|
||||
Close() error
|
||||
Reserve(id string, ip net.IP) (bool, error)
|
||||
Release(ip net.IP) error
|
||||
ReleaseByContainerID(id string) error
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
rktnet "github.com/coreos/rocket/networking/net"
|
||||
)
|
||||
|
||||
var defaultConfigDir = "/etc/rkt/ipam.d"
|
||||
|
||||
// IPAMConfig represents the IP related network configuration.
|
||||
type IPAMConfig struct {
|
||||
Name string
|
||||
Type string `json:"type"`
|
||||
RangeStart string `json:"range-start"`
|
||||
RangeEnd string `json:"range-end"`
|
||||
Subnet string `json:"subnet"`
|
||||
Gateway string `json:"gateway"`
|
||||
Routes []string `json:"routes"`
|
||||
}
|
||||
|
||||
type Net struct {
|
||||
Name string `json:"name"`
|
||||
IPAM *IPAMConfig `json:"ipam"`
|
||||
}
|
||||
|
||||
// NewIPAMConfig creates a NetworkConfig from the given network name.
|
||||
func NewIPAMConfig(netConf string) (*IPAMConfig, error) {
|
||||
n := Net{}
|
||||
if err := rktnet.LoadNet(netConf, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if n.IPAM == nil {
|
||||
return nil, fmt.Errorf("%q missing 'ipam' key")
|
||||
}
|
||||
|
||||
// Copy net name into IPAM so not to drag Net struct around
|
||||
n.IPAM.Name = n.Name
|
||||
|
||||
return n.IPAM, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/coreos/rocket/networking/ipam"
|
||||
"github.com/coreos/rocket/networking/ipam/static/backend/disk"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cmd, containerID, netConf := lookupParams()
|
||||
|
||||
ipamConf, err := NewIPAMConfig(netConf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
store, err := disk.New(ipamConf.Name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
allocator, err := NewIPAllocator(ipamConf, store)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "ADD":
|
||||
cmdAdd(allocator, containerID, ipamConf.Type)
|
||||
|
||||
case "DEL":
|
||||
allocator.Release(containerID)
|
||||
}
|
||||
}
|
||||
|
||||
func lookupParams() (cmd, containerID, netConf string) {
|
||||
names := []string{"RKT_NETPLUGIN_COMMAND", "RKT_NETPLUGIN_CONTID", "RKT_NETPLUGIN_NETCONF"}
|
||||
values := make([]string, len(names))
|
||||
|
||||
for i, n := range names {
|
||||
values[i] = os.Getenv(n)
|
||||
if values[i] == "" {
|
||||
log.Fatalf("%q environment variable is misssing", n)
|
||||
}
|
||||
}
|
||||
|
||||
cmd, containerID, netConf = values[0], values[1], values[2]
|
||||
return
|
||||
}
|
||||
|
||||
func cmdAdd(allocator *IPAllocator, containerID, plugin string) {
|
||||
var ipConf *ipam.IPConfig
|
||||
var err error
|
||||
|
||||
switch plugin {
|
||||
case "static":
|
||||
ipConf, err = allocator.Get(containerID)
|
||||
case "static-ptp":
|
||||
ipConf, err = allocator.GetPtP(containerID)
|
||||
default:
|
||||
log.Fatal("Unsupported IPAM plugin type")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(ipConf, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
+19
-10
@@ -16,6 +16,7 @@ package networking
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
@@ -24,20 +25,25 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/rocket/common"
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
rktnet "github.com/coreos/rocket/networking/net"
|
||||
)
|
||||
|
||||
// TODO(eyakubovich): make this configurable in rkt.conf
|
||||
const UserNetPluginsPath = "/usr/lib/rkt/plugins/net"
|
||||
const BuiltinNetPluginsPath = "usr/lib/rkt/plugins/net"
|
||||
|
||||
func (e *containerEnv) netPluginAdd(n *Net, netns, args, ifName string) (*net.IPNet, error) {
|
||||
func (e *containerEnv) netPluginAdd(n *Net, netns, args, ifName string) (net.IP, error) {
|
||||
output, err := e.execNetPlugin("ADD", n, netns, args, ifName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return util.ParseCIDR(output)
|
||||
ifConf := rktnet.IfConfig{}
|
||||
if err = json.Unmarshal(output, &ifConf); err != nil {
|
||||
return nil, fmt.Errorf("error parsing %q output: %v", n.Name, err)
|
||||
}
|
||||
|
||||
return ifConf.IP, nil
|
||||
}
|
||||
|
||||
func (e *containerEnv) netPluginDel(n *Net, netns, args, ifName string) error {
|
||||
@@ -45,14 +51,16 @@ func (e *containerEnv) netPluginDel(n *Net, netns, args, ifName string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *containerEnv) findNetPlugin(plugin string) string {
|
||||
func (e *containerEnv) pluginPaths() []string {
|
||||
// try 3rd-party path first
|
||||
paths := []string{
|
||||
return []string{
|
||||
UserNetPluginsPath,
|
||||
filepath.Join(common.Stage1RootfsPath(e.rktRoot), BuiltinNetPluginsPath),
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range paths {
|
||||
func (e *containerEnv) findNetPlugin(plugin string) string {
|
||||
for _, p := range e.pluginPaths() {
|
||||
fullname := filepath.Join(p, plugin)
|
||||
if fi, err := os.Stat(fullname); err == nil && fi.Mode().IsRegular() {
|
||||
return fullname
|
||||
@@ -72,10 +80,10 @@ func envVars(vars [][2]string) []string {
|
||||
return env
|
||||
}
|
||||
|
||||
func (e *containerEnv) execNetPlugin(cmd string, n *Net, netns, args, ifName string) (string, error) {
|
||||
func (e *containerEnv) execNetPlugin(cmd string, n *Net, netns, args, ifName string) ([]byte, error) {
|
||||
pluginPath := e.findNetPlugin(n.Type)
|
||||
if pluginPath == "" {
|
||||
return "", fmt.Errorf("Could not find plugin %q", n.Type)
|
||||
return nil, fmt.Errorf("Could not find plugin %q", n.Type)
|
||||
}
|
||||
|
||||
vars := [][2]string{
|
||||
@@ -86,6 +94,7 @@ func (e *containerEnv) execNetPlugin(cmd string, n *Net, netns, args, ifName str
|
||||
{"RKT_NETPLUGIN_IFNAME", ifName},
|
||||
{"RKT_NETPLUGIN_NETNAME", n.Name},
|
||||
{"RKT_NETPLUGIN_NETCONF", n.Filename},
|
||||
{"RKT_NETPLUGIN_IPAMPATH", strings.Join(e.pluginPaths(), ":")},
|
||||
}
|
||||
|
||||
stdout := &bytes.Buffer{}
|
||||
@@ -98,8 +107,8 @@ func (e *containerEnv) execNetPlugin(cmd string, n *Net, netns, args, ifName str
|
||||
Stderr: os.Stderr,
|
||||
}
|
||||
if err := c.Run(); err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stdout.String(), nil
|
||||
return stdout.Bytes(), nil
|
||||
}
|
||||
|
||||
+4
-4
@@ -22,12 +22,12 @@ import (
|
||||
"sort"
|
||||
|
||||
"github.com/coreos/rocket/common"
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
rktnet "github.com/coreos/rocket/networking/net"
|
||||
)
|
||||
|
||||
// Net encodes a network plugin.
|
||||
type Net struct {
|
||||
util.Net
|
||||
rktnet.Net
|
||||
args string
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func loadUserNets() ([]Net, error) {
|
||||
for _, filename := range files {
|
||||
filepath := path.Join(UserNetPath, filename)
|
||||
n := Net{}
|
||||
if err := util.LoadNet(filepath, &n); err != nil {
|
||||
if err := rktnet.LoadNet(filepath, &n); err != nil {
|
||||
return nil, fmt.Errorf("error loading %v: %v", filepath, err)
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (e *containerEnv) loadNets() ([]Net, error) {
|
||||
|
||||
defPath := path.Join(common.Stage1RootfsPath(e.rktRoot), DefaultNetPath)
|
||||
defNet := Net{}
|
||||
if err := util.LoadNet(defPath, &defNet); err != nil {
|
||||
if err := rktnet.LoadNet(defPath, &defNet); err != nil {
|
||||
return nil, fmt.Errorf("error loading net: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -26,13 +26,14 @@ import (
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink"
|
||||
|
||||
"github.com/coreos/rocket/networking/ipam"
|
||||
rktnet "github.com/coreos/rocket/networking/net"
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
)
|
||||
|
||||
const defaultBrName = "rkt0"
|
||||
|
||||
type netConf struct {
|
||||
util.Net
|
||||
type Net struct {
|
||||
rktnet.Net
|
||||
BrName string `json:"brName"`
|
||||
IsGW bool `json:"isGW"`
|
||||
}
|
||||
@@ -111,16 +112,20 @@ func ensureBridge(brName string, ipn *net.IPNet) (*netlink.Bridge, error) {
|
||||
return br, nil
|
||||
}
|
||||
|
||||
func setupVeth(contID types.UUID, netns string, br *netlink.Bridge, ipn *net.IPNet, ifName string) error {
|
||||
func setupVeth(contID types.UUID, netns string, br *netlink.Bridge, ifName string, ipConf *ipam.IPConfig) error {
|
||||
var hostVethName string
|
||||
|
||||
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)
|
||||
hostVeth, _, err := util.SetupVeth(contID.String(), ifName, nil, hostNS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = ipam.ApplyIPConfig(ifName, ipConf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostVethName = hostVeth.Attrs().Name
|
||||
return nil
|
||||
})
|
||||
@@ -142,49 +147,67 @@ func setupVeth(contID types.UUID, netns string, br *netlink.Bridge, ipn *net.IPN
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdAdd(contID, netns, netCfg, ifName string) error {
|
||||
func calcGatewayIP(ipn *net.IPNet) net.IP {
|
||||
nid := ipn.IP.Mask(ipn.Mask)
|
||||
return util.NextIP(nid)
|
||||
}
|
||||
|
||||
func setupBridge(n *Net, ipamConf *ipam.IPConfig) (*netlink.Bridge, error) {
|
||||
var gwn *net.IPNet
|
||||
if n.IsGW {
|
||||
gw := net.IP{}
|
||||
if ipamConf.Gateway != nil {
|
||||
gw = ipamConf.Gateway
|
||||
} else {
|
||||
gw = calcGatewayIP(ipamConf.IP)
|
||||
}
|
||||
|
||||
gwn = &net.IPNet{
|
||||
IP: gw,
|
||||
Mask: ipamConf.IP.Mask,
|
||||
}
|
||||
}
|
||||
|
||||
// create bridge if necessary
|
||||
br, err := ensureBridge(n.BrName, gwn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create bridge %q: %v", n.BrName, err)
|
||||
}
|
||||
|
||||
return br, 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 := netConf{
|
||||
n := &Net{
|
||||
BrName: defaultBrName,
|
||||
}
|
||||
if err := util.LoadNet(netCfg, &conf); err != nil {
|
||||
return fmt.Errorf("failed to load %q: %v", netCfg, err)
|
||||
if err := rktnet.LoadNet(netConf, n); err != nil {
|
||||
return fmt.Errorf("failed to load %q: %v", netConf, err)
|
||||
}
|
||||
|
||||
ipn, gw, err := ipam.AllocIP(*cid, netCfg, ifName, "")
|
||||
// run the IPAM plugin and get back the config to apply
|
||||
ipConf, err := ipam.ExecPlugin(n.Net.IPAM.Type)
|
||||
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)
|
||||
br, err := setupBridge(n, ipConf)
|
||||
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 {
|
||||
if err = setupVeth(*cid, netns, br, ifName, ipConf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return rktnet.PrintIfConfig(&rktnet.IfConfig{
|
||||
IP: ipConf.IP.IP,
|
||||
})
|
||||
}
|
||||
|
||||
func cmdDel(contID, netns, netConf, ifName string) error {
|
||||
@@ -221,8 +244,7 @@ func main() {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("%v: %v", os.Args[1], err)
|
||||
log.Printf("%v: %v", cmd, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,11 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package util
|
||||
package net
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
gonet "net"
|
||||
"os"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
@@ -25,11 +27,9 @@ type Net struct {
|
||||
Filename string
|
||||
Name string `json:"name,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
IPAlloc struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Subnet string `json:"subnet,omitempty"`
|
||||
} `json:"ipAlloc,omitempty"`
|
||||
Routes []string `json:"routes,omitempty"`
|
||||
IPAM struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
} `json:"ipam,omitempty"`
|
||||
}
|
||||
|
||||
// LoadNet loads a JSON-encoded Net from the filesystem.
|
||||
@@ -58,3 +58,18 @@ func LoadNet(path string, n interface{}) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// this is what net plugin returns to rkt
|
||||
type IfConfig struct {
|
||||
IP gonet.IP `json:"ip,omitempty"`
|
||||
IP6 gonet.IP `json:"ip6,omitempty"`
|
||||
}
|
||||
|
||||
func PrintIfConfig(conf *IfConfig) error {
|
||||
data, err := json.MarshalIndent(conf, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = os.Stdout.Write(data)
|
||||
return err
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package util
|
||||
package net
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -21,10 +21,10 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/appc/spec/schema/types"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink"
|
||||
|
||||
"github.com/coreos/rocket/networking/ipam"
|
||||
rktnet "github.com/coreos/rocket/networking/net"
|
||||
"github.com/coreos/rocket/networking/util"
|
||||
)
|
||||
|
||||
@@ -36,51 +36,33 @@ func init() {
|
||||
}
|
||||
|
||||
func cmdAdd(contID, netns, netConf, ifName, args string) error {
|
||||
var hostVethName, contIPNet string
|
||||
var hostVethName string
|
||||
|
||||
cid, err := types.NewUUID(contID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing ContainerID: %v", err)
|
||||
}
|
||||
|
||||
conf := util.Net{}
|
||||
if err := util.LoadNet(netConf, &conf); err != nil {
|
||||
conf := rktnet.Net{}
|
||||
if err := rktnet.LoadNet(netConf, &conf); err != nil {
|
||||
return fmt.Errorf("failed to load %q: %v", netConf, err)
|
||||
}
|
||||
|
||||
ips, err := ipam.AllocPtP(*cid, netConf, ifName, args)
|
||||
// run the IPAM plugin and get back the config to apply
|
||||
ipConf, err := ipam.ExecPlugin(conf.IPAM.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostIP, contIP := ips[0], ips[1]
|
||||
|
||||
err = util.WithNetNSPath(netns, func(hostNS *os.File) error {
|
||||
entropy := contID + ifName
|
||||
|
||||
ipn := &net.IPNet{
|
||||
IP: contIP,
|
||||
Mask: net.CIDRMask(31, 32),
|
||||
}
|
||||
|
||||
hostVeth, contVeth, err := util.SetupVeth(entropy, ifName, ipn, hostNS)
|
||||
hostVeth, _, err := util.SetupVeth(entropy, ifName, nil, hostNS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range conf.Routes {
|
||||
dst, err := util.ParseCIDR(r)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse route %q: %v", r, err)
|
||||
}
|
||||
|
||||
if err = util.AddRoute(dst, hostIP, contVeth); err != nil {
|
||||
return fmt.Errorf("failed to add route %q: %v", dst, err)
|
||||
}
|
||||
err = ipam.ApplyIPConfig(ifName, ipConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostVethName = hostVeth.Attrs().Name
|
||||
contIPNet = ipn.String()
|
||||
|
||||
return nil
|
||||
})
|
||||
@@ -95,7 +77,7 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error {
|
||||
}
|
||||
|
||||
ipn := &net.IPNet{
|
||||
IP: hostIP,
|
||||
IP: ipConf.Gateway,
|
||||
Mask: net.CIDRMask(31, 32),
|
||||
}
|
||||
addr := &netlink.Addr{IPNet: ipn, Label: ""}
|
||||
@@ -108,9 +90,9 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error {
|
||||
return fmt.Errorf("failed to add route on host: %v", err)
|
||||
}
|
||||
|
||||
fmt.Print(contIPNet)
|
||||
|
||||
return nil
|
||||
return rktnet.PrintIfConfig(&rktnet.IfConfig{
|
||||
IP: ipConf.IP.IP,
|
||||
})
|
||||
}
|
||||
|
||||
func cmdDel(contID, netns, netConf, ifName, args string) error {
|
||||
@@ -148,7 +130,7 @@ func main() {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("%v: %v", os.Args[1], err)
|
||||
log.Printf("%v: %v", cmd, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ const (
|
||||
type activeNet struct {
|
||||
Net
|
||||
ifName string
|
||||
ipn *net.IPNet
|
||||
ip net.IP
|
||||
}
|
||||
|
||||
// "base" struct that's populated from the beginning
|
||||
@@ -110,7 +110,7 @@ func Setup(rktRoot string, contID types.UUID) (*Networking, error) {
|
||||
}
|
||||
|
||||
// last net is the default
|
||||
n.MetadataIP = n.nets[len(n.nets)-1].ipn.IP
|
||||
n.MetadataIP = n.nets[len(n.nets)-1].ip
|
||||
|
||||
return &n, nil
|
||||
}
|
||||
@@ -184,7 +184,7 @@ func (e *containerEnv) setupNets(netns string, nets []Net) ([]activeNet, error)
|
||||
|
||||
log.Printf("Executing net-plugin %v", nt.Type)
|
||||
|
||||
an.ipn, err = e.netPluginAdd(&nt, netns, nt.args, an.ifName)
|
||||
an.ip, err = e.netPluginAdd(&nt, netns, nt.args, an.ifName)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error adding network %q: %v", nt.Name, err)
|
||||
break
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"net"
|
||||
)
|
||||
|
||||
@@ -28,3 +29,24 @@ func ParseCIDR(s string) (*net.IPNet, error) {
|
||||
ipn.IP = ip
|
||||
return ipn, nil
|
||||
}
|
||||
|
||||
func NextIP(ip net.IP) net.IP {
|
||||
i := ipToInt(ip)
|
||||
return intToIP(i.Add(i, big.NewInt(1)))
|
||||
}
|
||||
|
||||
func PrevIP(ip net.IP) net.IP {
|
||||
i := ipToInt(ip)
|
||||
return intToIP(i.Sub(i, big.NewInt(1)))
|
||||
}
|
||||
|
||||
func ipToInt(ip net.IP) *big.Int {
|
||||
if v := ip.To4(); v != nil {
|
||||
return big.NewInt(0).SetBytes(v)
|
||||
}
|
||||
return big.NewInt(0).SetBytes(ip.To16())
|
||||
}
|
||||
|
||||
func intToIP(i *big.Int) net.IP {
|
||||
return net.IP(i.Bytes())
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
PLUGINS=bin/veth bin/bridge
|
||||
PLUGINS=bin/veth bin/bridge bin/static
|
||||
|
||||
../aggregate/install.d/30net-plugins: Makefile install $(PLUGINS)
|
||||
@cp install ../aggregate/install.d/30net-plugins
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
DST="$ROOT/usr/lib/rkt/plugins/net"
|
||||
mkdir -p $DST
|
||||
install -m 0755 ../net-plugins/bin/* $DST
|
||||
ln -s static "$DST/static-ptp"
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "default",
|
||||
"type": "veth",
|
||||
"ipAlloc": {
|
||||
"type": "static",
|
||||
"subnet": "172.16.28.0/24"
|
||||
},
|
||||
"routes": [ "0.0.0.0/0" ]
|
||||
"ipam": {
|
||||
"type": "static-ptp",
|
||||
"subnet": "172.16.28.0/24",
|
||||
"routes": [ "169.254.169.255/32", "0.0.0.0/0" ]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user