diff --git a/plugin/plugin.go b/plugin/plugin.go index 9021ebcf..c1890193 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -24,40 +24,62 @@ const ( var Log = common.Log -func Start(weaveAPIAddr string, dockerClient *docker.Client, address string, meshAddress string, dns bool, isPluginV2, forceMulticast bool, defaultSubnet string, ready func()) { +type Config struct { + Socket string + MeshSocket string + Enable bool + EnableV2 bool + EnableV2Multicast bool + DNS bool + DefaultSubnet string +} + +type Plugin struct { + Config +} + +func NewPlugin(config Config) *Plugin { + if !config.Enable && !config.EnableV2 { + return nil + } + plugin := &Plugin{Config: config} + return plugin +} + +func (plugin *Plugin) Start(weaveAPIAddr string, dockerClient *docker.Client, ready func()) { weave := weaveapi.NewClient(weaveAPIAddr, Log) Log.Info("Waiting for Weave API Server...") weave.WaitAPIServer(30) Log.Info("Finished waiting for Weave API Server") - if err := run(dockerClient, weave, address, meshAddress, dns, isPluginV2, forceMulticast, defaultSubnet, ready); err != nil { + if err := plugin.run(dockerClient, weave, ready); err != nil { Log.Fatal(err) } } -func run(dockerClient *docker.Client, weave *weaveapi.Client, address, meshAddress string, dns, isPluginV2, forceMulticast bool, defaultSubnet string, ready func()) error { +func (plugin *Plugin) run(dockerClient *docker.Client, weave *weaveapi.Client, ready func()) error { endChan := make(chan error, 1) - if address != "" { - globalListener, err := listenAndServe(dockerClient, weave, address, endChan, "global", false, dns, isPluginV2, forceMulticast) + if plugin.Socket != "" { + globalListener, err := listenAndServe(dockerClient, weave, plugin.Socket, endChan, "global", false, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast) if err != nil { return err } - defer os.Remove(address) + defer os.Remove(plugin.Socket) defer globalListener.Close() } - if meshAddress != "" { - meshListener, err := listenAndServe(dockerClient, weave, meshAddress, endChan, "local", true, dns, isPluginV2, forceMulticast) + if plugin.MeshSocket != "" { + meshListener, err := listenAndServe(dockerClient, weave, plugin.MeshSocket, endChan, "local", true, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast) if err != nil { return err } - defer os.Remove(meshAddress) + defer os.Remove(plugin.MeshSocket) defer meshListener.Close() - if !isPluginV2 { + if !plugin.EnableV2 { Log.Printf("Creating default %q network", defaultNetwork) options := map[string]interface{}{MulticastOption: "true"} - dockerClient.EnsureNetwork(defaultNetwork, pluginNameFromAddress(meshAddress), defaultSubnet, options) + dockerClient.EnsureNetwork(defaultNetwork, pluginNameFromAddress(plugin.MeshSocket), plugin.DefaultSubnet, options) } } ready() diff --git a/plugin/status.go b/plugin/status.go index fc44990b..d22863a7 100644 --- a/plugin/status.go +++ b/plugin/status.go @@ -6,13 +6,16 @@ type Status struct { Version int } -func NewStatus(address, meshAddress string, isPluginV2 bool) *Status { +func (plugin *Plugin) NewStatus() *Status { + if plugin == nil { + return nil + } status := &Status{ - DriverName: pluginNameFromAddress(address), - MeshDriverName: pluginNameFromAddress(meshAddress), + DriverName: pluginNameFromAddress(plugin.Socket), + MeshDriverName: pluginNameFromAddress(plugin.MeshSocket), Version: 1, } - if isPluginV2 { + if plugin.EnableV2 { status.Version = 2 } return status diff --git a/prog/weaver/http.go b/prog/weaver/http.go index 34564344..2df37562 100644 --- a/prog/weaver/http.go +++ b/prog/weaver/http.go @@ -299,7 +299,7 @@ type WeaveStatus struct { } // Read-only functions, suitable for exposing on an unprotected socket -func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRouter, allocator *ipam.Allocator, defaultSubnet address.CIDR, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer, prxy *proxy.Proxy, pluginStatus *plugin.Status, waitReady *common.WaitGroup) { +func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRouter, allocator *ipam.Allocator, defaultSubnet address.CIDR, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer, prxy *proxy.Proxy, plugin *plugin.Plugin, waitReady *common.WaitGroup) { status := func() WeaveStatus { return WeaveStatus{ waitReady.IsDone(), @@ -309,7 +309,7 @@ func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRout ipam.NewStatus(allocator, defaultSubnet), nameserver.NewStatus(ns, dnsserver), proxy.NewStatus(prxy), - pluginStatus, + plugin.NewStatus(), } } muxRouter.Methods("GET").Path("/report").Headers("Accept", "application/json").HandlerFunc( diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 2a01c6f0..51f1df97 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -132,40 +132,36 @@ func main() { runtime.GOMAXPROCS(procs) var ( - justVersion bool - config mesh.Config - bridgeConfig weavenet.BridgeConfig - networkConfig weave.NetworkConfig - protocolMinVersion int - resume bool - routerName string - nickName string - password string - pktdebug bool - logLevel = "info" - prof string - bufSzMB int - noDiscovery bool - httpAddr string - statusAddr string - ipamConfig ipamConfig - dockerAPI string - peers []string - noDNS bool - dnsConfig dnsConfig - trustedSubnetStr string - dbPrefix string - hostRoot string - procPath string - discoveryEndpoint string - token string - advertiseAddress string - pluginSocket string - pluginMeshSocket string - enablePlugin bool - enablePluginV2 bool - enablePluginV2Multicast bool - defaultDockerHost = getenvOrDefault("DOCKER_HOST", "unix:///var/run/docker.sock") + justVersion bool + config mesh.Config + bridgeConfig weavenet.BridgeConfig + networkConfig weave.NetworkConfig + protocolMinVersion int + resume bool + routerName string + nickName string + password string + pktdebug bool + logLevel = "info" + prof string + bufSzMB int + noDiscovery bool + httpAddr string + statusAddr string + ipamConfig ipamConfig + dockerAPI string + peers []string + noDNS bool + dnsConfig dnsConfig + trustedSubnetStr string + dbPrefix string + hostRoot string + procPath string + discoveryEndpoint string + token string + advertiseAddress string + pluginConfig plugin.Config + defaultDockerHost = getenvOrDefault("DOCKER_HOST", "unix:///var/run/docker.sock") ) mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit") @@ -211,11 +207,11 @@ func main() { mflag.StringVar(&token, []string{"-token"}, "", "token for peer discovery") mflag.StringVar(&advertiseAddress, []string{"-advertise-address"}, "", "address to advertise for peer discovery") - mflag.BoolVar(&enablePlugin, []string{"-plugin"}, false, "enable Docker plugin (v1)") - mflag.BoolVar(&enablePluginV2, []string{"-plugin-v2"}, false, "enable Docker plugin (v2)") - mflag.BoolVar(&enablePluginV2Multicast, []string{"-plugin-v2-multicast"}, false, "enable multicast for Docker plugin (v2)") - mflag.StringVar(&pluginSocket, []string{"-plugin-socket"}, "/run/docker/plugins/weave.sock", "plugin socket on which to listen") - mflag.StringVar(&pluginMeshSocket, []string{"-plugin-mesh-socket"}, "/run/docker/plugins/weavemesh.sock", "plugin socket on which to listen in mesh mode") + mflag.BoolVar(&pluginConfig.Enable, []string{"-plugin"}, false, "enable Docker plugin (v1)") + mflag.BoolVar(&pluginConfig.EnableV2, []string{"-plugin-v2"}, false, "enable Docker plugin (v2)") + mflag.BoolVar(&pluginConfig.EnableV2Multicast, []string{"-plugin-v2-multicast"}, false, "enable multicast for Docker plugin (v2)") + mflag.StringVar(&pluginConfig.Socket, []string{"-plugin-socket"}, "/run/docker/plugins/weave.sock", "plugin socket on which to listen") + mflag.StringVar(&pluginConfig.MeshSocket, []string{"-plugin-mesh-socket"}, "/run/docker/plugins/weavemesh.sock", "plugin socket on which to listen in mesh mode") proxyConfig := configureProxy(version, defaultDockerHost) if bridgeConfig.AWSVPC { @@ -427,11 +423,9 @@ func main() { } checkFatal(router.CreateRestartSentinel()) - // Slightly ugly that plugin status is separated from the plugin.Start call - var pluginStatus *plugin.Status - if enablePlugin || enablePluginV2 { - pluginStatus = plugin.NewStatus(pluginSocket, pluginMeshSocket, enablePluginV2) - } + pluginConfig.DNS = !noDNS + pluginConfig.DefaultSubnet = defaultSubnet.String() + plugin := plugin.NewPlugin(pluginConfig) // The weave script always waits for a status call to succeed, // so there is no point in doing "weave launch --http-addr ''". @@ -445,7 +439,7 @@ func main() { ns.HandleHTTP(muxRouter, dockerCli) } router.HandleHTTP(muxRouter) - HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver, proxy, pluginStatus, &waitReady) + HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver, proxy, plugin, &waitReady) HandleHTTPPeer(muxRouter, allocator, discoveryEndpoint, token, name.String()) muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver)) if proxy != nil { @@ -458,7 +452,7 @@ func main() { if statusAddr != "" { muxRouter := mux.NewRouter() - HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver, proxy, pluginStatus, &waitReady) + HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver, proxy, plugin, &waitReady) muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver)) statusMux := http.NewServeMux() statusMux.Handle("/", muxRouter) @@ -466,8 +460,8 @@ func main() { go listenAndServeHTTP(statusAddr, statusMux) } - if enablePlugin || enablePluginV2 { - go plugin.Start(httpAddr, dockerCli, pluginSocket, pluginMeshSocket, !noDNS, enablePluginV2, enablePluginV2Multicast, defaultSubnet.String(), waitReady.Add()) + if plugin != nil { + go plugin.Start(httpAddr, dockerCli, waitReady.Add()) } if bridgeConfig.AWSVPC {