Browse code

Create only network that are used

- default network is only created if needed
- it's possible to override default network configuration

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2017/01/09 20:56:58
Showing 1 changed files
... ...
@@ -122,7 +122,8 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo
122 122
 
123 123
 	namespace := namespace{name: opts.namespace}
124 124
 
125
-	networks, externalNetworks := convertNetworks(namespace, config.Networks)
125
+	serviceNetworks := getServicesDeclaredNetworks(config.Services)
126
+	networks, externalNetworks := convertNetworks(namespace, config.Networks, serviceNetworks)
126 127
 	if err := validateExternalNetworks(ctx, dockerCli, externalNetworks); err != nil {
127 128
 		return err
128 129
 	}
... ...
@@ -135,6 +136,19 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo
135 135
 	}
136 136
 	return deployServices(ctx, dockerCli, services, namespace, opts.sendRegistryAuth)
137 137
 }
138
+func getServicesDeclaredNetworks(serviceConfigs []composetypes.ServiceConfig) map[string]struct{} {
139
+	serviceNetworks := map[string]struct{}{}
140
+	for _, serviceConfig := range serviceConfigs {
141
+		if len(serviceConfig.Networks) == 0 {
142
+			serviceNetworks["default"] = struct{}{}
143
+			continue
144
+		}
145
+		for network := range serviceConfig.Networks {
146
+			serviceNetworks[network] = struct{}{}
147
+		}
148
+	}
149
+	return serviceNetworks
150
+}
138 151
 
139 152
 func propertyWarnings(properties map[string]string) string {
140 153
 	var msgs []string
... ...
@@ -181,18 +195,17 @@ func getConfigFile(filename string) (*composetypes.ConfigFile, error) {
181 181
 func convertNetworks(
182 182
 	namespace namespace,
183 183
 	networks map[string]composetypes.NetworkConfig,
184
+	servicesNetworks map[string]struct{},
184 185
 ) (map[string]types.NetworkCreate, []string) {
185 186
 	if networks == nil {
186 187
 		networks = make(map[string]composetypes.NetworkConfig)
187 188
 	}
188 189
 
189
-	// TODO: only add default network if it's used
190
-	networks["default"] = composetypes.NetworkConfig{}
191
-
192 190
 	externalNetworks := []string{}
193 191
 	result := make(map[string]types.NetworkCreate)
194 192
 
195
-	for internalName, network := range networks {
193
+	for internalName := range servicesNetworks {
194
+		network := networks[internalName]
196 195
 		if network.External.External {
197 196
 			externalNetworks = append(externalNetworks, network.External.Name)
198 197
 			continue