Ejemplo n.º 1
0
func listenAndServe(dockerClient *docker.Client, address, nameserver string, noMulticastRoute bool, endChan chan<- error, scope string, withIpam bool) (net.Listener, error) {
	d, err := netplugin.New(dockerClient, version, nameserver, scope, noMulticastRoute)
	if err != nil {
		return nil, err
	}

	var i ipamapi.Ipam
	if withIpam {
		if i, err = ipamplugin.NewIpam(dockerClient, version); err != nil {
			return nil, err
		}
	}

	var listener net.Listener

	// remove sockets from last invocation
	if err := os.Remove(address); err != nil && !os.IsNotExist(err) {
		return nil, err
	}
	listener, err = net.Listen("unix", address)
	if err != nil {
		return nil, err
	}
	Log.Printf("Listening on %s for %s scope", address, scope)

	go func() {
		endChan <- skel.Listen(listener, d, i)
	}()

	return listener, nil
}
Ejemplo n.º 2
0
Archivo: cni.go Proyecto: n054/weave
func (c *CNIPlugin) CmdDel(args *skel.CmdArgs) error {
	conf, err := loadNetConf(args.StdinData)
	if err != nil {
		return err
	}

	ns, err := netns.GetFromPath(args.Netns)
	if err != nil {
		return err
	}
	defer ns.Close()
	err = weavenet.WithNetNSUnsafe(ns, func() error {
		link, err := netlink.LinkByName(args.IfName)
		if err != nil {
			return err
		}
		return netlink.LinkDel(link)
	})
	if err != nil {
		return fmt.Errorf("error removing interface: %s", err)
	}

	// Default IPAM is Weave's own
	if conf.IPAM.Type == "" {
		err = ipamplugin.NewIpam(c.weave).Release(args)
	} else {
		err = ipam.ExecDel(conf.IPAM.Type, args.StdinData)
	}
	if err != nil {
		return fmt.Errorf("unable to release IP address: %s", err)
	}
	return nil
}
Ejemplo n.º 3
0
Archivo: main.go Proyecto: n054/weave
func main() {
	var (
		justVersion      bool
		cniNet           bool
		cniIpam          bool
		address          string
		meshAddress      string
		logLevel         string
		noMulticastRoute bool
	)

	flag.BoolVar(&justVersion, "version", false, "print version and exit")
	flag.BoolVar(&cniNet, "cni-net", false, "act as a CNI network plugin")
	flag.BoolVar(&cniIpam, "cni-ipam", false, "act as a CNI IPAM plugin")
	flag.StringVar(&logLevel, "log-level", "info", "logging level (debug, info, warning, error)")
	flag.StringVar(&address, "socket", "/run/docker/plugins/weave.sock", "socket on which to listen")
	flag.StringVar(&meshAddress, "meshsocket", "/run/docker/plugins/weavemesh.sock", "socket on which to listen in mesh mode")
	flag.BoolVar(&noMulticastRoute, "no-multicast-route", false, "deprecated (this is now the default)")

	flag.Parse()

	if justVersion {
		fmt.Printf("weave plugin %s\n", version)
		os.Exit(0)
	}

	common.SetLogLevel(logLevel)

	weave := weaveapi.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), Log)

	switch {
	case cniIpam || strings.HasSuffix(os.Args[0], "weave-ipam"):
		i := ipamplugin.NewIpam(weave)
		cni.PluginMain(i.CmdAdd, i.CmdDel)
		os.Exit(0)
	case cniNet || strings.HasSuffix(os.Args[0], "weave-net"):
		n := netplugin.NewCNIPlugin(weave)
		cni.PluginMain(n.CmdAdd, n.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)
	}

	Log.Println("Weave plugin", version, "Command line options:", os.Args[1:])
	if noMulticastRoute {
		Log.Warning("--no-multicast-route option has been removed; multicast is off by default")
	}
	Log.Info(dockerClient.Info())

	err = run(dockerClient, weave, address, meshAddress)
	if err != nil {
		Log.Fatal(err)
	}
}
Ejemplo n.º 4
0
Archivo: cni.go Proyecto: n054/weave
func (c *CNIPlugin) getIP(ipamType string, args *skel.CmdArgs) (result *types.Result, err error) {
	// Default IPAM is Weave's own
	if ipamType == "" {
		result, err = ipamplugin.NewIpam(c.weave).Allocate(args)
	} else {
		result, err = ipam.ExecAdd(ipamType, args.StdinData)
	}
	if err == nil && result.IP4 == nil {
		return nil, fmt.Errorf("IPAM plugin failed to allocate IP address")
	}
	return result, err
}
Ejemplo n.º 5
0
Archivo: main.go Proyecto: brb/weave
func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address string, noMulticastRoute bool, endChan chan<- error, scope string, withIpam bool) (net.Listener, error) {
	d, err := netplugin.New(dockerClient, weave, scope, noMulticastRoute)
	if err != nil {
		return nil, err
	}

	var i ipamapi.Ipam
	if withIpam {
		i = ipamplugin.NewIpam(weave)
	}

	listener, err := weavenet.ListenUnixSocket(address)
	if err != nil {
		return nil, err
	}
	Log.Printf("Listening on %s for %s scope", address, scope)

	go func() {
		endChan <- skel.Listen(listener, d, i)
	}()

	return listener, nil
}
Ejemplo n.º 6
0
Archivo: main.go Proyecto: n054/weave
func listenAndServe(dockerClient *docker.Client, weave *weaveapi.Client, address string, endChan chan<- error, scope string, withIpam bool) (net.Listener, error) {
	name := strings.TrimSuffix(path.Base(address), ".sock")
	d, err := netplugin.New(dockerClient, weave, name, scope)
	if err != nil {
		return nil, err
	}

	var i ipamapi.Ipam
	if withIpam {
		i = ipamplugin.NewIpam(weave)
	}

	listener, err := weavenet.ListenUnixSocket(address)
	if err != nil {
		return nil, err
	}
	Log.Printf("Listening on %s for %s scope", address, scope)

	go func() {
		endChan <- skel.Listen(listener, d, i)
	}()

	return listener, nil
}