Factor out Plugin variables into Plugin and plugin.Config structs
This commit is contained in:
1 parent
280651da9c
commit
4f485f856b
4 files changed
+84
-65
No files matched your search
+33
-11
@@ -24,40 +24,62 @@ const (
|
|||||||
|
|
||||||
var Log = common.Log
|
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)
|
weave := weaveapi.NewClient(weaveAPIAddr, Log)
|
||||||
|
|
||||||
Log.Info("Waiting for Weave API Server...")
|
Log.Info("Waiting for Weave API Server...")
|
||||||
weave.WaitAPIServer(30)
|
weave.WaitAPIServer(30)
|
||||||
Log.Info("Finished waiting for Weave API Server")
|
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)
|
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)
|
endChan := make(chan error, 1)
|
||||||
|
|
||||||
if address != "" {
|
if plugin.Socket != "" {
|
||||||
globalListener, err := listenAndServe(dockerClient, weave, address, endChan, "global", false, dns, isPluginV2, forceMulticast)
|
globalListener, err := listenAndServe(dockerClient, weave, plugin.Socket, endChan, "global", false, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer os.Remove(address)
|
defer os.Remove(plugin.Socket)
|
||||||
defer globalListener.Close()
|
defer globalListener.Close()
|
||||||
}
|
}
|
||||||
if meshAddress != "" {
|
if plugin.MeshSocket != "" {
|
||||||
meshListener, err := listenAndServe(dockerClient, weave, meshAddress, endChan, "local", true, dns, isPluginV2, forceMulticast)
|
meshListener, err := listenAndServe(dockerClient, weave, plugin.MeshSocket, endChan, "local", true, plugin.DNS, plugin.EnableV2, plugin.EnableV2Multicast)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer os.Remove(meshAddress)
|
defer os.Remove(plugin.MeshSocket)
|
||||||
defer meshListener.Close()
|
defer meshListener.Close()
|
||||||
if !isPluginV2 {
|
if !plugin.EnableV2 {
|
||||||
Log.Printf("Creating default %q network", defaultNetwork)
|
Log.Printf("Creating default %q network", defaultNetwork)
|
||||||
options := map[string]interface{}{MulticastOption: "true"}
|
options := map[string]interface{}{MulticastOption: "true"}
|
||||||
dockerClient.EnsureNetwork(defaultNetwork, pluginNameFromAddress(meshAddress), defaultSubnet, options)
|
dockerClient.EnsureNetwork(defaultNetwork, pluginNameFromAddress(plugin.MeshSocket), plugin.DefaultSubnet, options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ready()
|
ready()
|
||||||
|
|||||||
+7
-4
@@ -6,13 +6,16 @@ type Status struct {
|
|||||||
Version int
|
Version int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStatus(address, meshAddress string, isPluginV2 bool) *Status {
|
func (plugin *Plugin) NewStatus() *Status {
|
||||||
|
if plugin == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
status := &Status{
|
status := &Status{
|
||||||
DriverName: pluginNameFromAddress(address),
|
DriverName: pluginNameFromAddress(plugin.Socket),
|
||||||
MeshDriverName: pluginNameFromAddress(meshAddress),
|
MeshDriverName: pluginNameFromAddress(plugin.MeshSocket),
|
||||||
Version: 1,
|
Version: 1,
|
||||||
}
|
}
|
||||||
if isPluginV2 {
|
if plugin.EnableV2 {
|
||||||
status.Version = 2
|
status.Version = 2
|
||||||
}
|
}
|
||||||
return status
|
return status
|
||||||
|
|||||||
+2
-2
@@ -299,7 +299,7 @@ type WeaveStatus struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read-only functions, suitable for exposing on an unprotected socket
|
// 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 {
|
status := func() WeaveStatus {
|
||||||
return WeaveStatus{
|
return WeaveStatus{
|
||||||
waitReady.IsDone(),
|
waitReady.IsDone(),
|
||||||
@@ -309,7 +309,7 @@ func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRout
|
|||||||
ipam.NewStatus(allocator, defaultSubnet),
|
ipam.NewStatus(allocator, defaultSubnet),
|
||||||
nameserver.NewStatus(ns, dnsserver),
|
nameserver.NewStatus(ns, dnsserver),
|
||||||
proxy.NewStatus(prxy),
|
proxy.NewStatus(prxy),
|
||||||
pluginStatus,
|
plugin.NewStatus(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
muxRouter.Methods("GET").Path("/report").Headers("Accept", "application/json").HandlerFunc(
|
muxRouter.Methods("GET").Path("/report").Headers("Accept", "application/json").HandlerFunc(
|
||||||
|
|||||||
+42
-48
@@ -132,40 +132,36 @@ func main() {
|
|||||||
runtime.GOMAXPROCS(procs)
|
runtime.GOMAXPROCS(procs)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
justVersion bool
|
justVersion bool
|
||||||
config mesh.Config
|
config mesh.Config
|
||||||
bridgeConfig weavenet.BridgeConfig
|
bridgeConfig weavenet.BridgeConfig
|
||||||
networkConfig weave.NetworkConfig
|
networkConfig weave.NetworkConfig
|
||||||
protocolMinVersion int
|
protocolMinVersion int
|
||||||
resume bool
|
resume bool
|
||||||
routerName string
|
routerName string
|
||||||
nickName string
|
nickName string
|
||||||
password string
|
password string
|
||||||
pktdebug bool
|
pktdebug bool
|
||||||
logLevel = "info"
|
logLevel = "info"
|
||||||
prof string
|
prof string
|
||||||
bufSzMB int
|
bufSzMB int
|
||||||
noDiscovery bool
|
noDiscovery bool
|
||||||
httpAddr string
|
httpAddr string
|
||||||
statusAddr string
|
statusAddr string
|
||||||
ipamConfig ipamConfig
|
ipamConfig ipamConfig
|
||||||
dockerAPI string
|
dockerAPI string
|
||||||
peers []string
|
peers []string
|
||||||
noDNS bool
|
noDNS bool
|
||||||
dnsConfig dnsConfig
|
dnsConfig dnsConfig
|
||||||
trustedSubnetStr string
|
trustedSubnetStr string
|
||||||
dbPrefix string
|
dbPrefix string
|
||||||
hostRoot string
|
hostRoot string
|
||||||
procPath string
|
procPath string
|
||||||
discoveryEndpoint string
|
discoveryEndpoint string
|
||||||
token string
|
token string
|
||||||
advertiseAddress string
|
advertiseAddress string
|
||||||
pluginSocket string
|
pluginConfig plugin.Config
|
||||||
pluginMeshSocket string
|
defaultDockerHost = getenvOrDefault("DOCKER_HOST", "unix:///var/run/docker.sock")
|
||||||
enablePlugin bool
|
|
||||||
enablePluginV2 bool
|
|
||||||
enablePluginV2Multicast bool
|
|
||||||
defaultDockerHost = getenvOrDefault("DOCKER_HOST", "unix:///var/run/docker.sock")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit")
|
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(&token, []string{"-token"}, "", "token for peer discovery")
|
||||||
mflag.StringVar(&advertiseAddress, []string{"-advertise-address"}, "", "address to advertise 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(&pluginConfig.Enable, []string{"-plugin"}, false, "enable Docker plugin (v1)")
|
||||||
mflag.BoolVar(&enablePluginV2, []string{"-plugin-v2"}, false, "enable Docker plugin (v2)")
|
mflag.BoolVar(&pluginConfig.EnableV2, []string{"-plugin-v2"}, false, "enable Docker plugin (v2)")
|
||||||
mflag.BoolVar(&enablePluginV2Multicast, []string{"-plugin-v2-multicast"}, false, "enable multicast for Docker plugin (v2)")
|
mflag.BoolVar(&pluginConfig.EnableV2Multicast, []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(&pluginConfig.Socket, []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.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)
|
proxyConfig := configureProxy(version, defaultDockerHost)
|
||||||
if bridgeConfig.AWSVPC {
|
if bridgeConfig.AWSVPC {
|
||||||
@@ -427,11 +423,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
checkFatal(router.CreateRestartSentinel())
|
checkFatal(router.CreateRestartSentinel())
|
||||||
|
|
||||||
// Slightly ugly that plugin status is separated from the plugin.Start call
|
pluginConfig.DNS = !noDNS
|
||||||
var pluginStatus *plugin.Status
|
pluginConfig.DefaultSubnet = defaultSubnet.String()
|
||||||
if enablePlugin || enablePluginV2 {
|
plugin := plugin.NewPlugin(pluginConfig)
|
||||||
pluginStatus = plugin.NewStatus(pluginSocket, pluginMeshSocket, enablePluginV2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The weave script always waits for a status call to succeed,
|
// The weave script always waits for a status call to succeed,
|
||||||
// so there is no point in doing "weave launch --http-addr ''".
|
// so there is no point in doing "weave launch --http-addr ''".
|
||||||
@@ -445,7 +439,7 @@ func main() {
|
|||||||
ns.HandleHTTP(muxRouter, dockerCli)
|
ns.HandleHTTP(muxRouter, dockerCli)
|
||||||
}
|
}
|
||||||
router.HandleHTTP(muxRouter)
|
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())
|
HandleHTTPPeer(muxRouter, allocator, discoveryEndpoint, token, name.String())
|
||||||
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver))
|
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver))
|
||||||
if proxy != nil {
|
if proxy != nil {
|
||||||
@@ -458,7 +452,7 @@ func main() {
|
|||||||
|
|
||||||
if statusAddr != "" {
|
if statusAddr != "" {
|
||||||
muxRouter := mux.NewRouter()
|
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))
|
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver))
|
||||||
statusMux := http.NewServeMux()
|
statusMux := http.NewServeMux()
|
||||||
statusMux.Handle("/", muxRouter)
|
statusMux.Handle("/", muxRouter)
|
||||||
@@ -466,8 +460,8 @@ func main() {
|
|||||||
go listenAndServeHTTP(statusAddr, statusMux)
|
go listenAndServeHTTP(statusAddr, statusMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
if enablePlugin || enablePluginV2 {
|
if plugin != nil {
|
||||||
go plugin.Start(httpAddr, dockerCli, pluginSocket, pluginMeshSocket, !noDNS, enablePluginV2, enablePluginV2Multicast, defaultSubnet.String(), waitReady.Add())
|
go plugin.Start(httpAddr, dockerCli, waitReady.Add())
|
||||||
}
|
}
|
||||||
|
|
||||||
if bridgeConfig.AWSVPC {
|
if bridgeConfig.AWSVPC {
|
||||||
|
|||||||
Reference in new issue
Block a user