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) networks := make(map[string]types.NetworkCreate)
for _, service := range bundle.Services { for _, service := range bundle.Services {
for _, networkName := range service.Networks { for _, networkName := range service.Networks {
networks[networkName] = types.NetworkCreate{ networks[namespace.Scope(networkName)] = types.NetworkCreate{
Labels: convert.AddStackLabel(namespace, nil), Labels: convert.AddStackLabel(namespace, nil),
} }
} }

View File

@ -181,8 +181,7 @@ func createNetworks(
existingNetworkMap[network.Name] = network existingNetworkMap[network.Name] = network
} }
for internalName, createOpts := range networks { for name, createOpts := range networks {
name := namespace.Scope(internalName)
if _, exists := existingNetworkMap[name]; exists { if _, exists := existingNetworkMap[name]; exists {
continue continue
} }
@ -193,7 +192,7 @@ func createNetworks(
fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name) fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name)
if _, err := client.NetworkCreate(ctx, name, createOpts); err != nil { 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 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) 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 return result, externalNetworks

View File

@ -35,6 +35,7 @@ func TestNetworks(t *testing.T) {
"outside": {}, "outside": {},
"default": {}, "default": {},
"attachablenet": {}, "attachablenet": {},
"named": {},
} }
source := networkMap{ source := networkMap{
"normal": composetypes.NetworkConfig{ "normal": composetypes.NetworkConfig{
@ -62,14 +63,17 @@ func TestNetworks(t *testing.T) {
Driver: "overlay", Driver: "overlay",
Attachable: true, Attachable: true,
}, },
"named": composetypes.NetworkConfig{
Name: "othername",
},
} }
expected := map[string]types.NetworkCreate{ expected := map[string]types.NetworkCreate{
"default": { "foo_default": {
Labels: map[string]string{ Labels: map[string]string{
LabelNamespace: "foo", LabelNamespace: "foo",
}, },
}, },
"normal": { "foo_normal": {
Driver: "overlay", Driver: "overlay",
IPAM: &network.IPAM{ IPAM: &network.IPAM{
Driver: "driver", Driver: "driver",
@ -87,18 +91,21 @@ func TestNetworks(t *testing.T) {
"something": "labeled", "something": "labeled",
}, },
}, },
"attachablenet": { "foo_attachablenet": {
Driver: "overlay", Driver: "overlay",
Attachable: true, Attachable: true,
Labels: map[string]string{ Labels: map[string]string{
LabelNamespace: "foo", LabelNamespace: "foo",
}, },
}, },
"othername": {
Labels: map[string]string{LabelNamespace: "foo"},
},
} }
networks, externals := Networks(namespace, source, serviceNetworks) networks, externals := Networks(namespace, source, serviceNetworks)
assert.Check(t, is.DeepEqual(expected, networks)) assert.DeepEqual(t, expected, networks)
assert.Check(t, is.DeepEqual([]string{"special"}, externals)) assert.DeepEqual(t, []string{"special"}, externals)
} }
func TestSecrets(t *testing.T) { func TestSecrets(t *testing.T) {

View File

@ -229,7 +229,7 @@ func convertServiceNetworks(
aliases = network.Aliases aliases = network.Aliases
} }
target := namespace.Scope(networkName) target := namespace.Scope(networkName)
if networkConfig.External.External { if networkConfig.Name != "" {
target = networkConfig.Name target = networkConfig.Name
} }
netAttachConfig := swarm.NetworkAttachmentConfig{ netAttachConfig := swarm.NetworkAttachmentConfig{

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/docker/cli/cli/compose/types" "github.com/docker/cli/cli/compose/types"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"
"github.com/sirupsen/logrus" "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, "network.external.name and network.name conflict; only use network.name")
assert.ErrorContains(t, err, "foo") 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())
}