From f9615c34dc1a07cb1c45ff117c653422bc50ddb4 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 8 Mar 2016 11:43:09 +0000 Subject: [PATCH] CNI IPAM plugin --- .gitmodules | 3 ++ Makefile | 2 +- plugin/ipam/cni.go | 65 ++++++++++++++++++++++++++++++++++++++ prog/plugin/main.go | 11 +++++-- vendor/github.com/appc/cni | 1 + 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 plugin/ipam/cni.go create mode 160000 vendor/github.com/appc/cni diff --git a/.gitmodules b/.gitmodules index adec6564..f2a0e10d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -64,3 +64,6 @@ [submodule "vendor/github.com/hashicorp/go-cleanhttp"] path = vendor/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 diff --git a/Makefile b/Makefile index 73072389..b181c05c 100644 --- a/Makefile +++ b/Makefile @@ -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 $(WEAVEUTIL_EXE): prog/weaveutil/*.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 $(WEAVEWAIT_NOOP_EXE): prog/weavewait/*.go $(WEAVEWAIT_EXE): prog/weavewait/*.go net/*.go diff --git a/plugin/ipam/cni.go b/plugin/ipam/cni.go new file mode 100644 index 00000000..8abcabcd --- /dev/null +++ b/plugin/ipam/cni.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) +} diff --git a/prog/plugin/main.go b/prog/plugin/main.go index e0697e29..bbb818ec 100644 --- a/prog/plugin/main.go +++ b/prog/plugin/main.go @@ -9,6 +9,7 @@ import ( "os/signal" "syscall" + cni "github.com/appc/cni/pkg/skel" "github.com/docker/libnetwork/ipamapi" weaveapi "github.com/weaveworks/weave/api" . "github.com/weaveworks/weave/common" @@ -45,14 +46,20 @@ func main() { 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 dockerClient, err := docker.NewVersionedClientFromEnv("1.21") if err != nil { 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.Info(dockerClient.Info()) diff --git a/vendor/github.com/appc/cni b/vendor/github.com/appc/cni new file mode 160000 index 00000000..cde7088c --- /dev/null +++ b/vendor/github.com/appc/cni @@ -0,0 +1 @@ +Subproject commit cde7088cbe539013c2efd994d07bdaac0f5a59b0