Allow custom ipam config

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2016-01-18 17:08:50 +00:00
parent da490f5b0a
commit afae365050
3 changed files with 84 additions and 2 deletions

View File

@ -4,6 +4,8 @@ from __future__ import unicode_literals
import logging
from docker.errors import NotFound
from docker.utils import create_ipam_config
from docker.utils import create_ipam_pool
from .config import ConfigurationError
@ -13,12 +15,13 @@ log = logging.getLogger(__name__)
class Network(object):
def __init__(self, client, project, name, driver=None, driver_opts=None,
external_name=None):
ipam=None, external_name=None):
self.client = client
self.project = project
self.name = name
self.driver = driver
self.driver_opts = driver_opts
self.ipam = create_ipam_config_from_dict(ipam)
self.external_name = external_name
def ensure(self):
@ -61,7 +64,10 @@ class Network(object):
)
self.client.create_network(
self.full_name, self.driver, self.driver_opts
name=self.full_name,
driver=self.driver,
options=self.driver_opts,
ipam=self.ipam,
)
def remove(self):
@ -80,3 +86,21 @@ class Network(object):
if self.external_name:
return self.external_name
return '{0}_{1}'.format(self.project, self.name)
def create_ipam_config_from_dict(ipam_dict):
if not ipam_dict:
return None
return create_ipam_config(
driver=ipam_dict.get('driver'),
pool_configs=[
create_ipam_pool(
subnet=config.get('subnet'),
iprange=config.get('ip_range'),
gateway=config.get('gateway'),
aux_addresses=config.get('aux_addresses'),
)
for config in ipam_dict.get('config', [])
],
)

View File

@ -64,6 +64,7 @@ class Project(object):
client=client, project=name, name=network_name,
driver=data.get('driver'),
driver_opts=data.get('driver_opts'),
ipam=data.get('ipam'),
external_name=data.get('external_name'),
)
for network_name, data in network_config.items()

View File

@ -516,6 +516,63 @@ class ProjectTest(DockerClientTestCase):
foo_data = self.client.inspect_network('composetest_foo')
self.assertEqual(foo_data['Driver'], 'bridge')
@v2_only()
def test_up_with_ipam_config(self):
config_data = config.Config(
version=2,
services=[],
volumes={},
networks={
'front': {
'driver': 'bridge',
'driver_opts': {
"com.docker.network.bridge.enable_icc": "false",
},
'ipam': {
'driver': 'default',
'config': [{
"subnet": "172.28.0.0/16",
"ip_range": "172.28.5.0/24",
"gateway": "172.28.5.254",
"aux_addresses": {
"a": "172.28.1.5",
"b": "172.28.1.6",
"c": "172.28.1.7",
},
}],
},
},
},
)
project = Project.from_config(
client=self.client,
name='composetest',
config_data=config_data,
)
project.up()
network = self.client.networks(names=['composetest_front'])[0]
assert network['Options'] == {
"com.docker.network.bridge.enable_icc": "false"
}
assert network['IPAM'] == {
'Driver': 'default',
'Options': None,
'Config': [{
'Subnet': "172.28.0.0/16",
'IPRange': "172.28.5.0/24",
'Gateway': "172.28.5.254",
'AuxiliaryAddresses': {
'a': '172.28.1.5',
'b': '172.28.1.6',
'c': '172.28.1.7',
},
}],
}
@v2_only()
def test_project_up_volumes(self):
vol_name = '{0:x}'.format(random.getrandbits(32))