diff --git a/cli/compose/convert/compose.go b/cli/compose/convert/compose.go index 7b369596f969..d04bdd6a4c60 100644 --- a/cli/compose/convert/compose.go +++ b/cli/compose/convert/compose.go @@ -51,6 +51,32 @@ func AddStackLabel(namespace Namespace, labels map[string]string) map[string]str type networkMap map[string]composetypes.NetworkConfig +func networkCreateOptions(namespace Namespace, network composetypes.NetworkConfig) types.NetworkCreate { + createOpts := types.NetworkCreate{ + Labels: AddStackLabel(namespace, network.Labels), + Driver: network.Driver, + Options: network.DriverOpts, + Internal: network.Internal, + Attachable: network.Attachable, + } + + if network.Ipam.Driver != "" || len(network.Ipam.Config) > 0 { + createOpts.IPAM = &networktypes.IPAM{} + } + + if network.Ipam.Driver != "" { + createOpts.IPAM.Driver = network.Ipam.Driver + } + for _, ipamConfig := range network.Ipam.Config { + config := networktypes.IPAMConfig{ + Subnet: ipamConfig.Subnet, + } + createOpts.IPAM.Config = append(createOpts.IPAM.Config, config) + } + + return createOpts +} + // Networks from the compose-file type to the engine API type func Networks(namespace Namespace, networks networkMap, servicesNetworks map[string]struct{}) (map[string]types.NetworkCreate, []string) { if networks == nil { @@ -59,6 +85,17 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str externalNetworks := []string{} result := make(map[string]types.NetworkCreate) + if len(servicesNetworks) == 0 { + for networkName := range networks { + network := networks[networkName] + if network.External.External { + externalNetworks = append(externalNetworks, network.Name) + } else { + result[networkName] = networkCreateOptions(namespace, network) + } + } + } + for internalName := range servicesNetworks { network := networks[internalName] if network.External.External { @@ -66,33 +103,11 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str continue } - createOpts := types.NetworkCreate{ - Labels: AddStackLabel(namespace, network.Labels), - Driver: network.Driver, - Options: network.DriverOpts, - Internal: network.Internal, - Attachable: network.Attachable, - } - - if network.Ipam.Driver != "" || len(network.Ipam.Config) > 0 { - createOpts.IPAM = &networktypes.IPAM{} - } - - if network.Ipam.Driver != "" { - createOpts.IPAM.Driver = network.Ipam.Driver - } - for _, ipamConfig := range network.Ipam.Config { - config := networktypes.IPAMConfig{ - Subnet: ipamConfig.Subnet, - } - createOpts.IPAM.Config = append(createOpts.IPAM.Config, config) - } - networkName := namespace.Scope(internalName) if network.Name != "" { networkName = network.Name } - result[networkName] = createOpts + result[networkName] = networkCreateOptions(namespace, network) } return result, externalNetworks diff --git a/cli/compose/convert/compose_test.go b/cli/compose/convert/compose_test.go index 1846df17eea8..538e9b2fe46c 100644 --- a/cli/compose/convert/compose_test.go +++ b/cli/compose/convert/compose_test.go @@ -108,6 +108,27 @@ func TestNetworks(t *testing.T) { assert.DeepEqual(t, []string{"special"}, externals) } +func TestNetworksCreatedWithoutServices(t *testing.T) { + namespace := Namespace{name: "foo"} + serviceNetworks := map[string]struct{}{} + source := networkMap{ + "named": composetypes.NetworkConfig{ + Name: "othername", + }, + } + expected := map[string]types.NetworkCreate{ + "named": { + Labels: map[string]string{ + LabelNamespace: "foo", + }, + }, + } + + networks, externals := Networks(namespace, source, serviceNetworks) + assert.DeepEqual(t, expected, networks) + assert.DeepEqual(t, []string{}, externals) +} + func TestSecrets(t *testing.T) { namespace := Namespace{name: "foo"}