From 01e89e8ceda719e15b3c3c74e1d11b1b1dfa4301 Mon Sep 17 00:00:00 2001
From: WangChangyu <wang.changyu@zte.com.cn>
Date: Mon, 5 Mar 2018 11:49:35 +0800
Subject: [PATCH] update port device_id when device_id is docker endpointID

if port with device_id has been created by user,
kuryr should not release it.

Change-Id: Ia6c8d673b29fea653c034684e846d8104d570d2d
Closes-bug: #1753357
---
 kuryr_libnetwork/controllers.py                | 7 ++++---
 kuryr_libnetwork/tests/unit/test_kuryr_ipam.py | 5 ++++-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/kuryr_libnetwork/controllers.py b/kuryr_libnetwork/controllers.py
index e3e79b45..73b39963 100644
--- a/kuryr_libnetwork/controllers.py
+++ b/kuryr_libnetwork/controllers.py
@@ -118,8 +118,7 @@ def get_driver(port):
     if driver is None:
         raise exceptions.KuryrException(
             "No port driver available for VNIC type %s" % vnic_type)
-    else:
-        return driver
+    return driver
 
 
 def _cache_default_subnetpool_ids(app):
@@ -1879,7 +1878,9 @@ def ipam_release_address():
                         app.neutron.delete_port(port['id'])
             elif tags and const.KURYR_EXISTING_NEUTRON_PORT in tags:
                 updated_port = {'name': '', 'device_owner': '',
-                                'device_id': '', 'binding:host_id': ''}
+                                'binding:host_id': ''}
+                if port['name'].startswith(port['device_id']):
+                    updated_port["device_id"] = ''
                 app.neutron.update_port(port['id'], {'port': updated_port})
                 _neutron_port_remove_tag(port['id'],
                                          const.KURYR_EXISTING_NEUTRON_PORT)
diff --git a/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py b/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py
index 2bfbaade..31c54d22 100644
--- a/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py
+++ b/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py
@@ -1562,7 +1562,10 @@ class TestKuryrIpam(base.TestKuryrBase):
         mock_list_subnets.assert_called_with(
             subnetpool_id=fake_kuryr_subnetpool_id)
         mock_list_ports.assert_called()
-        mock_update_port.assert_called()
+        expect_updated_port = {'name': '', 'device_owner': '',
+                               'device_id': '', 'binding:host_id': ''}
+        mock_update_port.assert_called_with(fake_port['port']['id'],
+                                            {'port': expect_updated_port})
         mock_delete_port.assert_not_called()
         mock_remove_tag.assert_called_with('ports',
                                            fake_port['port']['id'],