Skip to content

Commit

Permalink
Added prioritization of networks
Browse files Browse the repository at this point in the history
Signed-off-by: Zal Daroga <zaldaroga@yahoo.com>
  • Loading branch information
zaldaroga authored and shin- committed Jan 12, 2018
1 parent 9aa9758 commit f50e1a8
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 12 deletions.
3 changes: 2 additions & 1 deletion compose/config/config_schema_v2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@
},
"additionalProperties": false
},
"internal": {"type": "boolean"}
"internal": {"type": "boolean"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v2.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@
"internal": {"type": "boolean"},
"enable_ipv6": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"name": {"type": "string"}
"name": {"type": "string"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v2.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@
"internal": {"type": "boolean"},
"enable_ipv6": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"name": {"type": "string"}
"name": {"type": "string"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v2.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@
"internal": {"type": "boolean"},
"enable_ipv6": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"name": {"type": "string"}
"name": {"type": "string"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@
"additionalProperties": false
},
"internal": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/list_or_dict"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v3.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@
"additionalProperties": false
},
"internal": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/list_or_dict"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v3.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/list_or_dict"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v3.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/list_or_dict"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v3.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/list_or_dict"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v3.5.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/list_or_dict"},
"priority": {"type": "number"}
},
"additionalProperties": false
},
Expand Down
4 changes: 3 additions & 1 deletion compose/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class Network(object):
def __init__(self, client, project, name, driver=None, driver_opts=None,
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.project = project
self.name = name
Expand All @@ -38,6 +38,7 @@ def __init__(self, client, project, name, driver=None, driver_opts=None,
self.enable_ipv6 = enable_ipv6
self.labels = labels
self.custom_name = custom_name
self.priority = priority

def ensure(self):
if self.external:
Expand Down Expand Up @@ -214,6 +215,7 @@ def build_networks(name, config_data, client):
enable_ipv6=data.get('enable_ipv6'),
labels=data.get('labels'),
custom_name=data.get('name') is not None,
priority=data.get('priority'),
)
for network_name, data in network_config.items()
}
Expand Down
8 changes: 7 additions & 1 deletion compose/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import sys
from collections import namedtuple
from collections import OrderedDict
from operator import attrgetter

import enum
Expand Down Expand Up @@ -557,10 +558,15 @@ def start_container(self, container):
raise OperationFailedError("Cannot start service %s: %s" % (self.name, ex.explanation))
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):
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 short_id_alias_exists(container, network):
continue
Expand Down
12 changes: 12 additions & 0 deletions tests/acceptance/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,18 @@ def test_up_with_net_v1(self):
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()
def test_up_with_healthcheck(self):
def wait_on_health_status(container, status):
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/networks/ordered-networks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "2"

services:
web:
image: busybox
command: top
networks: ["foo", "bar"]

networks:
foo:
priority: 1
bar:
priority: 2

0 comments on commit f50e1a8

Please sign in to comment.