mirror of https://github.com/docker/cli.git
Merge pull request #31258 from dnephin/fix-override-default-stack-network
Support customizing the default network for a stack
This commit is contained in:
commit
f6f52d0823
|
@ -16,6 +16,8 @@ import (
|
||||||
runconfigopts "github.com/docker/docker/runconfig/opts"
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultNetwork = "default"
|
||||||
|
|
||||||
// Services from compose-file types to engine API types
|
// Services from compose-file types to engine API types
|
||||||
// TODO: fix secrets API so that SecretAPIClient is not required here
|
// TODO: fix secrets API so that SecretAPIClient is not required here
|
||||||
func Services(
|
func Services(
|
||||||
|
@ -156,18 +158,15 @@ func convertServiceNetworks(
|
||||||
name string,
|
name string,
|
||||||
) ([]swarm.NetworkAttachmentConfig, error) {
|
) ([]swarm.NetworkAttachmentConfig, error) {
|
||||||
if len(networks) == 0 {
|
if len(networks) == 0 {
|
||||||
return []swarm.NetworkAttachmentConfig{
|
networks = map[string]*composetypes.ServiceNetworkConfig{
|
||||||
{
|
defaultNetwork: {},
|
||||||
Target: namespace.Scope("default"),
|
}
|
||||||
Aliases: []string{name},
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nets := []swarm.NetworkAttachmentConfig{}
|
nets := []swarm.NetworkAttachmentConfig{}
|
||||||
for networkName, network := range networks {
|
for networkName, network := range networks {
|
||||||
networkConfig, ok := networkConfigs[networkName]
|
networkConfig, ok := networkConfigs[networkName]
|
||||||
if !ok {
|
if !ok && networkName != defaultNetwork {
|
||||||
return []swarm.NetworkAttachmentConfig{}, fmt.Errorf(
|
return []swarm.NetworkAttachmentConfig{}, fmt.Errorf(
|
||||||
"service %q references network %q, which is not declared", name, networkName)
|
"service %q references network %q, which is not declared", name, networkName)
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,10 +179,9 @@ func TestConvertEndpointSpec(t *testing.T) {
|
||||||
|
|
||||||
func TestConvertServiceNetworksOnlyDefault(t *testing.T) {
|
func TestConvertServiceNetworksOnlyDefault(t *testing.T) {
|
||||||
networkConfigs := networkMap{}
|
networkConfigs := networkMap{}
|
||||||
networks := map[string]*composetypes.ServiceNetworkConfig{}
|
|
||||||
|
|
||||||
configs, err := convertServiceNetworks(
|
configs, err := convertServiceNetworks(
|
||||||
networks, networkConfigs, NewNamespace("foo"), "service")
|
nil, networkConfigs, NewNamespace("foo"), "service")
|
||||||
|
|
||||||
expected := []swarm.NetworkAttachmentConfig{
|
expected := []swarm.NetworkAttachmentConfig{
|
||||||
{
|
{
|
||||||
|
@ -235,6 +234,31 @@ func TestConvertServiceNetworks(t *testing.T) {
|
||||||
assert.DeepEqual(t, []swarm.NetworkAttachmentConfig(sortedConfigs), expected)
|
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
|
type byTargetSort []swarm.NetworkAttachmentConfig
|
||||||
|
|
||||||
func (s byTargetSort) Len() int {
|
func (s byTargetSort) Len() int {
|
||||||
|
|
Loading…
Reference in New Issue