Browse code

Reworked ReleaseNetwork to make use of libnetwork's new LeaveAll API

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

Madhu Venugopal authored on 2015/06/21 09:09:36
Showing 1 changed files
... ...
@@ -980,33 +980,33 @@ func (container *Container) ReleaseNetwork() {
980 980
 		return
981 981
 	}
982 982
 
983
-	// If the container is not attached to any network do not try
984
-	// to release network and generate spurious error messages.
985
-	if container.NetworkSettings.NetworkID == "" {
986
-		return
987
-	}
988
-
989
-	n, err := container.daemon.netController.NetworkByID(container.NetworkSettings.NetworkID)
983
+	err := container.daemon.netController.LeaveAll(container.ID)
990 984
 	if err != nil {
991
-		logrus.Errorf("error locating network id %s: %v", container.NetworkSettings.NetworkID, err)
985
+		logrus.Errorf("Leave all failed for  %s: %v", container.ID, err)
992 986
 		return
993 987
 	}
994 988
 
995
-	ep, err := n.EndpointByID(container.NetworkSettings.EndpointID)
996
-	if err != nil {
997
-		logrus.Errorf("error locating endpoint id %s: %v", container.NetworkSettings.EndpointID, err)
998
-		return
999
-	}
989
+	eid := container.NetworkSettings.EndpointID
990
+	nid := container.NetworkSettings.NetworkID
1000 991
 
1001
-	if err := ep.Leave(container.ID); err != nil {
1002
-		logrus.Errorf("leaving endpoint failed: %v", err)
1003
-	}
992
+	container.NetworkSettings = &network.Settings{}
1004 993
 
1005
-	if err := ep.Delete(); err != nil {
1006
-		logrus.Errorf("deleting endpoint failed: %v", err)
994
+	// In addition to leaving all endpoints, delete implicitly created endpoint
995
+	if container.Config.PublishService == "" && eid != "" && nid != "" {
996
+		n, err := container.daemon.netController.NetworkByID(nid)
997
+		if err != nil {
998
+			logrus.Errorf("error locating network id %s: %v", nid, err)
999
+			return
1000
+		}
1001
+		ep, err := n.EndpointByID(eid)
1002
+		if err != nil {
1003
+			logrus.Errorf("error locating endpoint id %s: %v", eid, err)
1004
+			return
1005
+		}
1006
+		if err := ep.Delete(); err != nil {
1007
+			logrus.Errorf("deleting endpoint failed: %v", err)
1008
+		}
1007 1009
 	}
1008
-
1009
-	container.NetworkSettings = &network.Settings{}
1010 1010
 }
1011 1011
 
1012 1012
 func disableAllActiveLinks(container *Container) {