Browse Source

Untag resource if it was tagged

On removing a tag to a resource (port/subnet/subnetpool/network),
if the resource doesn't have the tag, skip the neutron API call to
remove_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: Iffa5fbf27c64f1aad0a9bb97da9c4e7c5015a196
Hongbin Lu 3 months ago
parent
commit
b194b0732f

+ 20
- 18
kuryr_libnetwork/controllers.py View File

@@ -358,38 +358,38 @@ def _neutron_net_add_tags(net, tag, tags=True):
358 358
             _neutron_net_add_tag(net, tag)
359 359
 
360 360
 
361
-def _neutron_net_remove_tag(netid, tag):
362
-    _neutron_remove_tag('networks', netid, tag)
361
+def _neutron_net_remove_tag(net, tag):
362
+    _neutron_remove_tag('networks', net, tag)
363 363
 
364 364
 
365
-def _neutron_net_remove_tags(netid, tag):
365
+def _neutron_net_remove_tags(net, tag):
366 366
     tags = utils.create_net_tags(tag)
367 367
     for tag in tags:
368
-        _neutron_net_remove_tag(netid, tag)
368
+        _neutron_net_remove_tag(net, tag)
369 369
 
370 370
 
371 371
 def _neutron_subnetpool_add_tag(pool, tag):
372 372
     _neutron_add_tag('subnetpools', pool, tag)
373 373
 
374 374
 
375
-def _neutron_subnetpool_remove_tag(poolid, tag):
376
-    _neutron_remove_tag('subnetpools', poolid, tag)
375
+def _neutron_subnetpool_remove_tag(pool, tag):
376
+    _neutron_remove_tag('subnetpools', pool, tag)
377 377
 
378 378
 
379 379
 def _neutron_subnet_add_tag(subnet, tag):
380 380
     _neutron_add_tag('subnets', subnet, tag)
381 381
 
382 382
 
383
-def _neutron_subnet_remove_tag(subnetid, tag):
384
-    _neutron_remove_tag('subnets', subnetid, tag)
383
+def _neutron_subnet_remove_tag(subnet, tag):
384
+    _neutron_remove_tag('subnets', subnet, tag)
385 385
 
386 386
 
387 387
 def _neutron_port_add_tag(port, tag):
388 388
     _neutron_add_tag('ports', port, tag)
389 389
 
390 390
 
391
-def _neutron_port_remove_tag(portid, tag):
392
-    _neutron_remove_tag('ports', portid, tag)
391
+def _neutron_port_remove_tag(port, tag):
392
+    _neutron_remove_tag('ports', port, tag)
393 393
 
394 394
 
395 395
 def _neutron_add_tag(resource_type, resource, tag):
@@ -402,8 +402,9 @@ def _neutron_add_tag(resource_type, resource, tag):
402 402
                         "cannot add tag to %s.", resource_type)
403 403
 
404 404
 
405
-def _neutron_remove_tag(resource_type, resource_id, tag):
406
-    app.neutron.remove_tag(resource_type, resource_id, tag)
405
+def _neutron_remove_tag(resource_type, resource, tag):
406
+    if tag in resource['tags']:
407
+        app.neutron.remove_tag(resource_type, resource['id'], tag)
407 408
 
408 409
 
409 410
 def _make_net_identifier(network_id, tags=True):
@@ -982,9 +983,10 @@ def network_driver_delete_network():
982 983
             LOG.warning("Network is a pre existing Neutron "
983 984
                         "network, not deleting in Neutron. "
984 985
                         "removing tags: %s", existing_network_identifier)
985
-            neutron_net_id = existing_networks[0]['id']
986
-            _neutron_net_remove_tags(neutron_net_id, container_net_id)
987
-            _neutron_net_remove_tag(neutron_net_id,
986
+            neutron_net = existing_networks[0]
987
+            neutron_net_id = neutron_net['id']
988
+            _neutron_net_remove_tags(neutron_net, container_net_id)
989
+            _neutron_net_remove_tag(neutron_net,
988 990
                                     utils.existing_net_tag(container_net_id))
989 991
             # Delete subnets created by kuryr
990 992
             filtered_subnets = _get_subnets_by_attrs(
@@ -1783,7 +1785,7 @@ def ipam_release_pool():
1783 1785
             cidr=six.text_type(subnet_cidr))
1784 1786
         for tmp_subnet in subnets_by_cidr:
1785 1787
             if pool_id in tmp_subnet.get('tags', []):
1786
-                _neutron_subnet_remove_tag(tmp_subnet['id'], pool_id)
1788
+                _neutron_subnet_remove_tag(tmp_subnet, pool_id)
1787 1789
                 break
1788 1790
 
1789 1791
     pools = _get_subnetpools_by_attrs(id=pool_id)
@@ -1793,7 +1795,7 @@ def ipam_release_pool():
1793 1795
             tags = pools[0].get('tags', [])
1794 1796
             if const.KURYR_EXISTING_NEUTRON_SUBNETPOOL in tags:
1795 1797
                 _neutron_subnetpool_remove_tag(
1796
-                    pool_id, const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
1798
+                    pools[0], const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
1797 1799
                 LOG.debug('Skip the cleanup since this is an existing Neutron '
1798 1800
                           'subnetpool.')
1799 1801
                 return flask.jsonify(const.SCHEMA['SUCCESS'])
@@ -1878,7 +1880,7 @@ def ipam_release_address():
1878 1880
                         app.neutron.update_port(
1879 1881
                             port['id'], {'port': updated_port})
1880 1882
                         _neutron_port_remove_tag(
1881
-                            port['id'], const.KURYR_EXISTING_NEUTRON_PORT)
1883
+                            port, const.KURYR_EXISTING_NEUTRON_PORT)
1882 1884
     except n_exceptions.NeutronClientException as ex:
1883 1885
         LOG.error("Error happened while fetching "
1884 1886
                   "and deleting port, %s", ex)

+ 9
- 9
kuryr_libnetwork/tests/unit/test_kuryr_existing_network.py View File

@@ -350,6 +350,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
350 350
             t = utils.make_net_tags(docker_network_id)
351 351
             te = t + ',' + utils.existing_net_tag(docker_network_id)
352 352
             tags = utils.create_net_tags(docker_network_id)
353
+            tags += [utils.existing_net_tag(docker_network_id)]
353 354
         else:
354 355
             fake_existing_subnets_response = {
355 356
                 "subnets": []
@@ -367,7 +368,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
367 368
                     "router:external": False,
368 369
                     "segments": [],
369 370
                     "shared": False,
370
-                    "id": fake_neutron_net_id
371
+                    "id": fake_neutron_net_id,
372
+                    "tags": tags if mock_tag.tag else [],
371 373
                 }
372 374
             ]
373 375
         }
@@ -387,8 +389,6 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
387 389
             for tag in tags:
388 390
                 mock_remove_tag.assert_any_call('networks',
389 391
                     fake_neutron_net_id, tag)
390
-            mock_remove_tag.assert_any_call('networks',
391
-                fake_neutron_net_id, utils.existing_net_tag(docker_network_id))
392 392
             mock_list_subnets.assert_called_with(
393 393
                 network_id=fake_neutron_net_id)
394 394
             mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id)
@@ -434,6 +434,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
434 434
             t = utils.make_net_tags(docker_network_id)
435 435
             te = t + ',' + utils.existing_net_tag(docker_network_id)
436 436
             tags = utils.create_net_tags(docker_network_id)
437
+            tags += [utils.existing_net_tag(docker_network_id)]
437 438
         else:
438 439
             fake_existing_subnets_response = {
439 440
                 "subnets": []
@@ -451,7 +452,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
451 452
                     "router:external": False,
452 453
                     "segments": [],
453 454
                     "shared": False,
454
-                    "id": fake_neutron_net_id
455
+                    "id": fake_neutron_net_id,
456
+                    "tags": tags if mock_tag.tag else [],
455 457
                 }
456 458
             ]
457 459
         }
@@ -471,8 +473,6 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
471 473
             for tag in tags:
472 474
                 mock_remove_tag.assert_any_call('networks',
473 475
                     fake_neutron_net_id, tag)
474
-            mock_remove_tag.assert_any_call('networks',
475
-                fake_neutron_net_id, utils.existing_net_tag(docker_network_id))
476 476
             mock_list_subnets.assert_called_with(
477 477
                 network_id=fake_neutron_net_id)
478 478
         else:
@@ -519,6 +519,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
519 519
             t = utils.make_net_tags(docker_network_id)
520 520
             te = t + ',' + utils.existing_net_tag(docker_network_id)
521 521
             tags = utils.create_net_tags(docker_network_id)
522
+            tags += [utils.existing_net_tag(docker_network_id)]
522 523
         else:
523 524
             fake_existing_subnets_response = {
524 525
                 "subnets": []
@@ -536,7 +537,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
536 537
                     "router:external": False,
537 538
                     "segments": [],
538 539
                     "shared": False,
539
-                    "id": fake_neutron_net_id
540
+                    "id": fake_neutron_net_id,
541
+                    "tags": tags if mock_tag.tag else [],
540 542
                 }
541 543
             ]
542 544
         }
@@ -556,8 +558,6 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
556 558
             for tag in tags:
557 559
                 mock_remove_tag.assert_any_call('networks',
558 560
                     fake_neutron_net_id, tag)
559
-            mock_remove_tag.assert_any_call('networks',
560
-                fake_neutron_net_id, utils.existing_net_tag(docker_network_id))
561 561
             mock_list_subnets.assert_called_with(
562 562
                 network_id=fake_neutron_net_id)
563 563
             mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id)

Loading…
Cancel
Save