Skip to content

Commit 74a0734

Browse files
author
Ulysses Souza
authored
Merge pull request #2551 from haboustak/2550-add-driveropts-to-endpointconfig
Add support for DriverOpts in EndpointConfig
2 parents d3adac9 + df7bf5f commit 74a0734

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

docker/api/container.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ def create_endpoint_config(self, *args, **kwargs):
636636
network, using the IPv6 protocol. Defaults to ``None``.
637637
link_local_ips (:py:class:`list`): A list of link-local (IPv4/IPv6)
638638
addresses.
639+
driver_opt (dict): A dictionary of options to provide to the
640+
network driver. Defaults to ``None``.
639641
640642
Returns:
641643
(dict) An endpoint config.

docker/api/network.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def inspect_network(self, net_id, verbose=None, scope=None):
216216
def connect_container_to_network(self, container, net_id,
217217
ipv4_address=None, ipv6_address=None,
218218
aliases=None, links=None,
219-
link_local_ips=None):
219+
link_local_ips=None, driver_opt=None):
220220
"""
221221
Connect a container to a network.
222222
@@ -240,7 +240,8 @@ def connect_container_to_network(self, container, net_id,
240240
"Container": container,
241241
"EndpointConfig": self.create_endpoint_config(
242242
aliases=aliases, links=links, ipv4_address=ipv4_address,
243-
ipv6_address=ipv6_address, link_local_ips=link_local_ips
243+
ipv6_address=ipv6_address, link_local_ips=link_local_ips,
244+
driver_opt=driver_opt
244245
),
245246
}
246247

docker/models/networks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def connect(self, container, *args, **kwargs):
4646
network, using the IPv6 protocol. Defaults to ``None``.
4747
link_local_ips (:py:class:`list`): A list of link-local (IPv4/IPv6)
4848
addresses.
49+
driver_opt (dict): A dictionary of options to provide to the
50+
network driver. Defaults to ``None``.
4951
5052
Raises:
5153
:py:class:`docker.errors.APIError`

docker/types/networks.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class EndpointConfig(dict):
66
def __init__(self, version, aliases=None, links=None, ipv4_address=None,
7-
ipv6_address=None, link_local_ips=None):
7+
ipv6_address=None, link_local_ips=None, driver_opt=None):
88
if version_lt(version, '1.22'):
99
raise errors.InvalidVersion(
1010
'Endpoint config is not supported for API version < 1.22'
@@ -33,6 +33,15 @@ def __init__(self, version, aliases=None, links=None, ipv4_address=None,
3333
if ipam_config:
3434
self['IPAMConfig'] = ipam_config
3535

36+
if driver_opt:
37+
if version_lt(version, '1.32'):
38+
raise errors.InvalidVersion(
39+
'DriverOpts is not supported for API version < 1.32'
40+
)
41+
if not isinstance(driver_opt, dict):
42+
raise TypeError('driver_opt must be a dictionary')
43+
self['DriverOpts'] = driver_opt
44+
3645

3746
class NetworkingConfig(dict):
3847
def __init__(self, endpoints_config=None):

tests/integration/api_network_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,27 @@ def test_create_with_linklocal_ips(self):
275275
assert 'LinkLocalIPs' in net_cfg['IPAMConfig']
276276
assert net_cfg['IPAMConfig']['LinkLocalIPs'] == ['169.254.8.8']
277277

278+
@requires_api_version('1.32')
279+
def test_create_with_driveropt(self):
280+
container = self.client.create_container(
281+
TEST_IMG, 'top',
282+
networking_config=self.client.create_networking_config(
283+
{
284+
'bridge': self.client.create_endpoint_config(
285+
driver_opt={'com.docker-py.setting': 'on'}
286+
)
287+
}
288+
),
289+
host_config=self.client.create_host_config(network_mode='bridge')
290+
)
291+
self.tmp_containers.append(container)
292+
self.client.start(container)
293+
container_data = self.client.inspect_container(container)
294+
net_cfg = container_data['NetworkSettings']['Networks']['bridge']
295+
assert 'DriverOpts' in net_cfg
296+
assert 'com.docker-py.setting' in net_cfg['DriverOpts']
297+
assert net_cfg['DriverOpts']['com.docker-py.setting'] == 'on'
298+
278299
@requires_api_version('1.22')
279300
def test_create_with_links(self):
280301
net_name, net_id = self.create_network()

tests/unit/api_network_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def test_connect_container_to_network(self):
136136
container={'Id': container_id},
137137
net_id=network_id,
138138
aliases=['foo', 'bar'],
139-
links=[('baz', 'quux')]
139+
links=[('baz', 'quux')],
140+
driver_opt={'com.docker-py.setting': 'yes'},
140141
)
141142

142143
assert post.call_args[0][0] == (
@@ -148,6 +149,7 @@ def test_connect_container_to_network(self):
148149
'EndpointConfig': {
149150
'Aliases': ['foo', 'bar'],
150151
'Links': ['baz:quux'],
152+
'DriverOpts': {'com.docker-py.setting': 'yes'},
151153
},
152154
}
153155

0 commit comments

Comments
 (0)