Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/command/stack/swarm/deploy_bundlefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func deployBundle(ctx context.Context, dockerCli command.Cli, opts options.Deplo
networks := make(map[string]types.NetworkCreate)
for _, service := range bundle.Services {
for _, networkName := range service.Networks {
networks[networkName] = types.NetworkCreate{
networks[namespace.Scope(networkName)] = types.NetworkCreate{
Labels: convert.AddStackLabel(namespace, nil),
}
}
Expand Down
5 changes: 2 additions & 3 deletions cli/command/stack/swarm/deploy_composefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ func createNetworks(
existingNetworkMap[network.Name] = network
}

for internalName, createOpts := range networks {
name := namespace.Scope(internalName)
for name, createOpts := range networks {
if _, exists := existingNetworkMap[name]; exists {
continue
}
Expand All @@ -193,7 +192,7 @@ func createNetworks(

fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name)
if _, err := client.NetworkCreate(ctx, name, createOpts); err != nil {
return errors.Wrapf(err, "failed to create network %s", internalName)
return errors.Wrapf(err, "failed to create network %s", name)
}
}
return nil
Expand Down
7 changes: 6 additions & 1 deletion cli/compose/convert/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str
}
createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
}
result[internalName] = createOpts

networkName := namespace.Scope(internalName)
if network.Name != "" {
networkName = network.Name
}
result[networkName] = createOpts
}

return result, externalNetworks
Expand Down
17 changes: 12 additions & 5 deletions cli/compose/convert/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestNetworks(t *testing.T) {
"outside": {},
"default": {},
"attachablenet": {},
"named": {},
}
source := networkMap{
"normal": composetypes.NetworkConfig{
Expand Down Expand Up @@ -62,14 +63,17 @@ func TestNetworks(t *testing.T) {
Driver: "overlay",
Attachable: true,
},
"named": composetypes.NetworkConfig{
Name: "othername",
},
}
expected := map[string]types.NetworkCreate{
"default": {
"foo_default": {
Labels: map[string]string{
LabelNamespace: "foo",
},
},
"normal": {
"foo_normal": {
Driver: "overlay",
IPAM: &network.IPAM{
Driver: "driver",
Expand All @@ -87,18 +91,21 @@ func TestNetworks(t *testing.T) {
"something": "labeled",
},
},
"attachablenet": {
"foo_attachablenet": {
Driver: "overlay",
Attachable: true,
Labels: map[string]string{
LabelNamespace: "foo",
},
},
"othername": {
Labels: map[string]string{LabelNamespace: "foo"},
},
}

networks, externals := Networks(namespace, source, serviceNetworks)
assert.Check(t, is.DeepEqual(expected, networks))
assert.Check(t, is.DeepEqual([]string{"special"}, externals))
assert.DeepEqual(t, expected, networks)
assert.DeepEqual(t, []string{"special"}, externals)
}

func TestSecrets(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func convertServiceNetworks(
aliases = network.Aliases
}
target := namespace.Scope(networkName)
if networkConfig.External.External {
if networkConfig.Name != "" {
target = networkConfig.Name
}
netAttachConfig := swarm.NetworkAttachmentConfig{
Expand Down
38 changes: 38 additions & 0 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/docker/cli/cli/compose/types"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -1357,3 +1358,40 @@ networks:
assert.ErrorContains(t, err, "network.external.name and network.name conflict; only use network.name")
assert.ErrorContains(t, err, "foo")
}

func TestLoadNetworkWithName(t *testing.T) {
config, err := loadYAML(`
version: '3.5'
services:
hello-world:
image: redis:alpine
networks:
- network1
- network3

networks:
network1:
name: network2
network3:
`)
assert.NilError(t, err)
expected := &types.Config{
Filename: "filename.yml",
Version: "3.5",
Services: types.Services{
{
Name: "hello-world",
Image: "redis:alpine",
Networks: map[string]*types.ServiceNetworkConfig{
"network1": nil,
"network3": nil,
},
},
},
Networks: map[string]types.NetworkConfig{
"network1": {Name: "network2"},
"network3": {},
},
}
assert.DeepEqual(t, config, expected, cmpopts.EquateEmpty())
}
26 changes: 26 additions & 0 deletions e2e/stack/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package stack

import (
"sort"
"strings"
"testing"

"github.com/gotestyourself/gotestyourself/assert"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/gotestyourself/gotestyourself/icmd"
)

func TestDeployWithNamedResources(t *testing.T) {
stackname := "test-stack-deploy-with-names"
composefile := golden.Path("stack-with-named-resources.yml")

result := icmd.RunCommand(
"docker", "stack", "deploy", "-c", composefile, stackname)

result.Assert(t, icmd.Success)
stdout := strings.Split(result.Stdout(), "\n")
expected := strings.Split(string(golden.Get(t, "stack-deploy-with-nanes.golden")), "\n")
sort.Strings(stdout)
sort.Strings(expected)
assert.DeepEqual(t, stdout, expected)
}
1 change: 1 addition & 0 deletions e2e/stack/testdata/data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A file with some text
7 changes: 7 additions & 0 deletions e2e/stack/testdata/stack-deploy-with-nanes.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Creating network test-stack-deploy-with-names_network2
Creating network named-network
Creating secret named-secret
Creating secret test-stack-deploy-with-names_secret2
Creating config test-stack-deploy-with-names_config2
Creating config named-config
Creating service test-stack-deploy-with-names_web
30 changes: 30 additions & 0 deletions e2e/stack/testdata/stack-with-named-resources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.5'
services:
web:
image: registry:5000/alpine:3.6
command: top
networks: [network1, network2]
volumes: [volume1, volume2]
secrets: [secret1, secret2]
configs: [config1, config2]

networks:
network1:
name: named-network
network2:
volumes:
volume1:
name: named-volume
volume2:
secrets:
secret1:
name: named-secret
file: ./data
secret2:
file: ./data
configs:
config1:
name: named-config
file: ./data
config2:
file: ./data
89 changes: 89 additions & 0 deletions vendor/github.com/google/go-cmp/cmp/cmpopts/equate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading