mirror of
https://github.com/docker/compose.git
synced 2025-04-08 17:05:13 +02:00
Network aliases are now part of the network dictionary
Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
parent
4b99b32ffb
commit
825a0941f0
@ -31,7 +31,6 @@ from .types import ServiceLink
|
||||
from .types import VolumeFromSpec
|
||||
from .types import VolumeSpec
|
||||
from .validation import match_named_volumes
|
||||
from .validation import match_network_aliases
|
||||
from .validation import validate_against_fields_schema
|
||||
from .validation import validate_against_service_schema
|
||||
from .validation import validate_depends_on
|
||||
@ -547,8 +546,6 @@ def validate_service(service_config, service_names, version):
|
||||
validate_network_mode(service_config, service_names)
|
||||
validate_depends_on(service_config, service_names)
|
||||
|
||||
match_network_aliases(service_config.config)
|
||||
|
||||
if not service_dict.get('image') and has_uppercase(service_name):
|
||||
raise ConfigurationError(
|
||||
"Service '{name}' contains uppercase characters which are not valid "
|
||||
|
@ -120,18 +120,28 @@
|
||||
"network_mode": {"type": "string"},
|
||||
|
||||
"networks": {
|
||||
"$ref": "#/definitions/list_of_strings"
|
||||
},
|
||||
"network_aliases": {
|
||||
"type": "object",
|
||||
"patternProperties": {
|
||||
"^[a-zA-Z0-9._-]+$": {
|
||||
"$ref": "#/definitions/list_of_strings"
|
||||
"oneOf": [
|
||||
{"$ref": "#/definitions/list_of_strings"},
|
||||
{
|
||||
"type": "object",
|
||||
"patternProperties": {
|
||||
"^[a-zA-Z0-9._-]+$": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"aliases": {"$ref": "#/definitions/list_of_strings"}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
{"type": "null"}
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
]
|
||||
},
|
||||
|
||||
"pid": {"type": ["string", "null"]},
|
||||
|
||||
"ports": {
|
||||
|
@ -91,19 +91,6 @@ def match_named_volumes(service_dict, project_volumes):
|
||||
)
|
||||
|
||||
|
||||
def match_network_aliases(service_dict):
|
||||
networks = service_dict.get('networks', [])
|
||||
aliased_networks = service_dict.get('network_aliases', {}).keys()
|
||||
for n in aliased_networks:
|
||||
if n not in networks:
|
||||
raise ConfigurationError(
|
||||
'Network "{0}" is referenced in network_aliases, but is not '
|
||||
'declared in the networks list for service "{1}"'.format(
|
||||
n, service_dict.get('name')
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def validate_top_level_service_objects(filename, service_dicts):
|
||||
"""Perform some high level validation of the service name and value.
|
||||
|
||||
|
@ -15,7 +15,7 @@ log = logging.getLogger(__name__)
|
||||
|
||||
class Network(object):
|
||||
def __init__(self, client, project, name, driver=None, driver_opts=None,
|
||||
ipam=None, external_name=None, aliases=None):
|
||||
ipam=None, external_name=None):
|
||||
self.client = client
|
||||
self.project = project
|
||||
self.name = name
|
||||
@ -23,7 +23,6 @@ class Network(object):
|
||||
self.driver_opts = driver_opts
|
||||
self.ipam = create_ipam_config_from_dict(ipam)
|
||||
self.external_name = external_name
|
||||
self.aliases = aliases or []
|
||||
|
||||
def ensure(self):
|
||||
if self.external_name:
|
||||
@ -160,25 +159,32 @@ class ProjectNetworks(object):
|
||||
network.ensure()
|
||||
|
||||
|
||||
def get_network_names_for_service(service_dict):
|
||||
def get_network_aliases_for_service(service_dict):
|
||||
if 'network_mode' in service_dict:
|
||||
return []
|
||||
return service_dict.get('networks', ['default'])
|
||||
return {}
|
||||
networks = service_dict.get('networks', ['default'])
|
||||
if isinstance(networks, list):
|
||||
return dict((net, []) for net in networks)
|
||||
|
||||
return dict(
|
||||
(net, (config or {}).get('aliases', []))
|
||||
for net, config in networks.items()
|
||||
)
|
||||
|
||||
|
||||
def get_network_names_for_service(service_dict):
|
||||
return get_network_aliases_for_service(service_dict).keys()
|
||||
|
||||
|
||||
def get_networks(service_dict, network_definitions):
|
||||
networks = {}
|
||||
aliases = service_dict.get('network_aliases', {})
|
||||
for name in get_network_names_for_service(service_dict):
|
||||
log.debug(name)
|
||||
for name, aliases in get_network_aliases_for_service(service_dict).items():
|
||||
network = network_definitions.get(name)
|
||||
if network:
|
||||
log.debug(aliases)
|
||||
networks[network.full_name] = aliases.get(name, [])
|
||||
networks[network.full_name] = aliases
|
||||
else:
|
||||
raise ConfigurationError(
|
||||
'Service "{}" uses an undefined network "{}"'
|
||||
.format(service_dict['name'], name))
|
||||
|
||||
log.debug(networks)
|
||||
return networks
|
||||
|
@ -451,24 +451,6 @@ id.
|
||||
net: "none"
|
||||
net: "container:[service name or container name/id]"
|
||||
|
||||
### network_aliases
|
||||
|
||||
> [Version 2 file format](#version-2) only.
|
||||
|
||||
Alias names for this service on each joined network. All networks referenced
|
||||
here must also appear under the `networks` key.
|
||||
|
||||
networks:
|
||||
- some-network
|
||||
- other-network
|
||||
network_aliases:
|
||||
some-network:
|
||||
- alias1
|
||||
- alias3
|
||||
other-network:
|
||||
- alias2
|
||||
- alias4
|
||||
|
||||
### network_mode
|
||||
|
||||
> [Version 2 file format](#version-2) only. In version 1, use [net](#net).
|
||||
@ -493,6 +475,19 @@ Networks to join, referencing entries under the
|
||||
- some-network
|
||||
- other-network
|
||||
|
||||
#### aliases
|
||||
|
||||
Alias names for this service on the specified network.
|
||||
|
||||
networks:
|
||||
some-network:
|
||||
aliases:
|
||||
- alias1
|
||||
- alias3
|
||||
other-network:
|
||||
aliases:
|
||||
- alias2
|
||||
|
||||
### pid
|
||||
|
||||
pid: "host"
|
||||
|
10
tests/fixtures/networks/network-aliases.yml
vendored
10
tests/fixtures/networks/network-aliases.yml
vendored
@ -5,13 +5,11 @@ services:
|
||||
image: busybox
|
||||
command: top
|
||||
networks:
|
||||
- front
|
||||
- back
|
||||
|
||||
network_aliases:
|
||||
front:
|
||||
- forward_facing
|
||||
- ahead
|
||||
aliases:
|
||||
- forward_facing
|
||||
- ahead
|
||||
back:
|
||||
|
||||
networks:
|
||||
front: {}
|
||||
|
@ -556,27 +556,6 @@ class ConfigTest(unittest.TestCase):
|
||||
assert services[1]['name'] == 'db'
|
||||
assert services[2]['name'] == 'web'
|
||||
|
||||
def test_invalid_network_alias(self):
|
||||
config_details = build_config_details({
|
||||
'version': '2',
|
||||
'services': {
|
||||
'web': {
|
||||
'image': 'busybox',
|
||||
'networks': ['hello'],
|
||||
'network_aliases': {
|
||||
'world': ['planet', 'universe']
|
||||
}
|
||||
}
|
||||
},
|
||||
'networks': {
|
||||
'hello': {},
|
||||
'world': {}
|
||||
}
|
||||
})
|
||||
with pytest.raises(ConfigurationError) as exc:
|
||||
config.load(config_details)
|
||||
assert 'not declared in the networks list' in exc.exconly()
|
||||
|
||||
def test_config_build_configuration(self):
|
||||
service = config.load(
|
||||
build_config_details(
|
||||
|
Loading…
x
Reference in New Issue
Block a user