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
This commit is contained in:
parent
610fd5f024
commit
dc3cf2cc93
|
@ -347,15 +347,15 @@ def _create_or_update_port(neutron_network_id, endpoint_id,
|
|||
return response_port, subnets
|
||||
|
||||
|
||||
def _neutron_net_add_tag(netid, tag):
|
||||
_neutron_add_tag('networks', netid, tag)
|
||||
def _neutron_net_add_tag(net, tag):
|
||||
_neutron_add_tag('networks', net, tag)
|
||||
|
||||
|
||||
def _neutron_net_add_tags(netid, tag, tags=True):
|
||||
def _neutron_net_add_tags(net, tag, tags=True):
|
||||
if tags:
|
||||
tags = utils.create_net_tags(tag)
|
||||
for tag in tags:
|
||||
_neutron_net_add_tag(netid, tag)
|
||||
_neutron_net_add_tag(net, tag)
|
||||
|
||||
|
||||
def _neutron_net_remove_tag(netid, tag):
|
||||
|
@ -368,37 +368,38 @@ def _neutron_net_remove_tags(netid, tag):
|
|||
_neutron_net_remove_tag(netid, tag)
|
||||
|
||||
|
||||
def _neutron_subnetpool_add_tag(poolid, tag):
|
||||
_neutron_add_tag('subnetpools', poolid, tag)
|
||||
def _neutron_subnetpool_add_tag(pool, tag):
|
||||
_neutron_add_tag('subnetpools', pool, tag)
|
||||
|
||||
|
||||
def _neutron_subnetpool_remove_tag(poolid, tag):
|
||||
_neutron_remove_tag('subnetpools', poolid, tag)
|
||||
|
||||
|
||||
def _neutron_subnet_add_tag(subnetid, tag):
|
||||
_neutron_add_tag('subnets', subnetid, tag)
|
||||
def _neutron_subnet_add_tag(subnet, tag):
|
||||
_neutron_add_tag('subnets', subnet, tag)
|
||||
|
||||
|
||||
def _neutron_subnet_remove_tag(subnetid, tag):
|
||||
_neutron_remove_tag('subnets', subnetid, tag)
|
||||
|
||||
|
||||
def _neutron_port_add_tag(portid, tag):
|
||||
_neutron_add_tag('ports', portid, tag)
|
||||
def _neutron_port_add_tag(port, tag):
|
||||
_neutron_add_tag('ports', port, tag)
|
||||
|
||||
|
||||
def _neutron_port_remove_tag(portid, tag):
|
||||
_neutron_remove_tag('ports', portid, tag)
|
||||
|
||||
|
||||
def _neutron_add_tag(resource_type, resource_id, tag):
|
||||
try:
|
||||
app.neutron.add_tag(resource_type, resource_id, tag)
|
||||
except n_exceptions.NotFound:
|
||||
LOG.warning("Neutron tags extension for given "
|
||||
"resource type is not supported, "
|
||||
"cannot add tag to %s.", resource_type)
|
||||
def _neutron_add_tag(resource_type, resource, tag):
|
||||
if tag not in resource['tags']:
|
||||
try:
|
||||
app.neutron.add_tag(resource_type, resource['id'], tag)
|
||||
except n_exceptions.NotFound:
|
||||
LOG.warning("Neutron tags extension for given "
|
||||
"resource type is not supported, "
|
||||
"cannot add tag to %s.", resource_type)
|
||||
|
||||
|
||||
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):
|
|||
subnets = app.neutron.create_subnet({'subnets': new_kuryr_subnet})
|
||||
if app.tag_ext:
|
||||
for subnet in subnets['subnets']:
|
||||
_neutron_subnet_add_tag(subnet['id'], pool_id)
|
||||
_neutron_subnet_add_tag(subnet, pool_id)
|
||||
LOG.debug("Created kuryr subnet %s", new_kuryr_subnet)
|
||||
|
||||
|
||||
|
@ -592,7 +593,7 @@ def _create_kuryr_subnetpool(pool_cidr, pool_tag, shared):
|
|||
{'subnetpool': new_subnetpool})
|
||||
pool = created_subnetpool_response['subnetpool']
|
||||
if pool_tag:
|
||||
_neutron_subnetpool_add_tag(pool['id'], pool_tag)
|
||||
_neutron_subnetpool_add_tag(pool, pool_tag)
|
||||
return pool
|
||||
|
||||
|
||||
|
@ -854,9 +855,9 @@ def network_driver_create_network():
|
|||
network = app.neutron.create_network(
|
||||
{'network': {'name': neutron_network_name,
|
||||
"admin_state_up": True,
|
||||
'shared': shared}})
|
||||
network_id = network['network']['id']
|
||||
_neutron_net_add_tags(network['network']['id'], container_net_id,
|
||||
'shared': shared}})['network']
|
||||
network_id = network['id']
|
||||
_neutron_net_add_tags(network, container_net_id,
|
||||
tags=app.tag)
|
||||
|
||||
LOG.info("Created a new network with name "
|
||||
|
@ -875,15 +876,16 @@ def network_driver_create_network():
|
|||
raise exceptions.KuryrException(
|
||||
("Specified network id/name({0}) does not "
|
||||
"exist.").format(specified_network))
|
||||
network_id = networks[0]['id']
|
||||
network = networks[0]
|
||||
network_id = network['id']
|
||||
network_shared = networks[0]['shared']
|
||||
except n_exceptions.NeutronClientException as ex:
|
||||
LOG.error("Error happened during listing "
|
||||
"Neutron networks: %s", ex)
|
||||
raise
|
||||
if app.tag:
|
||||
_neutron_net_add_tags(network_id, container_net_id, tags=app.tag)
|
||||
_neutron_net_add_tag(network_id,
|
||||
_neutron_net_add_tags(network, container_net_id, tags=app.tag)
|
||||
_neutron_net_add_tag(network,
|
||||
utils.existing_net_tag(container_net_id))
|
||||
else:
|
||||
network = app.neutron.update_network(
|
||||
|
@ -924,7 +926,7 @@ def network_driver_create_network():
|
|||
|
||||
def _add_tag_for_existing_subnet(subnet, pool_id):
|
||||
if len(subnet) == 1:
|
||||
_neutron_subnet_add_tag(subnet[0]['id'], pool_id)
|
||||
_neutron_subnet_add_tag(subnet[0], pool_id)
|
||||
|
||||
# This will add a subnetpool_id(created by kuryr) tag
|
||||
# for existing Neutron subnets.
|
||||
|
@ -1558,7 +1560,7 @@ def ipam_request_pool():
|
|||
pool_id = existing_pools[0]['id']
|
||||
if app.tag_ext:
|
||||
_neutron_subnetpool_add_tag(
|
||||
pool_id, const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
|
||||
existing_pools[0], const.KURYR_EXISTING_NEUTRON_SUBNETPOOL)
|
||||
prefixes = existing_pools[0]['prefixes']
|
||||
pool_cidr = ipaddress.ip_network(six.text_type(prefixes[0]))
|
||||
if pool_cidr == cidr:
|
||||
|
@ -1714,13 +1716,13 @@ def ipam_request_address():
|
|||
# be deleted in ipam_release_address.
|
||||
if app.tag_ext:
|
||||
_neutron_port_add_tag(
|
||||
created_port['id'],
|
||||
created_port,
|
||||
const.KURYR_EXISTING_NEUTRON_PORT)
|
||||
else:
|
||||
created_port_resp = app.neutron.create_port({'port': port})
|
||||
created_port = created_port_resp['port']
|
||||
if app.tag_ext:
|
||||
_neutron_port_add_tag(created_port['id'],
|
||||
_neutron_port_add_tag(created_port,
|
||||
lib_const.DEVICE_OWNER)
|
||||
|
||||
LOG.debug("created port %s", created_port)
|
||||
|
|
|
@ -101,9 +101,11 @@ class TestKuryrBase(TestCase):
|
|||
|
||||
@staticmethod
|
||||
def _get_fake_v6_subnetpools(subnetpool_id, prefixes=['fe80::/64'],
|
||||
name="kuryr6"):
|
||||
name="kuryr6", tags=None):
|
||||
# The following fake response is retrieved from the Neutron doc:
|
||||
# http://developer.openstack.org/api-ref-networking-v2-ext.html#listSubnetPools # noqa
|
||||
if tags is None:
|
||||
tags = []
|
||||
v6_subnetpools = {
|
||||
"subnetpools": [{
|
||||
"min_prefixlen": "64",
|
||||
|
@ -116,7 +118,8 @@ class TestKuryrBase(TestCase):
|
|||
"tenant_id": "9fadcee8aa7c40cdb2114fff7d569c08",
|
||||
"prefixes": prefixes,
|
||||
"ip_version": 6,
|
||||
"shared": False
|
||||
"shared": False,
|
||||
"tags": tags,
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -233,7 +236,7 @@ class TestKuryrBase(TestCase):
|
|||
@staticmethod
|
||||
def _get_fake_v4_subnet(neutron_network_id, docker_endpoint_id=None,
|
||||
subnet_v4_id=None, subnetpool_id=None,
|
||||
cidr='192.168.1.0/24',
|
||||
cidr='192.168.1.0/24', tag_subnetpool_id=True,
|
||||
name=None, host_routes=None, tags=None):
|
||||
if host_routes is None:
|
||||
host_routes = []
|
||||
|
@ -266,16 +269,19 @@ class TestKuryrBase(TestCase):
|
|||
}
|
||||
if subnetpool_id:
|
||||
fake_v4_subnet['subnet'].update(subnetpool_id=subnetpool_id)
|
||||
fake_v4_subnet['subnet'].get('tags').append(subnetpool_id)
|
||||
if tag_subnetpool_id:
|
||||
fake_v4_subnet['subnet'].get('tags').append(subnetpool_id)
|
||||
|
||||
return fake_v4_subnet
|
||||
|
||||
@staticmethod
|
||||
def _get_fake_v6_subnet(docker_network_id, docker_endpoint_id=None,
|
||||
subnet_v6_id=None, subnetpool_id=None,
|
||||
cidr='fe80::/64', name=None):
|
||||
cidr='fe80::/64', name=None, tags=None):
|
||||
if not name:
|
||||
name = str('-'.join([docker_endpoint_id, 'fe80::']))
|
||||
if not tags:
|
||||
tags = []
|
||||
gateway_ip = netaddr.IPNetwork(cidr).network + 1
|
||||
start_ip = gateway_ip + 1
|
||||
end_ip = netaddr.IPNetwork(cidr).broadcast - 1
|
||||
|
@ -292,7 +298,8 @@ class TestKuryrBase(TestCase):
|
|||
"ip_version": 6,
|
||||
"cidr": cidr,
|
||||
"id": subnet_v6_id,
|
||||
"enable_dhcp": True
|
||||
"enable_dhcp": True,
|
||||
"tags": tags,
|
||||
}
|
||||
}
|
||||
if subnetpool_id:
|
||||
|
|
|
@ -104,6 +104,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
if driver_value:
|
||||
|
@ -231,6 +232,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
if driver_value:
|
||||
|
@ -370,6 +372,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
if driver_value:
|
||||
|
@ -535,7 +538,8 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"name": fake_neutron_net_name
|
||||
"name": fake_neutron_net_name,
|
||||
"tags": [],
|
||||
}]
|
||||
}
|
||||
tags = utils.create_net_tags(docker_network_id)
|
||||
|
@ -670,6 +674,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -813,6 +818,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
if driver_value:
|
||||
|
@ -979,6 +985,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
if driver_value:
|
||||
|
@ -1141,6 +1148,7 @@ class TestKuryr(base.TestKuryrBase):
|
|||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
if driver_value:
|
||||
|
|
|
@ -39,7 +39,8 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
|||
"router:external": False,
|
||||
"segments": [],
|
||||
"shared": False,
|
||||
"id": fake_neutron_net_id
|
||||
"id": fake_neutron_net_id,
|
||||
"tags": [],
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -247,6 +248,7 @@ class TestKuryrNetworkPreExisting(base.TestKuryrBase):
|
|||
docker_endpoint_id="fake_id",
|
||||
subnet_v4_id=subnet_v4_id,
|
||||
subnetpool_id=fake_kuryr_v4_subnetpool_id,
|
||||
tag_subnetpool_id=False,
|
||||
cidr=fake_cidr_v4)
|
||||
fake_v6_subnet = self._get_fake_v6_subnet(
|
||||
fake_neutron_net_id,
|
||||
|
|
Loading…
Reference in New Issue