1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Few stack deploy network fixes

- Make sure we use the correct network name for external ones.
- Make the default network overridable and only creates networks that
  are used by services — so that default network is only created if a
  service doesn't declare a network.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-01-10 09:57:36 +01:00
parent 9c96768eae
commit 3191f5809b
No known key found for this signature in database
GPG key ID: 083CC6FD6EB699A3
3 changed files with 26 additions and 10 deletions

View file

@ -117,7 +117,9 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo
namespace := convert.NewNamespace(opts.namespace) namespace := convert.NewNamespace(opts.namespace)
networks, externalNetworks := convert.Networks(namespace, config.Networks) serviceNetworks := getServicesDeclaredNetworks(config.Services)
networks, externalNetworks := convert.Networks(namespace, config.Networks, serviceNetworks)
if err := validateExternalNetworks(ctx, dockerCli, externalNetworks); err != nil { if err := validateExternalNetworks(ctx, dockerCli, externalNetworks); err != nil {
return err return err
} }
@ -131,6 +133,20 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo
return deployServices(ctx, dockerCli, services, namespace, opts.sendRegistryAuth) return deployServices(ctx, dockerCli, services, namespace, opts.sendRegistryAuth)
} }
func getServicesDeclaredNetworks(serviceConfigs []composetypes.ServiceConfig) map[string]struct{} {
serviceNetworks := map[string]struct{}{}
for _, serviceConfig := range serviceConfigs {
if len(serviceConfig.Networks) == 0 {
serviceNetworks["default"] = struct{}{}
continue
}
for network := range serviceConfig.Networks {
serviceNetworks[network] = struct{}{}
}
}
return serviceNetworks
}
func propertyWarnings(properties map[string]string) string { func propertyWarnings(properties map[string]string) string {
var msgs []string var msgs []string
for name, description := range properties { for name, description := range properties {

View file

@ -43,20 +43,15 @@ func AddStackLabel(namespace Namespace, labels map[string]string) map[string]str
type networkMap map[string]composetypes.NetworkConfig type networkMap map[string]composetypes.NetworkConfig
// Networks from the compose-file type to the engine API type // Networks from the compose-file type to the engine API type
func Networks(namespace Namespace, networks networkMap) (map[string]types.NetworkCreate, []string) { func Networks(namespace Namespace, networks networkMap, servicesNetworks map[string]struct{}) (map[string]types.NetworkCreate, []string) {
if networks == nil { if networks == nil {
networks = make(map[string]composetypes.NetworkConfig) networks = make(map[string]composetypes.NetworkConfig)
} }
// TODO: only add default network if it's used
if _, ok := networks["default"]; !ok {
networks["default"] = composetypes.NetworkConfig{}
}
externalNetworks := []string{} externalNetworks := []string{}
result := make(map[string]types.NetworkCreate) result := make(map[string]types.NetworkCreate)
for internalName := range servicesNetworks {
for internalName, network := range networks { network := networks[internalName]
if network.External.External { if network.External.External {
externalNetworks = append(externalNetworks, network.External.Name) externalNetworks = append(externalNetworks, network.External.Name)
continue continue

View file

@ -28,6 +28,11 @@ func TestAddStackLabel(t *testing.T) {
func TestNetworks(t *testing.T) { func TestNetworks(t *testing.T) {
namespace := Namespace{name: "foo"} namespace := Namespace{name: "foo"}
serviceNetworks := map[string]struct{}{
"normal": {},
"outside": {},
"default": {},
}
source := networkMap{ source := networkMap{
"normal": composetypes.NetworkConfig{ "normal": composetypes.NetworkConfig{
Driver: "overlay", Driver: "overlay",
@ -79,7 +84,7 @@ func TestNetworks(t *testing.T) {
}, },
} }
networks, externals := Networks(namespace, source) networks, externals := Networks(namespace, source, serviceNetworks)
assert.DeepEqual(t, networks, expected) assert.DeepEqual(t, networks, expected)
assert.DeepEqual(t, externals, []string{"special"}) assert.DeepEqual(t, externals, []string{"special"})
} }