diff --git a/daemon/daemon.go b/daemon/daemon.go index 268a5ce00c..5ae5feea3e 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -720,7 +720,17 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo } } - d.netController, err = initNetworkController(config) + // Discovery is only enabled when the daemon is launched with an address to advertise. When + // initialized, the daemon is registered and we can store the discovery backend as its read-only + // DiscoveryWatcher version. + if config.ClusterStore != "" && config.ClusterAdvertise != "" { + var err error + if d.discoveryWatcher, err = initDiscovery(config.ClusterStore, config.ClusterAdvertise); err != nil { + return nil, fmt.Errorf("discovery initialization failed (%v)", err) + } + } + + d.netController, err = d.initNetworkController(config) if err != nil { return nil, fmt.Errorf("Error initializing network controller: %v", err) } @@ -754,16 +764,6 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo return nil, err } - // Discovery is only enabled when the daemon is launched with an address to advertise. When - // initialized, the daemon is registered and we can store the discovery backend as its read-only - // DiscoveryWatcher version. - if config.ClusterStore != "" && config.ClusterAdvertise != "" { - var err error - if d.discoveryWatcher, err = initDiscovery(config.ClusterStore, config.ClusterAdvertise); err != nil { - return nil, fmt.Errorf("discovery initialization failed (%v)", err) - } - } - d.ID = trustKey.PublicKey().KeyID() d.repository = daemonRepo d.containers = &contStore{s: make(map[string]*Container)} diff --git a/daemon/daemon_unix.go b/daemon/daemon_unix.go index 7b99245ceb..96ef4c04c8 100644 --- a/daemon/daemon_unix.go +++ b/daemon/daemon_unix.go @@ -302,7 +302,7 @@ func isBridgeNetworkDisabled(config *Config) bool { return config.Bridge.Iface == disableNetworkBridge } -func networkOptions(dconfig *Config) ([]nwconfig.Option, error) { +func (daemon *Daemon) networkOptions(dconfig *Config) ([]nwconfig.Option, error) { options := []nwconfig.Option{} if dconfig == nil { return options, nil @@ -330,13 +330,21 @@ func networkOptions(dconfig *Config) ([]nwconfig.Option, error) { options = append(options, nwconfig.OptionKVProviderURL(strings.Join(kv[1:], "://"))) } + if daemon.discoveryWatcher != nil { + options = append(options, nwconfig.OptionDiscoveryWatcher(daemon.discoveryWatcher)) + } + + if dconfig.ClusterAdvertise != "" { + options = append(options, nwconfig.OptionDiscoveryAddress(dconfig.ClusterAdvertise)) + } + options = append(options, nwconfig.OptionLabels(dconfig.Labels)) options = append(options, driverOptions(dconfig)...) return options, nil } -func initNetworkController(config *Config) (libnetwork.NetworkController, error) { - netOptions, err := networkOptions(config) +func (daemon *Daemon) initNetworkController(config *Config) (libnetwork.NetworkController, error) { + netOptions, err := daemon.networkOptions(config) if err != nil { return nil, err } diff --git a/daemon/daemon_windows.go b/daemon/daemon_windows.go index e5513b2032..58223dbcea 100644 --- a/daemon/daemon_windows.go +++ b/daemon/daemon_windows.go @@ -98,7 +98,7 @@ func isBridgeNetworkDisabled(config *Config) bool { return false } -func initNetworkController(config *Config) (libnetwork.NetworkController, error) { +func (daemon *Daemon) initNetworkController(config *Config) (libnetwork.NetworkController, error) { // Set the name of the virtual switch if not specified by -b on daemon start if config.Bridge.VirtualSwitchName == "" { config.Bridge.VirtualSwitchName = defaultVirtualSwitch