From f8d33f4602e372b8a3d9a7ca2e0c5cf74e6c7661 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 29 Aug 2025 14:29:26 +0200 Subject: [PATCH 1/2] cli/compose/convert: split exported AddStackLabel from implementation This function is currently only used within the package; create a non-exported version of it, to make it clear it's not used elsewhere. This patch keeps the exported function for now, but we can decide if we need to keep it in future. Signed-off-by: Sebastiaan van Stijn --- cli/compose/convert/compose.go | 11 ++++++++--- cli/compose/convert/compose_test.go | 2 +- cli/compose/convert/service.go | 4 ++-- cli/compose/convert/volume.go | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cli/compose/convert/compose.go b/cli/compose/convert/compose.go index 1634e310e1..7558e810bb 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, @@ -171,7 +176,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 +197,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, From 580c3aa218bfff4f6bd7af656268bafc742c953c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 29 Aug 2025 14:31:50 +0200 Subject: [PATCH 2/2] cli/compose/convert: Networks: use struct-literal for IPAM config Use a struct-literal for the IPAM config, and combine some of the checks. Also use the Name field as a default, and only construct a scoped name if the given name is empty (instead of the reverse). Signed-off-by: Sebastiaan van Stijn --- cli/compose/convert/compose.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/cli/compose/convert/compose.go b/cli/compose/convert/compose.go index 7558e810bb..d2122f8b26 100644 --- a/cli/compose/convert/compose.go +++ b/cli/compose/convert/compose.go @@ -80,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 }