Merge pull request #711 from dnephin/compose-add-name-to-network

Support network.name in the compose schema
This commit is contained in:
Sebastiaan van Stijn 2017-12-06 10:44:03 -08:00 committed by GitHub
commit 30755cf340
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 17 deletions

View File

@ -98,7 +98,7 @@ func loadSections(config map[string]interface{}, configDetails types.ConfigDetai
{ {
key: "networks", key: "networks",
fnc: func(config map[string]interface{}) error { fnc: func(config map[string]interface{}) error {
cfg.Networks, err = LoadNetworks(config) cfg.Networks, err = LoadNetworks(config, configDetails.Version)
return err return err
}, },
}, },
@ -425,17 +425,30 @@ func transformUlimits(data interface{}) (interface{}, error) {
// LoadNetworks produces a NetworkConfig map from a compose file Dict // LoadNetworks produces a NetworkConfig map from a compose file Dict
// the source Dict is not validated if directly used. Use Load() to enable validation // the source Dict is not validated if directly used. Use Load() to enable validation
func LoadNetworks(source map[string]interface{}) (map[string]types.NetworkConfig, error) { func LoadNetworks(source map[string]interface{}, version string) (map[string]types.NetworkConfig, error) {
networks := make(map[string]types.NetworkConfig) networks := make(map[string]types.NetworkConfig)
err := transform(source, &networks) err := transform(source, &networks)
if err != nil { if err != nil {
return networks, err return networks, err
} }
for name, network := range networks { for name, network := range networks {
if network.External.External && network.External.Name == "" { if !network.External.External {
network.External.Name = name continue
networks[name] = network
} }
switch {
case network.External.Name != "":
if network.Name != "" {
return nil, errors.Errorf("network %s: network.external.name and network.name conflict; only use network.name", name)
}
if versions.GreaterThanOrEqualTo(version, "3.5") {
logrus.Warnf("network %s: network.external.name is deprecated in favor of network.name", name)
}
network.Name = network.External.Name
network.External.Name = ""
case network.Name == "":
network.Name = name
}
networks[name] = network
} }
return networks, nil return networks, nil
} }

View File

@ -636,7 +636,8 @@ networks:
}, },
Networks: map[string]types.NetworkConfig{ Networks: map[string]types.NetworkConfig{
"front": { "front": {
External: types.External{External: true, Name: "front"}, External: types.External{External: true},
Name: "front",
Internal: true, Internal: true,
Attachable: true, Attachable: true,
}, },
@ -800,7 +801,7 @@ volumes:
assert.Contains(t, err.Error(), "external_volume") assert.Contains(t, err.Error(), "external_volume")
} }
func TestInvalidExternalNameAndNameCombination(t *testing.T) { func TestLoadVolumeInvalidExternalNameAndNameCombination(t *testing.T) {
_, err := loadYAML(` _, err := loadYAML(`
version: "3.4" version: "3.4"
volumes: volumes:
@ -1172,17 +1173,13 @@ func TestFullExample(t *testing.T) {
}, },
"external-network": { "external-network": {
External: types.External{
Name: "external-network", Name: "external-network",
External: true, External: types.External{External: true},
},
}, },
"other-external-network": { "other-external-network": {
External: types.External{
Name: "my-cool-network", Name: "my-cool-network",
External: true, External: types.External{External: true},
},
}, },
} }
@ -1516,7 +1513,7 @@ configs:
assert.Equal(t, "invalid", actual.Services[0].Isolation) assert.Equal(t, "invalid", actual.Services[0].Isolation)
} }
func TestInvalidSecretExternalNameAndNameCombination(t *testing.T) { func TestLoadSecretInvalidExternalNameAndNameCombination(t *testing.T) {
_, err := loadYAML(` _, err := loadYAML(`
version: "3.5" version: "3.5"
secrets: secrets:
@ -1556,3 +1553,65 @@ func TestLoadSecretsWarnOnDeprecatedExternalNameVersion35(t *testing.T) {
assert.Equal(t, expected, secrets) assert.Equal(t, expected, secrets)
assert.Contains(t, buf.String(), "secret.external.name is deprecated") assert.Contains(t, buf.String(), "secret.external.name is deprecated")
} }
func TestLoadNetworksWarnOnDeprecatedExternalNameVersion35(t *testing.T) {
buf, cleanup := patchLogrus()
defer cleanup()
source := map[string]interface{}{
"foo": map[string]interface{}{
"external": map[string]interface{}{
"name": "oops",
},
},
}
networks, err := LoadNetworks(source, "3.5")
require.NoError(t, err)
expected := map[string]types.NetworkConfig{
"foo": {
Name: "oops",
External: types.External{External: true},
},
}
assert.Equal(t, expected, networks)
assert.Contains(t, buf.String(), "network.external.name is deprecated")
}
func TestLoadNetworksWarnOnDeprecatedExternalNameVersion34(t *testing.T) {
buf, cleanup := patchLogrus()
defer cleanup()
source := map[string]interface{}{
"foo": map[string]interface{}{
"external": map[string]interface{}{
"name": "oops",
},
},
}
networks, err := LoadNetworks(source, "3.4")
require.NoError(t, err)
expected := map[string]types.NetworkConfig{
"foo": {
Name: "oops",
External: types.External{External: true},
},
}
assert.Equal(t, expected, networks)
assert.Equal(t, "", buf.String())
}
func TestLoadNetworkInvalidExternalNameAndNameCombination(t *testing.T) {
_, err := loadYAML(`
version: "3.5"
networks:
foo:
name: user_specified_name
external:
name: external_name
`)
require.Error(t, err)
assert.Contains(t, err.Error(), "network.external.name and network.name conflict; only use network.name")
assert.Contains(t, err.Error(), "foo")
}

File diff suppressed because one or more lines are too long

View File

@ -428,6 +428,7 @@
"id": "#/definitions/network", "id": "#/definitions/network",
"type": ["object", "null"], "type": ["object", "null"],
"properties": { "properties": {
"name": {"type": "string"},
"driver": {"type": "string"}, "driver": {"type": "string"},
"driver_opts": { "driver_opts": {
"type": "object", "type": "object",

View File

@ -319,6 +319,7 @@ type UlimitsConfig struct {
// NetworkConfig for a network // NetworkConfig for a network
type NetworkConfig struct { type NetworkConfig struct {
Name string
Driver string Driver string
DriverOpts map[string]string `mapstructure:"driver_opts"` DriverOpts map[string]string `mapstructure:"driver_opts"`
Ipam IPAMConfig Ipam IPAMConfig