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
This commit is contained in:
Hongbin Lu 2017-08-03 22:39:01 +00:00
parent c087960c27
commit 95a7b806c6
6 changed files with 18 additions and 13 deletions

View File

@ -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"

View File

@ -1,6 +1,6 @@
{
"addresses": {
"public": [{
"f82824cf-bd0e-4c39-9450-3cf802ed262f": [{
"version": 4,
"addr": "172.24.4.3",
"port": "4b077255-9b4d-4068-b24f-b89594f870c2"

View File

@ -1,6 +1,6 @@
{
"addresses": {
"public": [{
"c0c5f8ed-6654-4d25-b0de-c858b9f620be": [{
"version": 4,
"addr": "172.24.4.3",
"port": "4b077255-9b4d-4068-b24f-b89594f870c2"

View File

@ -1,6 +1,6 @@
{
"addresses": {
"public": [{
"eb7a3ee0-ad55-417b-ba11-6c57b1dbd4c6": [{
"version": 4,
"addr": "172.24.4.3",
"port": "4b077255-9b4d-4068-b24f-b89594f870c2"

View File

@ -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:

View File

@ -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)