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
This commit is contained in:
parent
dc3cf2cc93
commit
b194b0732f
|
@ -358,38 +358,38 @@ def _neutron_net_add_tags(net, tag, tags=True):
|
||||||
_neutron_net_add_tag(net, tag)
|
_neutron_net_add_tag(net, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_net_remove_tag(netid, tag):
|
def _neutron_net_remove_tag(net, tag):
|
||||||
_neutron_remove_tag('networks', netid, tag)
|
_neutron_remove_tag('networks', net, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_net_remove_tags(netid, tag):
|
def _neutron_net_remove_tags(net, tag):
|
||||||
tags = utils.create_net_tags(tag)
|
tags = utils.create_net_tags(tag)
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
_neutron_net_remove_tag(netid, tag)
|
_neutron_net_remove_tag(net, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_subnetpool_add_tag(pool, tag):
|
def _neutron_subnetpool_add_tag(pool, tag):
|
||||||
_neutron_add_tag('subnetpools', pool, tag)
|
_neutron_add_tag('subnetpools', pool, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_subnetpool_remove_tag(poolid, tag):
|
def _neutron_subnetpool_remove_tag(pool, tag):
|
||||||
_neutron_remove_tag('subnetpools', poolid, tag)
|
_neutron_remove_tag('subnetpools', pool, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_subnet_add_tag(subnet, tag):
|
def _neutron_subnet_add_tag(subnet, tag):
|
||||||
_neutron_add_tag('subnets', subnet, tag)
|
_neutron_add_tag('subnets', subnet, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_subnet_remove_tag(subnetid, tag):
|
def _neutron_subnet_remove_tag(subnet, tag):
|
||||||
_neutron_remove_tag('subnets', subnetid, tag)
|
_neutron_remove_tag('subnets', subnet, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_port_add_tag(port, tag):
|
def _neutron_port_add_tag(port, tag):
|
||||||
_neutron_add_tag('ports', port, tag)
|
_neutron_add_tag('ports', port, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_port_remove_tag(portid, tag):
|
def _neutron_port_remove_tag(port, tag):
|
||||||
_neutron_remove_tag('ports', portid, tag)
|
_neutron_remove_tag('ports', port, tag)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_add_tag(resource_type, resource, tag):
|
def _neutron_add_tag(resource_type, resource, tag):
|
||||||
|
@ -402,8 +402,9 @@ def _neutron_add_tag(resource_type, resource, tag):
|
||||||
"cannot add tag to %s.", resource_type)
|
"cannot add tag to %s.", resource_type)
|
||||||
|
|
||||||
|
|
||||||
def _neutron_remove_tag(resource_type, resource_id, tag):
|
def _neutron_remove_tag(resource_type, resource, tag):
|
||||||
app.neutron.remove_tag(resource_type, resource_id, tag)
|
if tag in resource['tags']:
|
||||||
|
app.neutron.remove_tag(resource_type, resource['id'], tag)
|
||||||
|
|
||||||
|
|
||||||
def _make_net_identifier(network_id, tags=True):
|
def _make_net_identifier(network_id, tags=True):
|
||||||
|
@ -982,9 +983,10 @@ def network_driver_delete_network():
|
||||||
LOG.warning("Network is a pre existing Neutron "
|
LOG.warning("Network is a pre existing Neutron "
|
||||||
"network, not deleting in Neutron. "
|
"network, not deleting in Neutron. "
|
||||||
"removing tags: %s", existing_network_identifier)
|
"removing tags: %s", existing_network_identifier)
|
||||||
neutron_net_id = existing_networks[0]['id']
|
neutron_net = existing_networks[0]
|
||||||
_neutron_net_remove_tags(neutron_net_id, container_net_id)
|
neutron_net_id = neutron_net['id']
|
||||||
_neutron_net_remove_tag(neutron_net_id,
|
_neutron_net_remove_tags(neutron_net, container_net_id)
|
||||||
|
_neutron_net_remove_tag(neutron_net,
|
||||||
utils.existing_net_tag(container_net_id))
|
utils.existing_net_tag(container_net_id))
|
||||||
# Delete subnets created by kuryr
|
# Delete subnets created by kuryr
|
||||||
filtered_subnets = _get_subnets_by_attrs(
|
filtered_subnets = _get_subnets_by_attrs(
|
||||||
|
@ -1783,7 +1785,7 @@ def ipam_release_pool():
|
||||||
cidr=six.text_type(subnet_cidr))
|
cidr=six.text_type(subnet_cidr))
|
||||||
for tmp_subnet in subnets_by_cidr:
|
for tmp_subnet in subnets_by_cidr:
|
||||||
if pool_id in tmp_subnet.get('tags', []):
|
if pool_id in tmp_subnet.get('tags', []):
|
||||||
_neutron_subnet_remove_tag(tmp_subnet['id'], pool_id)
|
_neutron_subnet_remove_tag(tmp_subnet, pool_id)
|
||||||
break
|
break
|
||||||
|
|
||||||
pools = _get_subnetpools_by_attrs(id=pool_id)
|
pools = _get_subnetpools_by_attrs(id=pool_id)
|
||||||
|
@ -1793,7 +1795,7 @@ def ipam_release_pool():
|
||||||
tags = pools[0].get('tags', [])
|
tags = pools[0].get('tags', [])
|
||||||
if const.KURYR_EXISTING_NEUTRON_SUBNETPOOL in tags:
|
if const.KURYR_EXISTING_NEUTRON_SUBNETPOOL in tags:
|
||||||
_neutron_subnetpool_remove_tag(
|
_neutron_subnetpool_remove_tag(
|
||||||
pool_id, const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
|
pools[0], const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
|
||||||
LOG.debug('Skip the cleanup since this is an existing Neutron '
|
LOG.debug('Skip the cleanup since this is an existing Neutron '
|
||||||
'subnetpool.')
|
'subnetpool.')
|
||||||
return flask.jsonify(const.SCHEMA['SUCCESS'])
|
return flask.jsonify(const.SCHEMA['SUCCESS'])
|
||||||
|
@ -1878,7 +1880,7 @@ def ipam_release_address():
|
||||||
app.neutron.update_port(
|
app.neutron.update_port(
|
||||||
port['id'], {'port': updated_port})
|
port['id'], {'port': updated_port})
|
||||||
_neutron_port_remove_tag(
|
_neutron_port_remove_tag(
|
||||||
port['id'], const.KURYR_EXISTING_NEUTRON_PORT)
|
port, const.KURYR_EXISTING_NEUTRON_PORT)
|
||||||
except n_exceptions.NeutronClientException as ex:
|
except n_exceptions.NeutronClientException as ex:
|
||||||
LOG.error("Error happened while fetching "
|
LOG.error("Error happened while fetching "
|
||||||
"and deleting port, %s", ex)
|
"and deleting port, %s", ex)
|
||||||
|
|
|
@ -350,6 +350,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
t = utils.make_net_tags(docker_network_id)
|
t = utils.make_net_tags(docker_network_id)
|
||||||
te = t + ',' + utils.existing_net_tag(docker_network_id)
|
te = t + ',' + utils.existing_net_tag(docker_network_id)
|
||||||
tags = utils.create_net_tags(docker_network_id)
|
tags = utils.create_net_tags(docker_network_id)
|
||||||
|
tags += [utils.existing_net_tag(docker_network_id)]
|
||||||
else:
|
else:
|
||||||
fake_existing_subnets_response = {
|
fake_existing_subnets_response = {
|
||||||
"subnets": []
|
"subnets": []
|
||||||
|
@ -367,7 +368,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
"router:external": False,
|
"router:external": False,
|
||||||
"segments": [],
|
"segments": [],
|
||||||
"shared": False,
|
"shared": False,
|
||||||
"id": fake_neutron_net_id
|
"id": fake_neutron_net_id,
|
||||||
|
"tags": tags if mock_tag.tag else [],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -387,8 +389,6 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
mock_remove_tag.assert_any_call('networks',
|
mock_remove_tag.assert_any_call('networks',
|
||||||
fake_neutron_net_id, tag)
|
fake_neutron_net_id, tag)
|
||||||
mock_remove_tag.assert_any_call('networks',
|
|
||||||
fake_neutron_net_id, utils.existing_net_tag(docker_network_id))
|
|
||||||
mock_list_subnets.assert_called_with(
|
mock_list_subnets.assert_called_with(
|
||||||
network_id=fake_neutron_net_id)
|
network_id=fake_neutron_net_id)
|
||||||
mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id)
|
mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id)
|
||||||
|
@ -434,6 +434,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
t = utils.make_net_tags(docker_network_id)
|
t = utils.make_net_tags(docker_network_id)
|
||||||
te = t + ',' + utils.existing_net_tag(docker_network_id)
|
te = t + ',' + utils.existing_net_tag(docker_network_id)
|
||||||
tags = utils.create_net_tags(docker_network_id)
|
tags = utils.create_net_tags(docker_network_id)
|
||||||
|
tags += [utils.existing_net_tag(docker_network_id)]
|
||||||
else:
|
else:
|
||||||
fake_existing_subnets_response = {
|
fake_existing_subnets_response = {
|
||||||
"subnets": []
|
"subnets": []
|
||||||
|
@ -451,7 +452,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
"router:external": False,
|
"router:external": False,
|
||||||
"segments": [],
|
"segments": [],
|
||||||
"shared": False,
|
"shared": False,
|
||||||
"id": fake_neutron_net_id
|
"id": fake_neutron_net_id,
|
||||||
|
"tags": tags if mock_tag.tag else [],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -471,8 +473,6 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
mock_remove_tag.assert_any_call('networks',
|
mock_remove_tag.assert_any_call('networks',
|
||||||
fake_neutron_net_id, tag)
|
fake_neutron_net_id, tag)
|
||||||
mock_remove_tag.assert_any_call('networks',
|
|
||||||
fake_neutron_net_id, utils.existing_net_tag(docker_network_id))
|
|
||||||
mock_list_subnets.assert_called_with(
|
mock_list_subnets.assert_called_with(
|
||||||
network_id=fake_neutron_net_id)
|
network_id=fake_neutron_net_id)
|
||||||
else:
|
else:
|
||||||
|
@ -519,6 +519,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
t = utils.make_net_tags(docker_network_id)
|
t = utils.make_net_tags(docker_network_id)
|
||||||
te = t + ',' + utils.existing_net_tag(docker_network_id)
|
te = t + ',' + utils.existing_net_tag(docker_network_id)
|
||||||
tags = utils.create_net_tags(docker_network_id)
|
tags = utils.create_net_tags(docker_network_id)
|
||||||
|
tags += [utils.existing_net_tag(docker_network_id)]
|
||||||
else:
|
else:
|
||||||
fake_existing_subnets_response = {
|
fake_existing_subnets_response = {
|
||||||
"subnets": []
|
"subnets": []
|
||||||
|
@ -536,7 +537,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
"router:external": False,
|
"router:external": False,
|
||||||
"segments": [],
|
"segments": [],
|
||||||
"shared": False,
|
"shared": False,
|
||||||
"id": fake_neutron_net_id
|
"id": fake_neutron_net_id,
|
||||||
|
"tags": tags if mock_tag.tag else [],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -556,8 +558,6 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
mock_remove_tag.assert_any_call('networks',
|
mock_remove_tag.assert_any_call('networks',
|
||||||
fake_neutron_net_id, tag)
|
fake_neutron_net_id, tag)
|
||||||
mock_remove_tag.assert_any_call('networks',
|
|
||||||
fake_neutron_net_id, utils.existing_net_tag(docker_network_id))
|
|
||||||
mock_list_subnets.assert_called_with(
|
mock_list_subnets.assert_called_with(
|
||||||
network_id=fake_neutron_net_id)
|
network_id=fake_neutron_net_id)
|
||||||
mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id)
|
mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id)
|
||||||
|
|
Loading…
Reference in New Issue