Cache subnets when building nics_to_port_map

The subnet is very likely the same for most ports,
keep a cache of subnets in a dict to reduce the
amount of network.get_subnet() calls.

Change-Id: Iac735552835c9dc2f65ea02a33ce0adb359ef838
This commit is contained in:
Harald Jensås 2023-02-08 15:20:58 +01:00
parent 40efd662ff
commit eb69b1b4f1
1 changed files with 7 additions and 1 deletions

View File

@ -639,13 +639,19 @@ def get_source(instance):
checksum=image.get('checksum'))
_subnets_cache = dict()
def nics_to_port_map(nics, connection):
"""Build a port map from a metalsmith instance."""
port_map = {}
for nic in nics:
for ip in nic.fixed_ips:
net_name = getattr(nic.network, 'name', None) or nic.network.id
subnet = connection.network.get_subnet(ip['subnet_id'])
if not ip['subnet_id'] in _subnets_cache:
_subnets_cache[ip['subnet_id']] = (
connection.network.get_subnet(ip['subnet_id']))
subnet = _subnets_cache[ip['subnet_id']]
net_info = port_map.setdefault(
net_name, {'network': nic.network.to_dict(),
'fixed_ips': [], 'subnets': []})