From b7577dd2ad6fbb2cf21baf7ee082d2f605157b2a Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 22 Feb 2017 13:52:09 -0500 Subject: [PATCH] Support customizing the default network for a stack. Signed-off-by: Daniel Nephin --- cli/compose/convert/service.go | 13 ++++++------- cli/compose/convert/service_test.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 93b910967e..9af4a74309 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -16,6 +16,8 @@ import ( runconfigopts "github.com/docker/docker/runconfig/opts" ) +const defaultNetwork = "default" + // Services from compose-file types to engine API types // TODO: fix secrets API so that SecretAPIClient is not required here func Services( @@ -156,18 +158,15 @@ func convertServiceNetworks( name string, ) ([]swarm.NetworkAttachmentConfig, error) { if len(networks) == 0 { - return []swarm.NetworkAttachmentConfig{ - { - Target: namespace.Scope("default"), - Aliases: []string{name}, - }, - }, nil + networks = map[string]*composetypes.ServiceNetworkConfig{ + defaultNetwork: {}, + } } nets := []swarm.NetworkAttachmentConfig{} for networkName, network := range networks { networkConfig, ok := networkConfigs[networkName] - if !ok { + if !ok && networkName != defaultNetwork { return []swarm.NetworkAttachmentConfig{}, fmt.Errorf( "service %q references network %q, which is not declared", name, networkName) } diff --git a/cli/compose/convert/service_test.go b/cli/compose/convert/service_test.go index 64ccfd038e..10bde35080 100644 --- a/cli/compose/convert/service_test.go +++ b/cli/compose/convert/service_test.go @@ -179,10 +179,9 @@ func TestConvertEndpointSpec(t *testing.T) { func TestConvertServiceNetworksOnlyDefault(t *testing.T) { networkConfigs := networkMap{} - networks := map[string]*composetypes.ServiceNetworkConfig{} configs, err := convertServiceNetworks( - networks, networkConfigs, NewNamespace("foo"), "service") + nil, networkConfigs, NewNamespace("foo"), "service") expected := []swarm.NetworkAttachmentConfig{ { @@ -235,6 +234,31 @@ func TestConvertServiceNetworks(t *testing.T) { assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(sortedConfigs), expected) } +func TestConvertServiceNetworksCustomDefault(t *testing.T) { + networkConfigs := networkMap{ + "default": composetypes.NetworkConfig{ + External: composetypes.External{ + External: true, + Name: "custom", + }, + }, + } + networks := map[string]*composetypes.ServiceNetworkConfig{} + + configs, err := convertServiceNetworks( + networks, networkConfigs, NewNamespace("foo"), "service") + + expected := []swarm.NetworkAttachmentConfig{ + { + Target: "custom", + Aliases: []string{"service"}, + }, + } + + assert.NilError(t, err) + assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(configs), expected) +} + type byTargetSort []swarm.NetworkAttachmentConfig func (s byTargetSort) Len() int {