Browse code

Fix network alias issue

This fix tries to address the issue raised in 33661 where
network alias does not work when connect to a network the second time.

This fix address the issue.

This fix fixes 33661.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/01/09 15:41:48
Showing 2 changed files
... ...
@@ -275,12 +275,11 @@ func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseW
275 275
 		return err
276 276
 	}
277 277
 
278
-	// Always make sure there is no ambiguity with respect to the network ID/name
279
-	nw, err := n.backend.FindNetwork(vars["id"])
280
-	if err != nil {
281
-		return err
282
-	}
283
-	return n.backend.ConnectContainerToNetwork(connect.Container, nw.ID(), connect.EndpointConfig)
278
+	// Unlike other operations, we does not check ambiguity of the name/ID here.
279
+	// The reason is that, In case of attachable network in swarm scope, the actual local network
280
+	// may not be available at the time. At the same time, inside daemon `ConnectContainerToNetwork`
281
+	// does the ambiguity check anyway. Therefore, passing the name to daemon would be enough.
282
+	return n.backend.ConnectContainerToNetwork(connect.Container, vars["id"], connect.EndpointConfig)
284 283
 }
285 284
 
286 285
 func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -20,6 +20,7 @@ import (
20 20
 	"github.com/docker/docker/runconfig"
21 21
 	"github.com/docker/go-connections/nat"
22 22
 	"github.com/docker/libnetwork"
23
+	netconst "github.com/docker/libnetwork/datastore"
23 24
 	"github.com/docker/libnetwork/netlabel"
24 25
 	"github.com/docker/libnetwork/options"
25 26
 	"github.com/docker/libnetwork/types"
... ...
@@ -259,6 +260,13 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n li
259 259
 		}
260 260
 
261 261
 		if sn.Name() == n.Name() {
262
+			// If the network scope is swarm, then this
263
+			// is an attachable network, which may not
264
+			// be locally available previously.
265
+			// So always update.
266
+			if n.Info().Scope() == netconst.SwarmScope {
267
+				continue
268
+			}
262 269
 			// Avoid duplicate config
263 270
 			return nil
264 271
 		}
... ...
@@ -272,10 +280,8 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n li
272 272
 		}
273 273
 	}
274 274
 
275
-	if _, ok := container.NetworkSettings.Networks[n.Name()]; !ok {
276
-		container.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{
277
-			EndpointSettings: endpointConfig,
278
-		}
275
+	container.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{
276
+		EndpointSettings: endpointConfig,
279 277
 	}
280 278
 
281 279
 	return nil