Merge branch 'master'

(Old PR unintentionally went to the `master` branch.)
This commit is contained in:
Milas Bowman 2023-01-27 09:27:42 -05:00
commit aca129dd69
2 changed files with 98 additions and 2 deletions

View File

@ -679,6 +679,10 @@ class ContainerCollection(Collection):
This mode is incompatible with ``ports``. This mode is incompatible with ``ports``.
Incompatible with ``network``. Incompatible with ``network``.
network_driver_opt (dict): A dictionary of options to provide
to the network driver. Defaults to ``None``. Used in
conjuction with ``network``. Incompatible
with ``network_mode``.
oom_kill_disable (bool): Whether to disable OOM killer. oom_kill_disable (bool): Whether to disable OOM killer.
oom_score_adj (int): An integer value containing the score given oom_score_adj (int): An integer value containing the score given
to the container in order to tune OOM killer preferences. to the container in order to tune OOM killer preferences.
@ -843,6 +847,12 @@ class ContainerCollection(Collection):
'together.' 'together.'
) )
if kwargs.get('network_driver_opt') and not kwargs.get('network'):
raise RuntimeError(
'The options "network_driver_opt" can not be used '
'without "network".'
)
try: try:
container = self.create(image=image, command=command, container = self.create(image=image, command=command,
detach=detach, **kwargs) detach=detach, **kwargs)
@ -1113,8 +1123,12 @@ def _create_container_args(kwargs):
host_config_kwargs['binds'] = volumes host_config_kwargs['binds'] = volumes
network = kwargs.pop('network', None) network = kwargs.pop('network', None)
network_driver_opt = kwargs.pop('network_driver_opt', None)
if network: if network:
create_kwargs['networking_config'] = {network: None} network_configuration = {'driver_opt': network_driver_opt} \
if network_driver_opt else None
create_kwargs['networking_config'] = {network: network_configuration}
host_config_kwargs['network_mode'] = network host_config_kwargs['network_mode'] = network
# All kwargs should have been consumed by this point, so raise # All kwargs should have been consumed by this point, so raise

View File

@ -74,6 +74,7 @@ class ContainerCollectionTest(unittest.TestCase):
name='somename', name='somename',
network_disabled=False, network_disabled=False,
network='foo', network='foo',
network_driver_opt={'key1': 'a'},
oom_kill_disable=True, oom_kill_disable=True,
oom_score_adj=5, oom_score_adj=5,
pid_mode='host', pid_mode='host',
@ -188,7 +189,7 @@ class ContainerCollectionTest(unittest.TestCase):
mac_address='abc123', mac_address='abc123',
name='somename', name='somename',
network_disabled=False, network_disabled=False,
networking_config={'foo': None}, networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
platform='linux', platform='linux',
ports=[('1111', 'tcp'), ('2222', 'tcp')], ports=[('1111', 'tcp'), ('2222', 'tcp')],
stdin_open=True, stdin_open=True,
@ -345,6 +346,42 @@ class ContainerCollectionTest(unittest.TestCase):
host_config={'NetworkMode': 'default'}, host_config={'NetworkMode': 'default'},
) )
def test_run_network_driver_opts_without_network(self):
client = make_fake_client()
with pytest.raises(RuntimeError):
client.containers.run(
image='alpine',
network_driver_opt={'key1': 'a'}
)
def test_run_network_driver_opts_with_network_mode(self):
client = make_fake_client()
with pytest.raises(RuntimeError):
client.containers.run(
image='alpine',
network_mode='none',
network_driver_opt={'key1': 'a'}
)
def test_run_network_driver_opts(self):
client = make_fake_client()
client.containers.run(
image='alpine',
network='foo',
network_driver_opt={'key1': 'a'}
)
client.api.create_container.assert_called_with(
detach=False,
image='alpine',
command=None,
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
host_config={'NetworkMode': 'foo'}
)
def test_create(self): def test_create(self):
client = make_fake_client() client = make_fake_client()
container = client.containers.create( container = client.containers.create(
@ -372,6 +409,51 @@ class ContainerCollectionTest(unittest.TestCase):
host_config={'NetworkMode': 'default'} host_config={'NetworkMode': 'default'}
) )
def test_create_network_driver_opts_without_network(self):
client = make_fake_client()
client.containers.create(
image='alpine',
network_driver_opt={'key1': 'a'}
)
client.api.create_container.assert_called_with(
image='alpine',
command=None,
host_config={'NetworkMode': 'default'}
)
def test_create_network_driver_opts_with_network_mode(self):
client = make_fake_client()
client.containers.create(
image='alpine',
network_mode='none',
network_driver_opt={'key1': 'a'}
)
client.api.create_container.assert_called_with(
image='alpine',
command=None,
host_config={'NetworkMode': 'none'}
)
def test_create_network_driver_opts(self):
client = make_fake_client()
client.containers.create(
image='alpine',
network='foo',
network_driver_opt={'key1': 'a'}
)
client.api.create_container.assert_called_with(
image='alpine',
command=None,
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
host_config={'NetworkMode': 'foo'}
)
def test_get(self): def test_get(self):
client = make_fake_client() client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID) container = client.containers.get(FAKE_CONTAINER_ID)