Merge pull request #2727 from weaveworks/issues/2397-v2-plugin
Create a V2 Docker plugin
This commit is contained in:
commit
8961041469
33 files changed
+804
-307
No files matched your search
+26
-23
@@ -14,29 +14,32 @@ var commands map[string]func([]string) error
|
||||
|
||||
func init() {
|
||||
commands = map[string]func([]string) error{
|
||||
"help": help,
|
||||
"netcheck": netcheck,
|
||||
"docker-tls-args": dockerTLSArgs,
|
||||
"create-datapath": createDatapath,
|
||||
"delete-datapath": deleteDatapath,
|
||||
"check-datapath": checkDatapath,
|
||||
"add-datapath-interface": addDatapathInterface,
|
||||
"create-plugin-network": createPluginNetwork,
|
||||
"remove-plugin-network": removePluginNetwork,
|
||||
"container-addrs": containerAddrs,
|
||||
"process-addrs": processAddrs,
|
||||
"attach-container": attach,
|
||||
"detach-container": detach,
|
||||
"configure-arp": configureARP,
|
||||
"check-iface": checkIface,
|
||||
"del-iface": delIface,
|
||||
"setup-iface": setupIface,
|
||||
"list-netdevs": listNetDevs,
|
||||
"cni-net": cniNet,
|
||||
"cni-ipam": cniIPAM,
|
||||
"expose-nat": exposeNAT,
|
||||
"bridge-ip": bridgeIP,
|
||||
"unique-id": uniqueID,
|
||||
"help": help,
|
||||
"netcheck": netcheck,
|
||||
"docker-tls-args": dockerTLSArgs,
|
||||
"create-datapath": createDatapath,
|
||||
"delete-datapath": deleteDatapath,
|
||||
"check-datapath": checkDatapath,
|
||||
"add-datapath-interface": addDatapathInterface,
|
||||
"create-plugin-network": createPluginNetwork,
|
||||
"remove-plugin-network": removePluginNetwork,
|
||||
"container-addrs": containerAddrs,
|
||||
"process-addrs": processAddrs,
|
||||
"attach-container": attach,
|
||||
"detach-container": detach,
|
||||
"configure-arp": configureARP,
|
||||
"check-iface": checkIface,
|
||||
"del-iface": delIface,
|
||||
"setup-iface": setupIface,
|
||||
"list-netdevs": listNetDevs,
|
||||
"cni-net": cniNet,
|
||||
"cni-ipam": cniIPAM,
|
||||
"expose-nat": exposeNAT,
|
||||
"bridge-ip": bridgeIP,
|
||||
"unique-id": uniqueID,
|
||||
"swarm-manager-peers": swarmManagerPeers,
|
||||
"is-swarm-manager": isSwarmManager,
|
||||
"is-docker-plugin-enabled": isDockerPluginEnabled,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
docker "github.com/fsouza/go-dockerclient"
|
||||
|
||||
"github.com/weaveworks/weave/plugin/net"
|
||||
@@ -71,6 +73,37 @@ while Weave is not running`, networkName, containers)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exits with 0 if the given plugin (v2) is enabled.
|
||||
//
|
||||
// Any failure due to missing plugin support is non-harmful as plugin (v2)
|
||||
// cannot be enabled when Docker does not support it.
|
||||
func isDockerPluginEnabled(args []string) error {
|
||||
if len(args) != 1 {
|
||||
cmdUsage("is-docker-plugin-enabled", "<plugin-name>")
|
||||
}
|
||||
|
||||
pluginName := args[0]
|
||||
|
||||
// This is messed up: we are using docker/docker/client instead of
|
||||
// fsouza/go-dockerclient because the latter does not support plugins.
|
||||
c, err := client.NewEnvClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to connect to docker: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
p, _, err := c.PluginInspectWithRaw(ctx, pluginName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !p.Enabled {
|
||||
return fmt.Errorf("plugin %q is disabled", pluginName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newDockerClient() (*docker.Client, error) {
|
||||
// API 1.21 is the first version that supports docker network
|
||||
// commands
|
||||
@@ -82,5 +115,6 @@ func newDockerClient() (*docker.Client, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to connect to docker: %s", err)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const DOCKER_API_VERSION = "1.26"
|
||||
|
||||
func isSwarmManager(args []string) error {
|
||||
info, err := dockerInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// hack-y way to denote that a node does not belong to any swarm
|
||||
if info.Swarm.LocalNodeState != swarm.LocalNodeStateActive {
|
||||
os.Exit(20)
|
||||
}
|
||||
|
||||
if !info.Swarm.ControlAvailable {
|
||||
return fmt.Errorf("node is not a manager")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func swarmManagerPeers(args []string) error {
|
||||
info, err := dockerInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, managerNode := range info.Swarm.RemoteManagers {
|
||||
ip, err := ipFromAddr(managerNode.Addr)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "ipFromAddr")
|
||||
}
|
||||
fmt.Println(ip)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func dockerInfo() (*docker.DockerInfo, error) {
|
||||
client, err := docker.NewVersionedClientFromEnv(DOCKER_API_VERSION)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "docker.NewVersionedClientFromEnv")
|
||||
}
|
||||
|
||||
info, err := client.Info()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "docker.Info")
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func ipFromAddr(addr string) (string, error) {
|
||||
parts := strings.Split(addr, ":")
|
||||
if len(parts) != 2 {
|
||||
return "", fmt.Errorf("invalid address: %q", addr)
|
||||
}
|
||||
|
||||
return parts[0], nil
|
||||
}
|
||||
Reference in new issue
Block a user