Browse code

Handle NetworkDettach for the case of network-id

When a container is attached to an "--attachable" network, it strictly
forms the attacherKey using either the network-id or network-name
because at the time of attachment, the daemon may not have the network
downloaded locally from the manager. Hence, when the NetworkDettach is
called, it should use either network-name or network-id. This fix
addresses the missing network-id based dettachment case.

Signed-off-by: Madhu Venugopal <madhu@docker.com>

Madhu Venugopal authored on 2016/11/04 07:44:45
Showing 2 changed files
... ...
@@ -793,7 +793,10 @@ func (daemon *Daemon) disconnectFromNetwork(container *container.Container, n li
793 793
 
794 794
 	if daemon.clusterProvider != nil && n.Info().Dynamic() && !container.Managed {
795 795
 		if err := daemon.clusterProvider.DetachNetwork(n.Name(), container.ID); err != nil {
796
-			logrus.Warnf("error detaching from network %s: %v", n, err)
796
+			logrus.Warnf("error detaching from network %s: %v", n.Name(), err)
797
+			if err := daemon.clusterProvider.DetachNetwork(n.ID(), container.ID); err != nil {
798
+				logrus.Warnf("error detaching from network %s: %v", n.ID(), err)
799
+			}
797 800
 		}
798 801
 	}
799 802
 
... ...
@@ -891,6 +894,9 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
891 891
 		if daemon.clusterProvider != nil && nw.Info().Dynamic() && !container.Managed {
892 892
 			if err := daemon.clusterProvider.DetachNetwork(nw.Name(), container.ID); err != nil {
893 893
 				logrus.Warnf("error detaching from network %s: %v", nw.Name(), err)
894
+				if err := daemon.clusterProvider.DetachNetwork(nw.ID(), container.ID); err != nil {
895
+					logrus.Warnf("error detaching from network %s: %v", nw.ID(), err)
896
+				}
894 897
 			}
895 898
 		}
896 899
 
... ...
@@ -295,6 +295,34 @@ func (s *DockerSwarmSuite) TestSwarmContainerEndpointOptions(c *check.C) {
295 295
 	c.Assert(err, check.IsNil)
296 296
 }
297 297
 
298
+func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *check.C) {
299
+	d := s.AddDaemon(c, true, true)
300
+
301
+	out, err := d.Cmd("network", "create", "--attachable", "-d", "overlay", "testnet")
302
+	c.Assert(err, checker.IsNil)
303
+	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
304
+	networkID := strings.TrimSpace(out)
305
+
306
+	out, err = d.Cmd("run", "-d", "--net", networkID, "busybox", "top")
307
+	c.Assert(err, checker.IsNil)
308
+	cID := strings.TrimSpace(out)
309
+	d.waitRun(cID)
310
+
311
+	_, err = d.Cmd("rm", "-f", cID)
312
+	c.Assert(err, checker.IsNil)
313
+
314
+	out, err = d.Cmd("network", "rm", "testnet")
315
+	c.Assert(err, checker.IsNil)
316
+
317
+	checkNetwork := func(*check.C) (interface{}, check.CommentInterface) {
318
+		out, err := d.Cmd("network", "ls")
319
+		c.Assert(err, checker.IsNil)
320
+		return out, nil
321
+	}
322
+
323
+	waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains), "testnet")
324
+}
325
+
298 326
 func (s *DockerSwarmSuite) TestSwarmRemoveInternalNetwork(c *check.C) {
299 327
 	d := s.AddDaemon(c, true, true)
300 328