Preserve assigned MAC on subsequent invocations

Tenks/libvirt was not reusing the MAC address assigned when invoked
repeatedly, which was leading to errors and confusion when extending
an existing Tenks deployment.

Check Ansible facts for a pre-existing interface and reuse the
assigned MAC if one is found.  Any existing MAC is added to the
data structure returned by set_libvirt_interfaces.

Change-Id: If45516d06d41992e7142ca8071ddcb84160a13e5
This commit is contained in:
Stig Telfer 2024-10-08 12:58:04 +00:00 committed by Will Szumski
parent eb33405ae4
commit b64f9adbc6
2 changed files with 24 additions and 5 deletions

View File

@ -68,6 +68,12 @@ def _get_hostvar(context, var_name, inventory_hostname=None):
return namespace.get(var_name)
def _get_fact(context, fact_name):
if fact_name not in context['ansible_facts']:
raise AnsibleFilterError("Fact '%s' not found" % fact_name)
return context['ansible_facts'][fact_name]
@pass_context
def set_libvirt_interfaces(context, node):
"""Set interfaces for a node's specified physical networks.
@ -76,11 +82,17 @@ def set_libvirt_interfaces(context, node):
for physnet in node.get('physical_networks', []):
# Use macvtap 'passthrough' mode, since this does not filter packets
# based on MAC address of the interface.
node['interfaces'].append(
{'type': 'direct',
'source': {'dev': source_link_name(context, node, physnet),
'mode': 'passthrough'}}
)
if_name = source_link_name(context, node, physnet)
if_data = {'type': 'direct',
'source': {'dev': if_name, 'mode': 'passthrough'}}
# Check for an existing interface. If found, pull in the existing MAC.
if_var = if_name.replace('-', '_')
if_fact = _get_fact(context, if_var)
if if_fact:
if_data['mac'] = if_fact['macaddress']
node['interfaces'].append(if_data)
return node

View File

@ -0,0 +1,7 @@
---
fixes:
- |
Fixes an issue where tenks was regenerating the MAC addresses for the
network interfaces used by the backing virtual machines on each run. This
would lead to a mismatch between the MAC address set on the baremetal port
in Ironic and the MAC address of the virtual machine's network interface.