Browse code

Merge pull request #31258 from dnephin/fix-override-default-stack-network

Support customizing the default network for a stack

Brian Goff authored on 2017/03/08 04:28:16
Showing 2 changed files
... ...
@@ -16,6 +16,8 @@ import (
16 16
 	runconfigopts "github.com/docker/docker/runconfig/opts"
17 17
 )
18 18
 
19
+const defaultNetwork = "default"
20
+
19 21
 // Services from compose-file types to engine API types
20 22
 // TODO: fix secrets API so that SecretAPIClient is not required here
21 23
 func Services(
... ...
@@ -156,18 +158,15 @@ func convertServiceNetworks(
156 156
 	name string,
157 157
 ) ([]swarm.NetworkAttachmentConfig, error) {
158 158
 	if len(networks) == 0 {
159
-		return []swarm.NetworkAttachmentConfig{
160
-			{
161
-				Target:  namespace.Scope("default"),
162
-				Aliases: []string{name},
163
-			},
164
-		}, nil
159
+		networks = map[string]*composetypes.ServiceNetworkConfig{
160
+			defaultNetwork: {},
161
+		}
165 162
 	}
166 163
 
167 164
 	nets := []swarm.NetworkAttachmentConfig{}
168 165
 	for networkName, network := range networks {
169 166
 		networkConfig, ok := networkConfigs[networkName]
170
-		if !ok {
167
+		if !ok && networkName != defaultNetwork {
171 168
 			return []swarm.NetworkAttachmentConfig{}, fmt.Errorf(
172 169
 				"service %q references network %q, which is not declared", name, networkName)
173 170
 		}
... ...
@@ -179,10 +179,9 @@ func TestConvertEndpointSpec(t *testing.T) {
179 179
 
180 180
 func TestConvertServiceNetworksOnlyDefault(t *testing.T) {
181 181
 	networkConfigs := networkMap{}
182
-	networks := map[string]*composetypes.ServiceNetworkConfig{}
183 182
 
184 183
 	configs, err := convertServiceNetworks(
185
-		networks, networkConfigs, NewNamespace("foo"), "service")
184
+		nil, networkConfigs, NewNamespace("foo"), "service")
186 185
 
187 186
 	expected := []swarm.NetworkAttachmentConfig{
188 187
 		{
... ...
@@ -235,6 +234,31 @@ func TestConvertServiceNetworks(t *testing.T) {
235 235
 	assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(sortedConfigs), expected)
236 236
 }
237 237
 
238
+func TestConvertServiceNetworksCustomDefault(t *testing.T) {
239
+	networkConfigs := networkMap{
240
+		"default": composetypes.NetworkConfig{
241
+			External: composetypes.External{
242
+				External: true,
243
+				Name:     "custom",
244
+			},
245
+		},
246
+	}
247
+	networks := map[string]*composetypes.ServiceNetworkConfig{}
248
+
249
+	configs, err := convertServiceNetworks(
250
+		networks, networkConfigs, NewNamespace("foo"), "service")
251
+
252
+	expected := []swarm.NetworkAttachmentConfig{
253
+		{
254
+			Target:  "custom",
255
+			Aliases: []string{"service"},
256
+		},
257
+	}
258
+
259
+	assert.NilError(t, err)
260
+	assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(configs), expected)
261
+}
262
+
238 263
 type byTargetSort []swarm.NetworkAttachmentConfig
239 264
 
240 265
 func (s byTargetSort) Len() int {