diff --git a/cli/compose/convert/compose.go b/cli/compose/convert/compose.go index 1634e310e1..d2122f8b26 100644 --- a/cli/compose/convert/compose.go +++ b/cli/compose/convert/compose.go @@ -42,6 +42,11 @@ func NewNamespace(name string) Namespace { // AddStackLabel returns labels with the namespace label added func AddStackLabel(namespace Namespace, labels map[string]string) map[string]string { + return addStackLabel(namespace, labels) +} + +// addStackLabel returns labels with the namespace label added +func addStackLabel(namespace Namespace, labels map[string]string) map[string]string { if labels == nil { labels = make(map[string]string) } @@ -67,7 +72,7 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str } createOpts := client.NetworkCreateOptions{ - Labels: AddStackLabel(namespace, nw.Labels), + Labels: addStackLabel(namespace, nw.Labels), Driver: nw.Driver, Options: nw.DriverOpts, Internal: nw.Internal, @@ -75,22 +80,19 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str } if nw.Ipam.Driver != "" || len(nw.Ipam.Config) > 0 { - createOpts.IPAM = &network.IPAM{} - } - - if nw.Ipam.Driver != "" { - createOpts.IPAM.Driver = nw.Ipam.Driver - } - for _, ipamConfig := range nw.Ipam.Config { - config := network.IPAMConfig{ - Subnet: ipamConfig.Subnet, + createOpts.IPAM = &network.IPAM{ + Driver: nw.Ipam.Driver, + } + for _, ipamConfig := range nw.Ipam.Config { + createOpts.IPAM.Config = append(createOpts.IPAM.Config, network.IPAMConfig{ + Subnet: ipamConfig.Subnet, + }) } - createOpts.IPAM.Config = append(createOpts.IPAM.Config, config) } - networkName := namespace.Scope(internalName) - if nw.Name != "" { - networkName = nw.Name + networkName := nw.Name + if nw.Name == "" { + networkName = namespace.Scope(internalName) } result[networkName] = createOpts } @@ -171,7 +173,7 @@ func driverObjectConfig(namespace Namespace, name string, obj composetypes.FileO return swarmFileObject{ Annotations: swarm.Annotations{ Name: name, - Labels: AddStackLabel(namespace, obj.Labels), + Labels: addStackLabel(namespace, obj.Labels), }, Data: []byte{}, } @@ -192,7 +194,7 @@ func fileObjectConfig(namespace Namespace, name string, obj composetypes.FileObj return swarmFileObject{ Annotations: swarm.Annotations{ Name: name, - Labels: AddStackLabel(namespace, obj.Labels), + Labels: addStackLabel(namespace, obj.Labels), }, Data: data, }, nil diff --git a/cli/compose/convert/compose_test.go b/cli/compose/convert/compose_test.go index 36363d8471..f3d697b672 100644 --- a/cli/compose/convert/compose_test.go +++ b/cli/compose/convert/compose_test.go @@ -30,7 +30,7 @@ func TestAddStackLabel(t *testing.T) { labels := map[string]string{ "something": "labeled", } - actual := AddStackLabel(Namespace{name: "foo"}, labels) + actual := addStackLabel(Namespace{name: "foo"}, labels) expected := map[string]string{ "something": "labeled", LabelNamespace: "foo", diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 2b86991da8..292c52d728 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -119,7 +119,7 @@ func Service( serviceSpec := swarm.ServiceSpec{ Annotations: swarm.Annotations{ Name: name, - Labels: AddStackLabel(namespace, service.Deploy.Labels), + Labels: addStackLabel(namespace, service.Deploy.Labels), }, TaskTemplate: swarm.TaskSpec{ ContainerSpec: &swarm.ContainerSpec{ @@ -131,7 +131,7 @@ func Service( DNSConfig: dnsConfig, Healthcheck: healthcheck, Env: convertEnvironment(service.Environment), - Labels: AddStackLabel(namespace, service.Labels), + Labels: addStackLabel(namespace, service.Labels), Dir: service.WorkingDir, User: service.User, Mounts: mounts, diff --git a/cli/compose/convert/volume.go b/cli/compose/convert/volume.go index 6155dffe8c..8eaa934cc0 100644 --- a/cli/compose/convert/volume.go +++ b/cli/compose/convert/volume.go @@ -80,7 +80,7 @@ func handleVolumeToMount( return result, nil } - result.VolumeOptions.Labels = AddStackLabel(namespace, stackVolume.Labels) + result.VolumeOptions.Labels = addStackLabel(namespace, stackVolume.Labels) if stackVolume.Driver != "" || stackVolume.DriverOpts != nil { result.VolumeOptions.DriverConfig = &mount.Driver{ Name: stackVolume.Driver,