Browse code

Merge pull request #29994 from vdemeester/29974-fix-external-network

[1.13.x] Few stack deploy network fixes

Victor Vieux authored on 2017/01/10 07:40:54
Showing 1 changed files
... ...
@@ -9,9 +9,6 @@ import (
9 9
 	"strings"
10 10
 	"time"
11 11
 
12
-	"github.com/spf13/cobra"
13
-	"golang.org/x/net/context"
14
-
15 12
 	"github.com/aanand/compose-file/loader"
16 13
 	composetypes "github.com/aanand/compose-file/types"
17 14
 	"github.com/docker/docker/api/types"
... ...
@@ -25,6 +22,8 @@ import (
25 25
 	"github.com/docker/docker/opts"
26 26
 	runconfigopts "github.com/docker/docker/runconfig/opts"
27 27
 	"github.com/docker/go-connections/nat"
28
+	"github.com/spf13/cobra"
29
+	"golang.org/x/net/context"
28 30
 )
29 31
 
30 32
 const (
... ...
@@ -123,7 +122,8 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo
123 123
 
124 124
 	namespace := namespace{name: opts.namespace}
125 125
 
126
-	networks, externalNetworks := convertNetworks(namespace, config.Networks)
126
+	serviceNetworks := getServicesDeclaredNetworks(config.Services)
127
+	networks, externalNetworks := convertNetworks(namespace, config.Networks, serviceNetworks)
127 128
 	if err := validateExternalNetworks(ctx, dockerCli, externalNetworks); err != nil {
128 129
 		return err
129 130
 	}
... ...
@@ -136,6 +136,19 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo
136 136
 	}
137 137
 	return deployServices(ctx, dockerCli, services, namespace, opts.sendRegistryAuth)
138 138
 }
139
+func getServicesDeclaredNetworks(serviceConfigs []composetypes.ServiceConfig) map[string]struct{} {
140
+	serviceNetworks := map[string]struct{}{}
141
+	for _, serviceConfig := range serviceConfigs {
142
+		if len(serviceConfig.Networks) == 0 {
143
+			serviceNetworks["default"] = struct{}{}
144
+			continue
145
+		}
146
+		for network := range serviceConfig.Networks {
147
+			serviceNetworks[network] = struct{}{}
148
+		}
149
+	}
150
+	return serviceNetworks
151
+}
139 152
 
140 153
 func propertyWarnings(properties map[string]string) string {
141 154
 	var msgs []string
... ...
@@ -182,18 +195,17 @@ func getConfigFile(filename string) (*composetypes.ConfigFile, error) {
182 182
 func convertNetworks(
183 183
 	namespace namespace,
184 184
 	networks map[string]composetypes.NetworkConfig,
185
+	servicesNetworks map[string]struct{},
185 186
 ) (map[string]types.NetworkCreate, []string) {
186 187
 	if networks == nil {
187 188
 		networks = make(map[string]composetypes.NetworkConfig)
188 189
 	}
189 190
 
190
-	// TODO: only add default network if it's used
191
-	networks["default"] = composetypes.NetworkConfig{}
192
-
193 191
 	externalNetworks := []string{}
194 192
 	result := make(map[string]types.NetworkCreate)
195 193
 
196
-	for internalName, network := range networks {
194
+	for internalName := range servicesNetworks {
195
+		network := networks[internalName]
197 196
 		if network.External.External {
198 197
 			externalNetworks = append(externalNetworks, network.External.Name)
199 198
 			continue
... ...
@@ -310,7 +322,7 @@ func convertServiceNetworks(
310 310
 		}
311 311
 		target := namespace.scope(networkName)
312 312
 		if networkConfig.External.External {
313
-			target = networkName
313
+			target = networkConfig.External.Name
314 314
 		}
315 315
 		nets = append(nets, swarm.NetworkAttachmentConfig{
316 316
 			Target:  target,