Fix named network in compose file

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2018-03-12 13:09:59 -04:00
parent ca88e5e9df
commit b4c108a385
6 changed files with 60 additions and 11 deletions

View File

@ -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),
}
}

View File

@ -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
}
@ -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

View File

@ -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

View File

@ -35,6 +35,7 @@ func TestNetworks(t *testing.T) {
"outside": {},
"default": {},
"attachablenet": {},
"named": {},
}
source := networkMap{
"normal": composetypes.NetworkConfig{
@ -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",
@ -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) {

View File

@ -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{

View File

@ -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"
@ -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())
}