From 70643ad005145a10af049a5ffb705b682ae1454e Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 10 Jan 2017 09:57:36 +0100 Subject: [PATCH] Few stack deploy network fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- command/stack/deploy.go | 18 +++++++++++++++++- compose/convert/compose.go | 11 +++-------- compose/convert/compose_test.go | 7 ++++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/command/stack/deploy.go b/command/stack/deploy.go index f4730db556..306a583e1e 100644 --- a/command/stack/deploy.go +++ b/command/stack/deploy.go @@ -117,7 +117,9 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo 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 { return err } @@ -131,6 +133,20 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo 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 { var msgs []string for name, description := range properties { diff --git a/compose/convert/compose.go b/compose/convert/compose.go index 7c410844c7..70c1762a48 100644 --- a/compose/convert/compose.go +++ b/compose/convert/compose.go @@ -43,20 +43,15 @@ func AddStackLabel(namespace Namespace, labels map[string]string) map[string]str type networkMap map[string]composetypes.NetworkConfig // 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 { 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{} result := make(map[string]types.NetworkCreate) - - for internalName, network := range networks { + for internalName := range servicesNetworks { + network := networks[internalName] if network.External.External { externalNetworks = append(externalNetworks, network.External.Name) continue diff --git a/compose/convert/compose_test.go b/compose/convert/compose_test.go index 27a67047d8..d88ac7f7c4 100644 --- a/compose/convert/compose_test.go +++ b/compose/convert/compose_test.go @@ -28,6 +28,11 @@ func TestAddStackLabel(t *testing.T) { func TestNetworks(t *testing.T) { namespace := Namespace{name: "foo"} + serviceNetworks := map[string]struct{}{ + "normal": {}, + "outside": {}, + "default": {}, + } source := networkMap{ "normal": composetypes.NetworkConfig{ 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, externals, []string{"special"}) }