Skip to content

Commit

Permalink
Restore old behavior, and only provide all networks on creation for A…
Browse files Browse the repository at this point in the history
…PI 1.44+.
  • Loading branch information
felixfontein committed Jul 20, 2024
1 parent ebe2fd0 commit 00fb562
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 7 additions & 3 deletions changelogs/fragments/933-docker_container-networks.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
minor_changes:
- "docker_container - when creating a network, directly pass the networks to connect to to the Docker Daemon.
This makes creation more efficient and works around a bug in Docker Daemon that does not use the specified
MAC address in at least some cases, though only for creation (https://github.com/ansible-collections/community.docker/pull/933)."
- "docker_container - when creating a container, directly pass all networks to connect to to the Docker Daemon
for API version 1.44 and newer. This makes creation more efficient and works around a bug in Docker Daemon that
does not use the specified MAC address in at least some cases, though only for creation
(https://github.com/ansible-collections/community.docker/pull/933)."
bugfixes:
- "docker_container - restore behavior of the module from community.docker 2.x.y that passes the first network
to the Docker Deamon while creating the container (https://github.com/ansible-collections/community.docker/pull/933).
known_issues:
- "docker_container - when specifying a MAC address for a container's network, and the network is attached
after container creation (for example, due to idempotency checks), the MAC address is at least in some
Expand Down
3 changes: 3 additions & 0 deletions plugins/module_utils/module_container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ def disconnect_container_from_network(self, client, container_id, network_id):
def connect_container_to_network(self, client, container_id, network_id, parameters=None):
pass

def create_container_supports_more_than_one_network(self, client):
return False

@abc.abstractmethod
def create_container(self, client, container_name, create_parameters, networks=None):
pass
Expand Down
9 changes: 6 additions & 3 deletions plugins/module_utils/module_container/docker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,20 @@ def connect_container_to_network(self, client, container_id, network_id, paramet
}
client.post_json('/networks/{0}/connect', network_id, data=data)

def create_container_supports_more_than_one_network(self, client):
return client.docker_api_version >= LooseVersion('1.44')

def create_container(self, client, container_name, create_parameters, networks=None):
params = {'name': container_name}
if 'platform' in create_parameters:
params['platform'] = create_parameters.pop('platform')
if networks is not None:
create_parameters = create_parameters.copy()
create_parameters['NetworkingConfig'] = {
'EndpointsConfig': {
network: self._create_endpoint_config(network_params)
'EndpointsConfig': dict(
(network, self._create_endpoint_config(network_params)
for network, network_params in networks.items()
}
)
}
new_container = client.post_json_to_json('/containers/create', data=create_parameters, params=params)
client.report_warnings(new_container)
Expand Down
7 changes: 5 additions & 2 deletions plugins/module_utils/module_container/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,11 @@ def container_create(self, image):
self.log("image: %s parameters:" % image)
self.log(create_parameters, pretty_print=True)
networks = {}
if self.module.params['networks']:
for network in self.module.params['networks']:
if self.param_networks_cli_compatible and self.module.params['networks']:
network_list = self.module.params['networks']
if not self.engine_driver.create_container_supports_more_than_one_network(self.client):
network_list = network_list[:1]
for network in network_list:
networks[network['name']] = {
key: value for key, value in network.items()
if key not in ('name', 'id')
Expand Down

0 comments on commit 00fb562

Please sign in to comment.