diff --git a/net/netns.go b/net/netns.go index f3a8816a..971baf63 100644 --- a/net/netns.go +++ b/net/netns.go @@ -57,12 +57,14 @@ func WithNetNSLinkUnsafe(ns netns.NsHandle, ifName string, work func(link netlin }) } +var WeaveUtilCmd = "weaveutil" + // A safe version of WithNetNS* which creates a process executing // "nsenter --net= weaveutil [args]". func WithNetNS(nsPath string, cmd string, args ...string) ([]byte, error) { var stdout, stderr bytes.Buffer - args = append([]string{"--net=" + nsPath, "weaveutil", cmd}, args...) + args = append([]string{"--net=" + nsPath, WeaveUtilCmd, cmd}, args...) c := exec.Command("nsenter", args...) c.Stdout = &stdout c.Stderr = &stderr diff --git a/prog/weaveutil/cni.go b/prog/weaveutil/cni.go new file mode 100644 index 00000000..3f5dba4d --- /dev/null +++ b/prog/weaveutil/cni.go @@ -0,0 +1,26 @@ +package main + +import ( + "os" + + cni "github.com/appc/cni/pkg/skel" + + weaveapi "github.com/weaveworks/weave/api" + "github.com/weaveworks/weave/common" + ipamplugin "github.com/weaveworks/weave/plugin/ipam" + netplugin "github.com/weaveworks/weave/plugin/net" +) + +func cniNet(args []string) error { + weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), common.Log) + n := netplugin.NewCNIPlugin(weave) + cni.PluginMain(n.CmdAdd, n.CmdDel) + return nil +} + +func cniIPAM(args []string) error { + weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), common.Log) + i := ipamplugin.NewIpam(weave) + cni.PluginMain(i.CmdAdd, i.CmdDel) + return nil +} diff --git a/prog/weaveutil/main.go b/prog/weaveutil/main.go index 2381827d..f8bcc500 100644 --- a/prog/weaveutil/main.go +++ b/prog/weaveutil/main.go @@ -4,6 +4,10 @@ package main import ( "fmt" "os" + "path/filepath" + "strings" + + weavenet "github.com/weaveworks/weave/net" ) var commands map[string]func([]string) error @@ -28,10 +32,30 @@ func init() { "del-iface": delIface, "setup-iface": setupIface, "list-netdevs": listNetDevs, + "cni-net": cniNet, + "cni-ipam": cniIPAM, } } func main() { + // force re-exec of this binary + selfPath, err := filepath.EvalSymlinks("/proc/self/exe") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + weavenet.WeaveUtilCmd = selfPath + + // If no args passed, act as CNI plugin based on executable name + switch { + case len(os.Args) == 1 && strings.HasSuffix(os.Args[0], "weave-ipam"): + cniIPAM(os.Args) + os.Exit(0) + case len(os.Args) == 1 && strings.HasSuffix(os.Args[0], "weave-net"): + cniNet(os.Args) + os.Exit(0) + } + if len(os.Args) < 2 { usage() os.Exit(1) diff --git a/weave b/weave index 9350e754..6a081dd9 100755 --- a/weave +++ b/weave @@ -1036,7 +1036,7 @@ docker run --rm $(docker_run_options) --pid=host -i \\ -e CNI_VERSION -e CNI_COMMAND -e CNI_CONTAINERID -e CNI_NETNS \\ -e CNI_IFNAME -e CNI_ARGS -e CNI_PATH \\ -v /etc/cni:/etc/cni -v /opt/cni:/opt/cni \$EXTRA_VOLUMES \\ - $PLUGIN_IMAGE $COVERAGE_ARGS $2 +--entrypoint=/usr/bin/weaveutil $EXEC_IMAGE $2 EOF chmod a+x "$1" } @@ -1052,8 +1052,8 @@ EOF setup_cni() { if [ -d $HOST_ROOT/opt/cni/bin ] ; then - create_cni_script $HOST_ROOT/opt/cni/bin/weave-net --cni-net - create_cni_script $HOST_ROOT/opt/cni/bin/weave-ipam --cni-ipam + create_cni_script $HOST_ROOT/opt/cni/bin/weave-net cni-net + create_cni_script $HOST_ROOT/opt/cni/bin/weave-ipam cni-ipam fi if [ -d $HOST_ROOT/etc/cni/net.d -a ! -f $HOST_ROOT/etc/cni/net.d/10-weave.conf ] ; then create_cni_config $HOST_ROOT/etc/cni/net.d/10-weave.conf