CNI IPAM plugin
This commit is contained in:
1 parent
703b8dfd11
commit
f9615c34dc
5 files changed
+79
-3
No files matched your search
@@ -64,3 +64,6 @@
|
|||||||
[submodule "vendor/github.com/hashicorp/go-cleanhttp"]
|
[submodule "vendor/github.com/hashicorp/go-cleanhttp"]
|
||||||
path = vendor/github.com/hashicorp/go-cleanhttp
|
path = vendor/github.com/hashicorp/go-cleanhttp
|
||||||
url = http://github.com/hashicorp/go-cleanhttp
|
url = http://github.com/hashicorp/go-cleanhttp
|
||||||
|
[submodule "vendor/github.com/appc/cni"]
|
||||||
|
path = vendor/github.com/appc/cni
|
||||||
|
url = https://github.com/appc/cni
|
||||||
@@ -67,7 +67,7 @@ $(WEAVER_EXE): router/*.go ipam/*.go ipam/*/*.go db/*.go nameserver/*.go prog/we
|
|||||||
$(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/*.go
|
$(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/*.go
|
||||||
$(WEAVEUTIL_EXE): prog/weaveutil/*.go
|
$(WEAVEUTIL_EXE): prog/weaveutil/*.go
|
||||||
$(SIGPROXY_EXE): prog/sigproxy/*.go
|
$(SIGPROXY_EXE): prog/sigproxy/*.go
|
||||||
$(DOCKERPLUGIN_EXE): prog/plugin/*.go plugin/*/*.go api/*.go common/docker/*.go
|
$(DOCKERPLUGIN_EXE): prog/plugin/*.go plugin/*/*.go api/*.go common/*.go common/docker/*.go
|
||||||
$(TEST_TLS_EXE): test/tls/*.go
|
$(TEST_TLS_EXE): test/tls/*.go
|
||||||
$(WEAVEWAIT_NOOP_EXE): prog/weavewait/*.go
|
$(WEAVEWAIT_NOOP_EXE): prog/weavewait/*.go
|
||||||
$(WEAVEWAIT_EXE): prog/weavewait/*.go net/*.go
|
$(WEAVEWAIT_EXE): prog/weavewait/*.go net/*.go
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package ipamplugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/appc/cni/pkg/skel"
|
||||||
|
"github.com/appc/cni/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i *Ipam) CmdAdd(args *skel.CmdArgs) error {
|
||||||
|
// extract the things we care about
|
||||||
|
conf, err := loadIPAMConf(args.StdinData)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if conf == nil {
|
||||||
|
conf = &ipamConf{}
|
||||||
|
}
|
||||||
|
containerID := args.ContainerID
|
||||||
|
var ipnet *net.IPNet
|
||||||
|
|
||||||
|
if conf.Subnet == "" {
|
||||||
|
ipnet, err = i.weave.AllocateIP(containerID)
|
||||||
|
} else {
|
||||||
|
var subnet *net.IPNet
|
||||||
|
subnet, err = types.ParseCIDR(conf.Subnet)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("subnet given in config, but not parseable: %s", err)
|
||||||
|
}
|
||||||
|
ipnet, err = i.weave.AllocateIPInSubnet(containerID, subnet)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
result := types.Result{
|
||||||
|
IP4: &types.IPConfig{
|
||||||
|
IP: *ipnet,
|
||||||
|
Gateway: conf.Gateway,
|
||||||
|
Routes: conf.Routes,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return result.Print()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Ipam) CmdDel(args *skel.CmdArgs) error {
|
||||||
|
return i.weave.ReleaseIPsFor(args.ContainerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ipamConf struct {
|
||||||
|
Subnet string `json:"subnet,omitempty"`
|
||||||
|
Gateway net.IP `json:"gateway,omitempty"`
|
||||||
|
Routes []types.Route `json:"routes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type netConf struct {
|
||||||
|
IPAM *ipamConf `json:"ipam"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadIPAMConf(stdinData []byte) (*ipamConf, error) {
|
||||||
|
var conf netConf
|
||||||
|
return conf.IPAM, json.Unmarshal(stdinData, &conf)
|
||||||
|
}
|
||||||
+9
-2
@@ -9,6 +9,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
cni "github.com/appc/cni/pkg/skel"
|
||||||
"github.com/docker/libnetwork/ipamapi"
|
"github.com/docker/libnetwork/ipamapi"
|
||||||
weaveapi "github.com/weaveworks/weave/api"
|
weaveapi "github.com/weaveworks/weave/api"
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
@@ -45,14 +46,20 @@ func main() {
|
|||||||
|
|
||||||
SetLogLevel(logLevel)
|
SetLogLevel(logLevel)
|
||||||
|
|
||||||
|
weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), Log)
|
||||||
|
|
||||||
|
if os.Args[0] == "weave-ipam" {
|
||||||
|
i := ipamplugin.NewIpam(weave)
|
||||||
|
cni.PluginMain(i.CmdAdd, i.CmdDel)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
// API 1.21 is the first version that supports docker network commands
|
// API 1.21 is the first version that supports docker network commands
|
||||||
dockerClient, err := docker.NewVersionedClientFromEnv("1.21")
|
dockerClient, err := docker.NewVersionedClientFromEnv("1.21")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Fatalf("unable to connect to docker: %s", err)
|
Log.Fatalf("unable to connect to docker: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), Log)
|
|
||||||
|
|
||||||
Log.Println("Weave plugin", version, "Command line options:", os.Args[1:])
|
Log.Println("Weave plugin", version, "Command line options:", os.Args[1:])
|
||||||
Log.Info(dockerClient.Info())
|
Log.Info(dockerClient.Info())
|
||||||
|
|
||||||
|
|||||||
+1
Submodule vendor/github.com/appc/cni added at cde7088cbe.
Reference in new issue
Block a user