From 95a7b806c6d9f3cacddce8582c9f0ceb25b94c71 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Thu, 3 Aug 2017 22:39:01 +0000 Subject: [PATCH] Fix the 'addresses' field of container The 'addresses' field is a map of ip addresses. In before, it used docker network name as key, which is hard to be interpreted by users, since docker network is hiden from end-users. This commit switches to use neutron network uuid as key. A use case is from our Heat plugin, which requires neutron network information to do orchestration. Change-Id: Ic3e4ee80962cdd777543e5c9c988fc0cdc623dad --- .../source/samples/container-get-all-resp.json | 2 +- .../source/samples/container-rename-resp.json | 2 +- api-ref/source/samples/container-show-resp.json | 2 +- .../source/samples/container-update-resp.json | 2 +- zun/container/docker/driver.py | 16 ++++++++++------ zun/network/kuryr_network.py | 7 ++++--- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/api-ref/source/samples/container-get-all-resp.json b/api-ref/source/samples/container-get-all-resp.json index 2417b6edf..79fbed488 100644 --- a/api-ref/source/samples/container-get-all-resp.json +++ b/api-ref/source/samples/container-get-all-resp.json @@ -2,7 +2,7 @@ "containers": [ { "addresses": { - "public": [{ + "2ede1821-1334-4e8b-b731-d3a82b41f42f": [{ "version": 4, "addr": "172.24.4.3", "port": "4b077255-9b4d-4068-b24f-b89594f870c2" diff --git a/api-ref/source/samples/container-rename-resp.json b/api-ref/source/samples/container-rename-resp.json index cb1804299..987b5be38 100644 --- a/api-ref/source/samples/container-rename-resp.json +++ b/api-ref/source/samples/container-rename-resp.json @@ -1,6 +1,6 @@ { "addresses": { - "public": [{ + "f82824cf-bd0e-4c39-9450-3cf802ed262f": [{ "version": 4, "addr": "172.24.4.3", "port": "4b077255-9b4d-4068-b24f-b89594f870c2" diff --git a/api-ref/source/samples/container-show-resp.json b/api-ref/source/samples/container-show-resp.json index 902ed36ef..d9a2d35b0 100644 --- a/api-ref/source/samples/container-show-resp.json +++ b/api-ref/source/samples/container-show-resp.json @@ -1,6 +1,6 @@ { "addresses": { - "public": [{ + "c0c5f8ed-6654-4d25-b0de-c858b9f620be": [{ "version": 4, "addr": "172.24.4.3", "port": "4b077255-9b4d-4068-b24f-b89594f870c2" diff --git a/api-ref/source/samples/container-update-resp.json b/api-ref/source/samples/container-update-resp.json index a14775381..8ff6a76bb 100644 --- a/api-ref/source/samples/container-update-resp.json +++ b/api-ref/source/samples/container-update-resp.json @@ -1,6 +1,6 @@ { "addresses": { - "public": [{ + "eb7a3ee0-ad55-417b-ba11-6c57b1dbd4c6": [{ "version": 4, "addr": "172.24.4.3", "port": "4b077255-9b4d-4068-b24f-b89594f870c2" diff --git a/zun/container/docker/driver.py b/zun/container/docker/driver.py index d6f30e951..9a21d3142 100644 --- a/zun/container/docker/driver.py +++ b/zun/container/docker/driver.py @@ -184,7 +184,7 @@ class DockerDriver(driver.ContainerDriver): addrs = network_api.connect_container_to_network( container, docker_net_name, network, security_groups=security_group_ids) - addresses[docker_net_name] = addrs + addresses[network['network']] = addrs return addresses @@ -197,7 +197,8 @@ class DockerDriver(driver.ContainerDriver): if teardown_network: network_api = zun_network.api(context=context, docker_api=docker) - self._cleanup_network_for_container(container, network_api) + self._cleanup_network_for_container(context, container, + network_api) if container.container_id: try: @@ -208,11 +209,13 @@ class DockerDriver(driver.ContainerDriver): return raise - def _cleanup_network_for_container(self, container, network_api): + def _cleanup_network_for_container(self, context, container, network_api): if not container.addresses: return - for name in container.addresses: - network_api.disconnect_container_from_network(container, name) + for neutron_net in container.addresses: + docker_net = self._get_docker_network_name(context, neutron_net) + network_api.disconnect_container_from_network( + container, docker_net, neutron_network_id=neutron_net) def list(self, context): id_to_container_map = {} @@ -674,7 +677,8 @@ class DockerDriver(driver.ContainerDriver): sandbox_id = container.get_sandbox_id() with docker_utils.docker_client() as docker: network_api = zun_network.api(context=context, docker_api=docker) - self._cleanup_network_for_container(container, network_api) + self._cleanup_network_for_container(context, container, + network_api) try: docker.remove_container(sandbox_id, force=True) except errors.APIError as api_error: diff --git a/zun/network/kuryr_network.py b/zun/network/kuryr_network.py index 53f54a267..4d4c44e7d 100644 --- a/zun/network/kuryr_network.py +++ b/zun/network/kuryr_network.py @@ -179,14 +179,15 @@ class KuryrNetwork(network.Network): container_id, network_name, **kwargs) return addresses - def disconnect_container_from_network(self, container, network_name): + def disconnect_container_from_network(self, container, network_name, + neutron_network_id=None): container_id = container.get_sandbox_id() if not container_id: container_id = container.container_id neutron_ports = set() - if container.addresses: - addrs_list = container.addresses.get(network_name, []) + if container.addresses and neutron_network_id: + addrs_list = container.addresses.get(neutron_network_id, []) for addr in addrs_list: port_id = addr['port'] neutron_ports.add(port_id)