mirror of https://github.com/docker/compose.git
Added prioritization of networks
Signed-off-by: Zal Daroga <zaldaroga@yahoo.com>
This commit is contained in:
parent
9aa9758819
commit
f50e1a8c2d
|
@ -299,7 +299,8 @@
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"}
|
"internal": {"type": "boolean"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -351,7 +351,8 @@
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"enable_ipv6": {"type": "boolean"},
|
"enable_ipv6": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"},
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
"name": {"type": "string"}
|
"name": {"type": "string"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -358,7 +358,8 @@
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"enable_ipv6": {"type": "boolean"},
|
"enable_ipv6": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"},
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
"name": {"type": "string"}
|
"name": {"type": "string"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -395,7 +395,8 @@
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"enable_ipv6": {"type": "boolean"},
|
"enable_ipv6": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"},
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
"name": {"type": "string"}
|
"name": {"type": "string"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -310,7 +310,8 @@
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"}
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -339,7 +339,8 @@
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"}
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -387,7 +387,8 @@
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"attachable": {"type": "boolean"},
|
"attachable": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"}
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -430,7 +430,8 @@
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"attachable": {"type": "boolean"},
|
"attachable": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"}
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -438,7 +438,8 @@
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"attachable": {"type": "boolean"},
|
"attachable": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"}
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -464,7 +464,8 @@
|
||||||
},
|
},
|
||||||
"internal": {"type": "boolean"},
|
"internal": {"type": "boolean"},
|
||||||
"attachable": {"type": "boolean"},
|
"attachable": {"type": "boolean"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"}
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"priority": {"type": "number"}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -26,7 +26,7 @@ OPTS_EXCEPTIONS = [
|
||||||
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=False, internal=False, enable_ipv6=False,
|
ipam=None, external=False, internal=False, enable_ipv6=False,
|
||||||
labels=None, custom_name=False):
|
labels=None, custom_name=False, priority=0):
|
||||||
self.client = client
|
self.client = client
|
||||||
self.project = project
|
self.project = project
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -38,6 +38,7 @@ class Network(object):
|
||||||
self.enable_ipv6 = enable_ipv6
|
self.enable_ipv6 = enable_ipv6
|
||||||
self.labels = labels
|
self.labels = labels
|
||||||
self.custom_name = custom_name
|
self.custom_name = custom_name
|
||||||
|
self.priority = priority
|
||||||
|
|
||||||
def ensure(self):
|
def ensure(self):
|
||||||
if self.external:
|
if self.external:
|
||||||
|
@ -214,6 +215,7 @@ def build_networks(name, config_data, client):
|
||||||
enable_ipv6=data.get('enable_ipv6'),
|
enable_ipv6=data.get('enable_ipv6'),
|
||||||
labels=data.get('labels'),
|
labels=data.get('labels'),
|
||||||
custom_name=data.get('name') is not None,
|
custom_name=data.get('name') is not None,
|
||||||
|
priority=data.get('priority'),
|
||||||
)
|
)
|
||||||
for network_name, data in network_config.items()
|
for network_name, data in network_config.items()
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from collections import OrderedDict
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
||||||
import enum
|
import enum
|
||||||
|
@ -557,10 +558,15 @@ class Service(object):
|
||||||
raise OperationFailedError("Cannot start service %s: %s" % (self.name, ex.explanation))
|
raise OperationFailedError("Cannot start service %s: %s" % (self.name, ex.explanation))
|
||||||
return container
|
return container
|
||||||
|
|
||||||
|
def prioritized_networks(self):
|
||||||
|
prioritized_networks = OrderedDict(
|
||||||
|
sorted(self.networks.items(), key=lambda t: t[1].get('priority', 0), reverse=True))
|
||||||
|
return prioritized_networks
|
||||||
|
|
||||||
def connect_container_to_networks(self, container):
|
def connect_container_to_networks(self, container):
|
||||||
connected_networks = container.get('NetworkSettings.Networks')
|
connected_networks = container.get('NetworkSettings.Networks')
|
||||||
|
|
||||||
for network, netdefs in self.networks.items():
|
for network, netdefs in self.prioritized_networks().items():
|
||||||
if network in connected_networks:
|
if network in connected_networks:
|
||||||
if short_id_alias_exists(container, network):
|
if short_id_alias_exists(container, network):
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -1274,6 +1274,18 @@ class CLITestCase(DockerClientTestCase):
|
||||||
bar_container.id
|
bar_container.id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_up_ordered_networks(self):
|
||||||
|
self.base_dir = 'tests/fixtures/networks'
|
||||||
|
|
||||||
|
self.dispatch(['-f', 'ordered-networks.yml', 'up', '-d'])
|
||||||
|
|
||||||
|
containers = self.project.get_service('web').containers()
|
||||||
|
|
||||||
|
for container in containers:
|
||||||
|
networks = container.get('NetworkSettings.Networks')
|
||||||
|
assert networks.keys()[0] == "networks_bar"
|
||||||
|
assert networks.keys()[1] == "networks_foo"
|
||||||
|
|
||||||
@v3_only()
|
@v3_only()
|
||||||
def test_up_with_healthcheck(self):
|
def test_up_with_healthcheck(self):
|
||||||
def wait_on_health_status(container, status):
|
def wait_on_health_status(container, status):
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
version: "2"
|
||||||
|
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
image: busybox
|
||||||
|
command: top
|
||||||
|
networks: ["foo", "bar"]
|
||||||
|
|
||||||
|
networks:
|
||||||
|
foo:
|
||||||
|
priority: 1
|
||||||
|
bar:
|
||||||
|
priority: 2
|
Loading…
Reference in New Issue