mirror of https://github.com/docker/docs.git
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 VolumeFromSpec
|
||||||
from .types import VolumeSpec
|
from .types import VolumeSpec
|
||||||
from .validation import match_named_volumes
|
from .validation import match_named_volumes
|
||||||
from .validation import match_network_aliases
|
|
||||||
from .validation import validate_against_fields_schema
|
from .validation import validate_against_fields_schema
|
||||||
from .validation import validate_against_service_schema
|
from .validation import validate_against_service_schema
|
||||||
from .validation import validate_depends_on
|
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_network_mode(service_config, service_names)
|
||||||
validate_depends_on(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):
|
if not service_dict.get('image') and has_uppercase(service_name):
|
||||||
raise ConfigurationError(
|
raise ConfigurationError(
|
||||||
"Service '{name}' contains uppercase characters which are not valid "
|
"Service '{name}' contains uppercase characters which are not valid "
|
||||||
|
|
|
@ -120,18 +120,28 @@
|
||||||
"network_mode": {"type": "string"},
|
"network_mode": {"type": "string"},
|
||||||
|
|
||||||
"networks": {
|
"networks": {
|
||||||
"$ref": "#/definitions/list_of_strings"
|
"oneOf": [
|
||||||
},
|
{"$ref": "#/definitions/list_of_strings"},
|
||||||
"network_aliases": {
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"patternProperties": {
|
"patternProperties": {
|
||||||
"^[a-zA-Z0-9._-]+$": {
|
"^[a-zA-Z0-9._-]+$": {
|
||||||
"$ref": "#/definitions/list_of_strings"
|
"oneOf": [
|
||||||
}
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"aliases": {"$ref": "#/definitions/list_of_strings"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
{"type": "null"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"pid": {"type": ["string", "null"]},
|
"pid": {"type": ["string", "null"]},
|
||||||
|
|
||||||
"ports": {
|
"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):
|
def validate_top_level_service_objects(filename, service_dicts):
|
||||||
"""Perform some high level validation of the service name and value.
|
"""Perform some high level validation of the service name and value.
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Network(object):
|
class Network(object):
|
||||||
def __init__(self, client, project, name, driver=None, driver_opts=None,
|
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.client = client
|
||||||
self.project = project
|
self.project = project
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -23,7 +23,6 @@ class Network(object):
|
||||||
self.driver_opts = driver_opts
|
self.driver_opts = driver_opts
|
||||||
self.ipam = create_ipam_config_from_dict(ipam)
|
self.ipam = create_ipam_config_from_dict(ipam)
|
||||||
self.external_name = external_name
|
self.external_name = external_name
|
||||||
self.aliases = aliases or []
|
|
||||||
|
|
||||||
def ensure(self):
|
def ensure(self):
|
||||||
if self.external_name:
|
if self.external_name:
|
||||||
|
@ -160,25 +159,32 @@ class ProjectNetworks(object):
|
||||||
network.ensure()
|
network.ensure()
|
||||||
|
|
||||||
|
|
||||||
def get_network_names_for_service(service_dict):
|
def get_network_aliases_for_service(service_dict):
|
||||||
if 'network_mode' in service_dict:
|
if 'network_mode' in service_dict:
|
||||||
return []
|
return {}
|
||||||
return service_dict.get('networks', ['default'])
|
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):
|
def get_networks(service_dict, network_definitions):
|
||||||
networks = {}
|
networks = {}
|
||||||
aliases = service_dict.get('network_aliases', {})
|
for name, aliases in get_network_aliases_for_service(service_dict).items():
|
||||||
for name in get_network_names_for_service(service_dict):
|
|
||||||
log.debug(name)
|
|
||||||
network = network_definitions.get(name)
|
network = network_definitions.get(name)
|
||||||
if network:
|
if network:
|
||||||
log.debug(aliases)
|
networks[network.full_name] = aliases
|
||||||
networks[network.full_name] = aliases.get(name, [])
|
|
||||||
else:
|
else:
|
||||||
raise ConfigurationError(
|
raise ConfigurationError(
|
||||||
'Service "{}" uses an undefined network "{}"'
|
'Service "{}" uses an undefined network "{}"'
|
||||||
.format(service_dict['name'], name))
|
.format(service_dict['name'], name))
|
||||||
|
|
||||||
log.debug(networks)
|
|
||||||
return networks
|
return networks
|
||||||
|
|
|
@ -451,24 +451,6 @@ id.
|
||||||
net: "none"
|
net: "none"
|
||||||
net: "container:[service name or container name/id]"
|
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
|
### network_mode
|
||||||
|
|
||||||
> [Version 2 file format](#version-2) only. In version 1, use [net](#net).
|
> [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
|
- some-network
|
||||||
- other-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
|
||||||
|
|
||||||
pid: "host"
|
pid: "host"
|
||||||
|
|
|
@ -5,13 +5,11 @@ services:
|
||||||
image: busybox
|
image: busybox
|
||||||
command: top
|
command: top
|
||||||
networks:
|
networks:
|
||||||
- front
|
|
||||||
- back
|
|
||||||
|
|
||||||
network_aliases:
|
|
||||||
front:
|
front:
|
||||||
|
aliases:
|
||||||
- forward_facing
|
- forward_facing
|
||||||
- ahead
|
- ahead
|
||||||
|
back:
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
front: {}
|
front: {}
|
||||||
|
|
|
@ -556,27 +556,6 @@ class ConfigTest(unittest.TestCase):
|
||||||
assert services[1]['name'] == 'db'
|
assert services[1]['name'] == 'db'
|
||||||
assert services[2]['name'] == 'web'
|
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):
|
def test_config_build_configuration(self):
|
||||||
service = config.load(
|
service = config.load(
|
||||||
build_config_details(
|
build_config_details(
|
||||||
|
|
Loading…
Reference in New Issue