Browse Source

Tag resource if it was not tagged

On adding a tag to a resource (port/subnet/subnetpool/network),
if the resource already has the tag, skip the neutron API call to
add_tag because the tags will be unchanged.
This will avoid one or two Neutron API calls and optimize the
performance a bit.

Partial-Bug: #1809306
Change-Id: I8a181d6af5228007d36fa8b217fec7f766dd37fd
Hongbin Lu 3 months ago
parent
commit
dc3cf2cc93

+ 31
- 29
kuryr_libnetwork/controllers.py View File

@@ -347,15 +347,15 @@ def _create_or_update_port(neutron_network_id, endpoint_id,
347 347
     return response_port, subnets
348 348
 
349 349
 
350
-def _neutron_net_add_tag(netid, tag):
351
-    _neutron_add_tag('networks', netid, tag)
350
+def _neutron_net_add_tag(net, tag):
351
+    _neutron_add_tag('networks', net, tag)
352 352
 
353 353
 
354
-def _neutron_net_add_tags(netid, tag, tags=True):
354
+def _neutron_net_add_tags(net, tag, tags=True):
355 355
     if tags:
356 356
         tags = utils.create_net_tags(tag)
357 357
         for tag in tags:
358
-            _neutron_net_add_tag(netid, tag)
358
+            _neutron_net_add_tag(net, tag)
359 359
 
360 360
 
361 361
 def _neutron_net_remove_tag(netid, tag):
@@ -368,37 +368,38 @@ def _neutron_net_remove_tags(netid, tag):
368 368
         _neutron_net_remove_tag(netid, tag)
369 369
 
370 370
 
371
-def _neutron_subnetpool_add_tag(poolid, tag):
372
-    _neutron_add_tag('subnetpools', poolid, tag)
371
+def _neutron_subnetpool_add_tag(pool, tag):
372
+    _neutron_add_tag('subnetpools', pool, tag)
373 373
 
374 374
 
375 375
 def _neutron_subnetpool_remove_tag(poolid, tag):
376 376
     _neutron_remove_tag('subnetpools', poolid, tag)
377 377
 
378 378
 
379
-def _neutron_subnet_add_tag(subnetid, tag):
380
-    _neutron_add_tag('subnets', subnetid, tag)
379
+def _neutron_subnet_add_tag(subnet, tag):
380
+    _neutron_add_tag('subnets', subnet, tag)
381 381
 
382 382
 
383 383
 def _neutron_subnet_remove_tag(subnetid, tag):
384 384
     _neutron_remove_tag('subnets', subnetid, tag)
385 385
 
386 386
 
387
-def _neutron_port_add_tag(portid, tag):
388
-    _neutron_add_tag('ports', portid, tag)
387
+def _neutron_port_add_tag(port, tag):
388
+    _neutron_add_tag('ports', port, tag)
389 389
 
390 390
 
391 391
 def _neutron_port_remove_tag(portid, tag):
392 392
     _neutron_remove_tag('ports', portid, tag)
393 393
 
394 394
 
395
-def _neutron_add_tag(resource_type, resource_id, tag):
396
-    try:
397
-        app.neutron.add_tag(resource_type, resource_id, tag)
398
-    except n_exceptions.NotFound:
399
-        LOG.warning("Neutron tags extension for given "
400
-                    "resource type is not supported, "
401
-                    "cannot add tag to %s.", resource_type)
395
+def _neutron_add_tag(resource_type, resource, tag):
396
+    if tag not in resource['tags']:
397
+        try:
398
+            app.neutron.add_tag(resource_type, resource['id'], tag)
399
+        except n_exceptions.NotFound:
400
+            LOG.warning("Neutron tags extension for given "
401
+                        "resource type is not supported, "
402
+                        "cannot add tag to %s.", resource_type)
402 403
 
403 404
 
404 405
 def _neutron_remove_tag(resource_type, resource_id, tag):
@@ -564,7 +565,7 @@ def _create_kuryr_subnet(pool_cidr, subnet_cidr, pool_id, network_id, gateway):
564 565
     subnets = app.neutron.create_subnet({'subnets': new_kuryr_subnet})
565 566
     if app.tag_ext:
566 567
         for subnet in subnets['subnets']:
567
-            _neutron_subnet_add_tag(subnet['id'], pool_id)
568
+            _neutron_subnet_add_tag(subnet, pool_id)
568 569
     LOG.debug("Created kuryr subnet %s", new_kuryr_subnet)
569 570
 
570 571
 
@@ -592,7 +593,7 @@ def _create_kuryr_subnetpool(pool_cidr, pool_tag, shared):
592 593
         {'subnetpool': new_subnetpool})
593 594
     pool = created_subnetpool_response['subnetpool']
594 595
     if pool_tag:
595
-        _neutron_subnetpool_add_tag(pool['id'], pool_tag)
596
+        _neutron_subnetpool_add_tag(pool, pool_tag)
596 597
     return pool
597 598
 
598 599
 
@@ -854,9 +855,9 @@ def network_driver_create_network():
854 855
         network = app.neutron.create_network(
855 856
             {'network': {'name': neutron_network_name,
856 857
                          "admin_state_up": True,
857
-                         'shared': shared}})
858
-        network_id = network['network']['id']
859
-        _neutron_net_add_tags(network['network']['id'], container_net_id,
858
+                         'shared': shared}})['network']
859
+        network_id = network['id']
860
+        _neutron_net_add_tags(network, container_net_id,
860 861
                               tags=app.tag)
861 862
 
862 863
         LOG.info("Created a new network with name "
@@ -875,15 +876,16 @@ def network_driver_create_network():
875 876
                 raise exceptions.KuryrException(
876 877
                     ("Specified network id/name({0}) does not "
877 878
                      "exist.").format(specified_network))
878
-            network_id = networks[0]['id']
879
+            network = networks[0]
880
+            network_id = network['id']
879 881
             network_shared = networks[0]['shared']
880 882
         except n_exceptions.NeutronClientException as ex:
881 883
             LOG.error("Error happened during listing "
882 884
                       "Neutron networks: %s", ex)
883 885
             raise
884 886
         if app.tag:
885
-            _neutron_net_add_tags(network_id, container_net_id, tags=app.tag)
886
-            _neutron_net_add_tag(network_id,
887
+            _neutron_net_add_tags(network, container_net_id, tags=app.tag)
888
+            _neutron_net_add_tag(network,
887 889
                                  utils.existing_net_tag(container_net_id))
888 890
         else:
889 891
             network = app.neutron.update_network(
@@ -924,7 +926,7 @@ def network_driver_create_network():
924 926
 
925 927
     def _add_tag_for_existing_subnet(subnet, pool_id):
926 928
         if len(subnet) == 1:
927
-            _neutron_subnet_add_tag(subnet[0]['id'], pool_id)
929
+            _neutron_subnet_add_tag(subnet[0], pool_id)
928 930
 
929 931
     # This will add a subnetpool_id(created by kuryr) tag
930 932
     # for existing Neutron subnets.
@@ -1558,7 +1560,7 @@ def ipam_request_pool():
1558 1560
             pool_id = existing_pools[0]['id']
1559 1561
             if app.tag_ext:
1560 1562
                 _neutron_subnetpool_add_tag(
1561
-                    pool_id, const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
1563
+                    existing_pools[0], const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
1562 1564
             prefixes = existing_pools[0]['prefixes']
1563 1565
             pool_cidr = ipaddress.ip_network(six.text_type(prefixes[0]))
1564 1566
             if pool_cidr == cidr:
@@ -1714,13 +1716,13 @@ def ipam_request_address():
1714 1716
                     # be deleted in ipam_release_address.
1715 1717
                     if app.tag_ext:
1716 1718
                         _neutron_port_add_tag(
1717
-                            created_port['id'],
1719
+                            created_port,
1718 1720
                             const.KURYR_EXISTING_NEUTRON_PORT)
1719 1721
                 else:
1720 1722
                     created_port_resp = app.neutron.create_port({'port': port})
1721 1723
                     created_port = created_port_resp['port']
1722 1724
                     if app.tag_ext:
1723
-                        _neutron_port_add_tag(created_port['id'],
1725
+                        _neutron_port_add_tag(created_port,
1724 1726
                                               lib_const.DEVICE_OWNER)
1725 1727
 
1726 1728
                 LOG.debug("created port %s", created_port)

+ 13
- 6
kuryr_libnetwork/tests/unit/base.py View File

@@ -101,9 +101,11 @@ class TestKuryrBase(TestCase):
101 101
 
102 102
     @staticmethod
103 103
     def _get_fake_v6_subnetpools(subnetpool_id, prefixes=['fe80::/64'],
104
-                                 name="kuryr6"):
104
+                                 name="kuryr6", tags=None):
105 105
         # The following fake response is retrieved from the Neutron doc:
106 106
         #   http://developer.openstack.org/api-ref-networking-v2-ext.html#listSubnetPools  # noqa
107
+        if tags is None:
108
+            tags = []
107 109
         v6_subnetpools = {
108 110
             "subnetpools": [{
109 111
                 "min_prefixlen": "64",
@@ -116,7 +118,8 @@ class TestKuryrBase(TestCase):
116 118
                 "tenant_id": "9fadcee8aa7c40cdb2114fff7d569c08",
117 119
                 "prefixes": prefixes,
118 120
                 "ip_version": 6,
119
-                "shared": False
121
+                "shared": False,
122
+                "tags": tags,
120 123
             }]
121 124
         }
122 125
 
@@ -233,7 +236,7 @@ class TestKuryrBase(TestCase):
233 236
     @staticmethod
234 237
     def _get_fake_v4_subnet(neutron_network_id, docker_endpoint_id=None,
235 238
                             subnet_v4_id=None, subnetpool_id=None,
236
-                            cidr='192.168.1.0/24',
239
+                            cidr='192.168.1.0/24', tag_subnetpool_id=True,
237 240
                             name=None, host_routes=None, tags=None):
238 241
         if host_routes is None:
239 242
             host_routes = []
@@ -266,16 +269,19 @@ class TestKuryrBase(TestCase):
266 269
         }
267 270
         if subnetpool_id:
268 271
             fake_v4_subnet['subnet'].update(subnetpool_id=subnetpool_id)
269
-            fake_v4_subnet['subnet'].get('tags').append(subnetpool_id)
272
+            if tag_subnetpool_id:
273
+                fake_v4_subnet['subnet'].get('tags').append(subnetpool_id)
270 274
 
271 275
         return fake_v4_subnet
272 276
 
273 277
     @staticmethod
274 278
     def _get_fake_v6_subnet(docker_network_id, docker_endpoint_id=None,
275 279
                             subnet_v6_id=None, subnetpool_id=None,
276
-                            cidr='fe80::/64', name=None):
280
+                            cidr='fe80::/64', name=None, tags=None):
277 281
         if not name:
278 282
             name = str('-'.join([docker_endpoint_id, 'fe80::']))
283
+        if not tags:
284
+            tags = []
279 285
         gateway_ip = netaddr.IPNetwork(cidr).network + 1
280 286
         start_ip = gateway_ip + 1
281 287
         end_ip = netaddr.IPNetwork(cidr).broadcast - 1
@@ -292,7 +298,8 @@ class TestKuryrBase(TestCase):
292 298
                 "ip_version": 6,
293 299
                 "cidr": cidr,
294 300
                 "id": subnet_v6_id,
295
-                "enable_dhcp": True
301
+                "enable_dhcp": True,
302
+                "tags": tags,
296 303
             }
297 304
         }
298 305
         if subnetpool_id:

+ 9
- 1
kuryr_libnetwork/tests/unit/test_kuryr.py View File

@@ -104,6 +104,7 @@ class TestKuryr(base.TestKuryrBase):
104 104
             "segments": [],
105 105
             "shared": False,
106 106
             "id": fake_neutron_net_id,
107
+            "tags": [],
107 108
         }
108 109
 
109 110
         if driver_value:
@@ -231,6 +232,7 @@ class TestKuryr(base.TestKuryrBase):
231 232
             "segments": [],
232 233
             "shared": False,
233 234
             "id": fake_neutron_net_id,
235
+            "tags": [],
234 236
         }
235 237
 
236 238
         if driver_value:
@@ -370,6 +372,7 @@ class TestKuryr(base.TestKuryrBase):
370 372
             "segments": [],
371 373
             "shared": False,
372 374
             "id": fake_neutron_net_id,
375
+            "tags": [],
373 376
         }
374 377
 
375 378
         if driver_value:
@@ -535,7 +538,8 @@ class TestKuryr(base.TestKuryrBase):
535 538
                 "segments": [],
536 539
                 "shared": False,
537 540
                 "id": fake_neutron_net_id,
538
-                "name": fake_neutron_net_name
541
+                "name": fake_neutron_net_name,
542
+                "tags": [],
539 543
             }]
540 544
         }
541 545
         tags = utils.create_net_tags(docker_network_id)
@@ -670,6 +674,7 @@ class TestKuryr(base.TestKuryrBase):
670 674
                 "segments": [],
671 675
                 "shared": False,
672 676
                 "id": fake_neutron_net_id,
677
+                "tags": [],
673 678
             }]
674 679
         }
675 680
 
@@ -813,6 +818,7 @@ class TestKuryr(base.TestKuryrBase):
813 818
             "segments": [],
814 819
             "shared": False,
815 820
             "id": fake_neutron_net_id,
821
+            "tags": [],
816 822
         }
817 823
 
818 824
         if driver_value:
@@ -979,6 +985,7 @@ class TestKuryr(base.TestKuryrBase):
979 985
             "segments": [],
980 986
             "shared": False,
981 987
             "id": fake_neutron_net_id,
988
+            "tags": [],
982 989
         }
983 990
 
984 991
         if driver_value:
@@ -1141,6 +1148,7 @@ class TestKuryr(base.TestKuryrBase):
1141 1148
             "segments": [],
1142 1149
             "shared": False,
1143 1150
             "id": fake_neutron_net_id,
1151
+            "tags": [],
1144 1152
         }
1145 1153
 
1146 1154
         if driver_value:

+ 3
- 1
kuryr_libnetwork/tests/unit/test_kuryr_existing_network.py View File

@@ -39,7 +39,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
39 39
                     "router:external": False,
40 40
                     "segments": [],
41 41
                     "shared": False,
42
-                    "id": fake_neutron_net_id
42
+                    "id": fake_neutron_net_id,
43
+                    "tags": [],
43 44
                 }
44 45
             ]
45 46
         }
@@ -247,6 +248,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
247 248
             docker_endpoint_id="fake_id",
248 249
             subnet_v4_id=subnet_v4_id,
249 250
             subnetpool_id=fake_kuryr_v4_subnetpool_id,
251
+            tag_subnetpool_id=False,
250 252
             cidr=fake_cidr_v4)
251 253
         fake_v6_subnet = self._get_fake_v6_subnet(
252 254
             fake_neutron_net_id,

Loading…
Cancel
Save