Implement CNI plugin via weaveutil
This commit is contained in:
1 parent
40b3a49c90
commit
5630a01a8c
4 files changed
+56
-4
No files matched your search
+3
-1
@@ -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=<ns-path> weaveutil <cmd> [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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user