Browse code

Support customizing the default network for a stack.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
(cherry picked from commit b7577dd2ad6fbb2cf21baf7ee082d2f605157b2a)
Signed-off-by: Victor Vieux <victorvieux@gmail.com>

Daniel Nephin authored on 2017/02/23 03:52:09
Showing 2 changed files
... ...
@@ -17,6 +17,8 @@ import (
17 17
 	"github.com/docker/go-connections/nat"
18 18
 )
19 19
 
20
+const defaultNetwork = "default"
21
+
20 22
 // Services from compose-file types to engine API types
21 23
 // TODO: fix secrets API so that SecretAPIClient is not required here
22 24
 func Services(
... ...
@@ -157,18 +159,15 @@ func convertServiceNetworks(
157 157
 	name string,
158 158
 ) ([]swarm.NetworkAttachmentConfig, error) {
159 159
 	if len(networks) == 0 {
160
-		return []swarm.NetworkAttachmentConfig{
161
-			{
162
-				Target:  namespace.Scope("default"),
163
-				Aliases: []string{name},
164
-			},
165
-		}, nil
160
+		networks = map[string]*composetypes.ServiceNetworkConfig{
161
+			defaultNetwork: {},
162
+		}
166 163
 	}
167 164
 
168 165
 	nets := []swarm.NetworkAttachmentConfig{}
169 166
 	for networkName, network := range networks {
170 167
 		networkConfig, ok := networkConfigs[networkName]
171
-		if !ok {
168
+		if !ok && networkName != defaultNetwork {
172 169
 			return []swarm.NetworkAttachmentConfig{}, fmt.Errorf(
173 170
 				"service %q references network %q, which is not declared", name, networkName)
174 171
 		}
... ...
@@ -145,10 +145,9 @@ func TestConvertHealthcheckDisableWithTest(t *testing.T) {
145 145
 
146 146
 func TestConvertServiceNetworksOnlyDefault(t *testing.T) {
147 147
 	networkConfigs := networkMap{}
148
-	networks := map[string]*composetypes.ServiceNetworkConfig{}
149 148
 
150 149
 	configs, err := convertServiceNetworks(
151
-		networks, networkConfigs, NewNamespace("foo"), "service")
150
+		nil, networkConfigs, NewNamespace("foo"), "service")
152 151
 
153 152
 	expected := []swarm.NetworkAttachmentConfig{
154 153
 		{
... ...
@@ -201,6 +200,31 @@ func TestConvertServiceNetworks(t *testing.T) {
201 201
 	assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(sortedConfigs), expected)
202 202
 }
203 203
 
204
+func TestConvertServiceNetworksCustomDefault(t *testing.T) {
205
+	networkConfigs := networkMap{
206
+		"default": composetypes.NetworkConfig{
207
+			External: composetypes.External{
208
+				External: true,
209
+				Name:     "custom",
210
+			},
211
+		},
212
+	}
213
+	networks := map[string]*composetypes.ServiceNetworkConfig{}
214
+
215
+	configs, err := convertServiceNetworks(
216
+		networks, networkConfigs, NewNamespace("foo"), "service")
217
+
218
+	expected := []swarm.NetworkAttachmentConfig{
219
+		{
220
+			Target:  "custom",
221
+			Aliases: []string{"service"},
222
+		},
223
+	}
224
+
225
+	assert.NilError(t, err)
226
+	assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(configs), expected)
227
+}
228
+
204 229
 type byTargetSort []swarm.NetworkAttachmentConfig
205 230
 
206 231
 func (s byTargetSort) Len() int {