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
|
||||
|
||||
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()
|
||||
|
||||
+7
-4
@@ -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
|
||||
|
||||
Reference in new issue
Block a user