diff --git a/daemon/cluster/cluster.go b/daemon/cluster/cluster.go index b5a626596d..476e0097bb 100644 --- a/daemon/cluster/cluster.go +++ b/daemon/cluster/cluster.go @@ -351,7 +351,7 @@ func (c *Cluster) startNewNode(conf nodeStartConfig) (*node, error) { c.actualLocalAddr = actualLocalAddr // not saved c.saveState(conf) - c.config.Backend.SetClusterProvider(c) + c.config.Backend.DaemonJoinsCluster(c) go func() { err := detectLockedError(n.Err(ctx)) if err != nil { @@ -724,6 +724,7 @@ func (c *Cluster) Leave(force bool) error { if err := c.clearState(); err != nil { return err } + return nil } @@ -751,7 +752,7 @@ func (c *Cluster) clearState() error { if err := os.MkdirAll(c.root, 0700); err != nil { return err } - c.config.Backend.SetClusterProvider(nil) + c.config.Backend.DaemonLeavesCluster() return nil } diff --git a/daemon/cluster/executor/backend.go b/daemon/cluster/executor/backend.go index 17ede3341a..8d536fb358 100644 --- a/daemon/cluster/executor/backend.go +++ b/daemon/cluster/executor/backend.go @@ -47,7 +47,8 @@ type Backend interface { VolumeCreate(name, driverName string, opts, labels map[string]string) (*types.Volume, error) Containers(config *types.ContainerListOptions) ([]*types.Container, error) SetNetworkBootstrapKeys([]*networktypes.EncryptionKey) error - SetClusterProvider(provider cluster.Provider) + DaemonJoinsCluster(provider cluster.Provider) + DaemonLeavesCluster() IsSwarmCompatible() error SubscribeToEvents(since, until time.Time, filter filters.Args) ([]events.Message, chan interface{}) UnsubscribeFromEvents(listener chan interface{}) diff --git a/daemon/daemon.go b/daemon/daemon.go index f0a1760fda..91f55f9d30 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -446,8 +446,22 @@ func (daemon *Daemon) registerLink(parent, child *container.Container, alias str return nil } -// SetClusterProvider sets a component for querying the current cluster state. -func (daemon *Daemon) SetClusterProvider(clusterProvider cluster.Provider) { +// DaemonJoinsCluster informs the daemon has joined the cluster and provides +// the handler to query the cluster component +func (daemon *Daemon) DaemonJoinsCluster(clusterProvider cluster.Provider) { + daemon.setClusterProvider(clusterProvider) +} + +// DaemonLeavesCluster informs the daemon has left the cluster +func (daemon *Daemon) DaemonLeavesCluster() { + // Daemon is in charge of removing the attachable networks with + // connected containers when the node leaves the swarm + daemon.clearAttachableNetworks() + daemon.setClusterProvider(nil) +} + +// setClusterProvider sets a component for querying the current cluster state. +func (daemon *Daemon) setClusterProvider(clusterProvider cluster.Provider) { daemon.clusterProvider = clusterProvider daemon.netController.SetClusterProvider(clusterProvider) } diff --git a/daemon/network.go b/daemon/network.go index daa1f3718c..ab8fd88da8 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -468,3 +468,31 @@ func (daemon *Daemon) deleteNetwork(networkID string, dynamic bool) error { func (daemon *Daemon) GetNetworks() []libnetwork.Network { return daemon.getAllNetworks() } + +// clearAttachableNetworks removes the attachable networks +// after disconnecting any connected container +func (daemon *Daemon) clearAttachableNetworks() { + for _, n := range daemon.GetNetworks() { + if !n.Info().Attachable() { + continue + } + for _, ep := range n.Endpoints() { + epInfo := ep.Info() + if epInfo == nil { + continue + } + sb := epInfo.Sandbox() + if sb == nil { + continue + } + containerID := sb.ContainerID() + if err := daemon.DisconnectContainerFromNetwork(containerID, n.ID(), true); err != nil { + logrus.Warnf("Failed to disconnect container %s from swarm network %s on cluster leave: %v", + containerID, n.Name(), err) + } + } + if err := daemon.DeleteManagedNetwork(n.ID()); err != nil { + logrus.Warnf("Failed to remove swarm network %s on cluster leave: %v", n.Name(), err) + } + } +} diff --git a/integration-cli/docker_cli_swarm_test.go b/integration-cli/docker_cli_swarm_test.go index 1ecfd07433..7e774af853 100644 --- a/integration-cli/docker_cli_swarm_test.go +++ b/integration-cli/docker_cli_swarm_test.go @@ -393,6 +393,33 @@ func (s *DockerSwarmSuite) TestOverlayAttachable(c *check.C) { c.Assert(strings.TrimSpace(out), checker.Equals, "true") } +func (s *DockerSwarmSuite) TestOverlayAttachableOnSwarmLeave(c *check.C) { + d := s.AddDaemon(c, true, true) + + // Create an attachable swarm network + nwName := "attovl" + out, err := d.Cmd("network", "create", "-d", "overlay", "--attachable", nwName) + c.Assert(err, checker.IsNil, check.Commentf(out)) + + // Connect a container to the network + out, err = d.Cmd("run", "-d", "--network", nwName, "--name", "c1", "busybox", "top") + c.Assert(err, checker.IsNil, check.Commentf(out)) + + // Leave the swarm + err = d.Leave(true) + c.Assert(err, checker.IsNil) + + // Check the container is disconnected + out, err = d.Cmd("inspect", "c1", "--format", "{{.NetworkSettings.Networks."+nwName+"}}") + c.Assert(err, checker.IsNil) + c.Assert(strings.TrimSpace(out), checker.Equals, "") + + // Check the network is gone + out, err = d.Cmd("network", "ls", "--format", "{{.Name}}") + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Not(checker.Contains), nwName) +} + func (s *DockerSwarmSuite) TestSwarmRemoveInternalNetwork(c *check.C) { d := s.AddDaemon(c, true, true)