Support customizing the default network for a stack.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
(cherry picked from commit b7577dd2ad)
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Daniel Nephin 2017-02-22 13:52:09 -05:00 committed by Victor Vieux
parent a3c0c6d03b
commit bc8f9c8249
2 changed files with 32 additions and 9 deletions

View file

@ -17,6 +17,8 @@ import (
"github.com/docker/go-connections/nat"
)
const defaultNetwork = "default"
// Services from compose-file types to engine API types
// TODO: fix secrets API so that SecretAPIClient is not required here
func Services(
@ -157,18 +159,15 @@ func convertServiceNetworks(
name string,
) ([]swarm.NetworkAttachmentConfig, error) {
if len(networks) == 0 {
return []swarm.NetworkAttachmentConfig{
{
Target: namespace.Scope("default"),
Aliases: []string{name},
},
}, nil
networks = map[string]*composetypes.ServiceNetworkConfig{
defaultNetwork: {},
}
}
nets := []swarm.NetworkAttachmentConfig{}
for networkName, network := range networks {
networkConfig, ok := networkConfigs[networkName]
if !ok {
if !ok && networkName != defaultNetwork {
return []swarm.NetworkAttachmentConfig{}, fmt.Errorf(
"service %q references network %q, which is not declared", name, networkName)
}

View file

@ -145,10 +145,9 @@ func TestConvertHealthcheckDisableWithTest(t *testing.T) {
func TestConvertServiceNetworksOnlyDefault(t *testing.T) {
networkConfigs := networkMap{}
networks := map[string]*composetypes.ServiceNetworkConfig{}
configs, err := convertServiceNetworks(
networks, networkConfigs, NewNamespace("foo"), "service")
nil, networkConfigs, NewNamespace("foo"), "service")
expected := []swarm.NetworkAttachmentConfig{
{
@ -201,6 +200,31 @@ func TestConvertServiceNetworks(t *testing.T) {
assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(sortedConfigs), expected)
}
func TestConvertServiceNetworksCustomDefault(t *testing.T) {
networkConfigs := networkMap{
"default": composetypes.NetworkConfig{
External: composetypes.External{
External: true,
Name: "custom",
},
},
}
networks := map[string]*composetypes.ServiceNetworkConfig{}
configs, err := convertServiceNetworks(
networks, networkConfigs, NewNamespace("foo"), "service")
expected := []swarm.NetworkAttachmentConfig{
{
Target: "custom",
Aliases: []string{"service"},
},
}
assert.NilError(t, err)
assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(configs), expected)
}
type byTargetSort []swarm.NetworkAttachmentConfig
func (s byTargetSort) Len() int {