Browse Source

Merge "Remove tripleo_hostname tag"

changes/60/772360/11
Zuul 2 weeks ago
committed by Gerrit Code Review
parent
commit
e8fd3b3d95
2 changed files with 76 additions and 43 deletions
  1. +21
    -13
      tripleo_ansible/ansible_plugins/modules/tripleo_overcloud_network_ports.py
  2. +55
    -30
      tripleo_ansible/tests/modules/test_tripleo_overcloud_network_ports.py

+ 21
- 13
tripleo_ansible/ansible_plugins/modules/tripleo_overcloud_network_ports.py View File

@ -333,7 +333,8 @@ def generate_port_defs(net_maps, instance, inst_ports):
port_name = '_'.join([hostname, net_name])
port_def = dict(name=port_name, network_id=net_id, fixed_ips=fixed_ips)
port_def = dict(name=port_name, dns_name=hostname, network_id=net_id,
fixed_ips=fixed_ips)
if port_name not in existing_port_names:
create_port_defs.append(port_def)
@ -365,12 +366,14 @@ def _provision_ports(result, conn, stack, instance, net_maps, ports_by_node,
ironic_uuid, role):
hostname = instance['hostname']
tags = ['tripleo_stack_name={}'.format(stack),
'tripleo_hostname={}'.format(hostname),
'tripleo_role={}'.format(role)]
# TODO(hjensas): This can be moved below the ironic_uuid condition in
# later release when all upgraded deployments has had the
# tripleo_ironic_uuid tag added
inst_ports = list(conn.network.ports(tags=tags))
inst_ports = conn.network.ports(tags=tags)
# NOTE(hjensas): 'dns_name' is not a valid attribute for filtering, so we
# have to do it manually.
inst_ports = [port for port in inst_ports if port.dns_name == hostname]
if ironic_uuid:
tags.append('tripleo_ironic_uuid={}'.format(ironic_uuid))
@ -393,18 +396,20 @@ def _provision_ports(result, conn, stack, instance, net_maps, ports_by_node,
def _unprovision_ports(result, conn, stack, instance, ironic_uuid):
hostname = instance['hostname']
tags = ['tripleo_stack_name={}'.format(stack),
'tripleo_hostname={}'.format(hostname)]
tags = ['tripleo_stack_name={}'.format(stack)]
if ironic_uuid:
tags.append('tripleo_ironic_uuid={}'.format(ironic_uuid))
inst_ports = list(conn.network.ports(tags=tags))
inst_ports = conn.network.ports(tags=tags)
# NOTE(hjensas): 'dns_name' is not a valid attribute for filtering, so we
# have to do it manually.
inst_ports = [port for port in inst_ports if port.dns_name == hostname]
# TODO(hjensas): This can be removed in later release when all upgraded
# deployments has had the tripleo_ironic_uuid tag added.
if not inst_ports:
tags = ['tripleo_stack_name={}'.format(stack),
'tripleo_hostname={}'.format(hostname)]
inst_ports = list(conn.network.ports(tags=tags))
tags = ['tripleo_stack_name={}'.format(stack)]
inst_ports = conn.network.ports(tags=tags)
inst_ports = [port for port in inst_ports if port.dns_name == hostname]
if inst_ports:
delete_ports(conn, inst_ports)
@ -492,7 +497,8 @@ def manage_instances_ports(result, conn, stack, instances, concurrency, state,
generate_node_port_map(result, net_maps, ports_by_node)
def _tag_metalsmith_instance_ports(result, conn, provisioner, uuid, tags):
def _tag_metalsmith_instance_ports(result, conn, provisioner, uuid, hostname,
tags):
instance = provisioner.show_instance(uuid)
for nic in instance.nics():
@ -501,6 +507,9 @@ def _tag_metalsmith_instance_ports(result, conn, provisioner, uuid, tags):
nic_tags.update(tags)
conn.network.set_tags(nic, list(nic_tags))
result['changed'] = True
if not nic.dns_name == hostname:
conn.network.update_port(nic, dns_name=hostname)
result['changed'] = True
def tag_metalsmith_managed_ports(result, conn, concurrency, stack,
@ -516,14 +525,13 @@ def tag_metalsmith_managed_ports(result, conn, concurrency, stack,
with futures.ThreadPoolExecutor(max_workers=concurrency) as p:
for hostname, uuid in uuid_by_hostname.items():
role = hostname_role_map[hostname]
tags = {'tripleo_hostname={}'.format(hostname),
'tripleo_stack_name={}'.format(stack),
tags = {'tripleo_stack_name={}'.format(stack),
'tripleo_ironic_uuid={}'.format(uuid),
'tripleo_role={}'.format(role),
'tripleo_ironic_vif_port=true'}
provision_jobs.append(
p.submit(_tag_metalsmith_instance_ports,
result, conn, provisioner, uuid, tags)
result, conn, provisioner, uuid, hostname, tags)
)
for job in futures.as_completed(provision_jobs):


+ 55
- 30
tripleo_ansible/tests/modules/test_tripleo_overcloud_network_ports.py View File

@ -146,8 +146,7 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
def test_pre_provisioned_ports(self, mock_conn):
result = {'changed': False}
inst_ports = []
tags = set(['tripleo_hostname=instance0',
'tripleo_stack_name=overcloud',
tags = set(['tripleo_stack_name=overcloud',
'tripleo_ironic_uuid=ironic_uuid'])
fake_instance = copy.deepcopy(FAKE_INSTANCE)
fake_instance['networks'] = [{'network': 'foo', 'port': 'some_port'}]
@ -173,9 +172,11 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
FAKE_MAPS, FAKE_INSTANCE, inst_ports)
self.assertEqual([
{'name': 'instance0_foo',
'dns_name': 'instance0',
'network_id': 'foo_id',
'fixed_ips': [{'subnet_id': 'foo_subnet_id'}]},
{'name': 'instance0_bar',
'dns_name': 'instance0',
'network_id': 'bar_id',
'fixed_ips': [{'subnet_id': 'bar_subnet_id'}]},
], create_port_defs)
@ -194,9 +195,11 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
self.assertEqual([], create_port_defs)
self.assertEqual([
{'name': 'instance0_foo',
'dns_name': 'instance0',
'network_id': 'foo_id',
'fixed_ips': [{'subnet_id': 'foo_subnet_id'}]},
{'name': 'instance0_bar',
'dns_name': 'instance0',
'network_id': 'bar_id',
'fixed_ips': [{'subnet_id': 'bar_subnet_id'}]}
], update_port_defs)
@ -210,11 +213,13 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
FAKE_MAPS, FAKE_INSTANCE, inst_ports)
self.assertEqual([
{'name': 'instance0_bar',
'dns_name': 'instance0',
'network_id': 'bar_id',
'fixed_ips': [{'subnet_id': 'bar_subnet_id'}]},
], create_port_defs)
self.assertEqual([
{'name': 'instance0_foo',
'dns_name': 'instance0',
'network_id': 'foo_id',
'fixed_ips': [{'subnet_id': 'foo_subnet_id'}]},
], update_port_defs)
@ -228,9 +233,11 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
FAKE_MAPS, instance, inst_ports)
self.assertEqual([
{'name': 'instance0_foo',
'dns_name': 'instance0',
'network_id': 'foo_id',
'fixed_ips': [{'subnet_id': 'foo_subnet_id'}]},
{'name': 'instance0_bar',
'dns_name': 'instance0',
'network_id': 'bar_id',
'fixed_ips': [{'subnet_id': 'bar_subnet_id'}]},
], create_port_defs)
@ -264,9 +271,11 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
maps, instance, inst_ports)
self.assertEqual([
{'name': 'instance0_foo',
'dns_name': 'instance0',
'network_id': 'foo_id',
'fixed_ips': [{'ip_address': 'baz_fixed_ip'}]},
{'name': 'instance0_bar',
'dns_name': 'instance0',
'network_id': 'bar_id',
'fixed_ips': [{'ip_address': 'bar_fixed_ip'}]},
], create_port_defs)
@ -304,8 +313,7 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
def test_create_ports(self, mock_conn):
result = {'changed': False}
inst_ports = []
tags = set(['tripleo_hostname=instance0',
'tripleo_stack_name=overcloud',
tags = set(['tripleo_stack_name=overcloud',
'tripleo_ironic_uuid=ironic_uuid'])
port_foo = stubs.FakeNeutronPort(
name='instance0_foo', network_id='foo_id',
@ -351,14 +359,17 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
def test__provision_ports_create(self, mock_conn, mock_pre_provisioned,
mock_create_ports, mock_update_ports):
create_port_defs = [
dict(name='instance0_foo', network_id='foo_id',
dict(name='instance0_foo',
dns_name='instance0',
network_id='foo_id',
fixed_ips=[{'subnet_id': 'foo_subnet_id'}]),
dict(name='instance0_bar', network_id='bar_id',
dict(name='instance0_bar',
dns_name='instance0',
network_id='bar_id',
fixed_ips=[{'subnet_id': 'bar_subnet_id'}]),
]
mock_conn.network.ports.return_value = self.a2g([])
expected_tags = {'tripleo_hostname=instance0',
'tripleo_ironic_uuid=ironic_uuid',
expected_tags = {'tripleo_ironic_uuid=ironic_uuid',
'tripleo_role=role',
'tripleo_stack_name=overcloud'}
plugin._provision_ports({}, mock_conn, STACK, FAKE_INSTANCE,
@ -378,21 +389,28 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
def test__provision_ports_update(self, mock_conn, mock_pre_provisioned,
mock_create_ports, mock_update_ports):
port_foo = stubs.FakeNeutronPort(
name='instance0_foo', network_id='foo_id',
name='instance0_foo',
dns_name='instance0',
network_id='foo_id',
fixed_ips=[{'subnet_id': 'foo_subnet_id'}],
tags=[])
port_bar = stubs.FakeNeutronPort(
name='instance0_bar', network_id='bar_id',
name='instance0_bar',
dns_name='instance0',
network_id='bar_id',
fixed_ips=[{'subnet_id': 'bar_subnet_id'}],
tags=[])
update_port_defs = [
dict(name='instance0_foo', network_id='foo_id',
dict(name='instance0_foo',
dns_name='instance0',
network_id='foo_id',
fixed_ips=[{'subnet_id': 'foo_subnet_id'}]),
dict(name='instance0_bar', network_id='bar_id',
dict(name='instance0_bar',
dns_name='instance0',
network_id='bar_id',
fixed_ips=[{'subnet_id': 'bar_subnet_id'}]),
]
expected_tags = {'tripleo_hostname=instance0',
'tripleo_ironic_uuid=ironic_uuid',
expected_tags = {'tripleo_ironic_uuid=ironic_uuid',
'tripleo_role=role',
'tripleo_stack_name=overcloud'}
mock_conn.network.ports.return_value = self.a2g([port_foo, port_bar])
@ -416,20 +434,25 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
mock_create_ports,
mock_update_ports):
port_foo = stubs.FakeNeutronPort(
name='instance0_foo', network_id='foo_id',
name='instance0_foo',
dns_name='instance0',
network_id='foo_id',
fixed_ips=[{'subnet_id': 'foo_subnet_id'}],
tags=[])
create_port_defs = [
dict(name='instance0_bar', network_id='bar_id',
dict(name='instance0_bar',
dns_name='instance0',
network_id='bar_id',
fixed_ips=[{'subnet_id': 'bar_subnet_id'}]),
]
update_port_defs = [
dict(name='instance0_foo', network_id='foo_id',
dict(name='instance0_foo',
dns_name='instance0',
network_id='foo_id',
fixed_ips=[{'subnet_id': 'foo_subnet_id'}]),
]
mock_conn.network.ports.return_value = self.a2g([port_foo])
expected_tags = {'tripleo_hostname=instance0',
'tripleo_ironic_uuid=ironic_uuid',
expected_tags = {'tripleo_ironic_uuid=ironic_uuid',
'tripleo_role=role',
'tripleo_stack_name=overcloud'}
plugin._provision_ports({}, mock_conn, STACK, FAKE_INSTANCE,
@ -449,10 +472,14 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
def test__unprovision_ports(self, mock_conn, mock_delete_ports):
result = {'changed': False, 'instance_port_map': {}}
port_foo = stubs.FakeNeutronPort(
name='instance_foo', network_id='foo_id',
name='instance_foo',
dns_name='instance0',
network_id='foo_id',
fixed_ips=[{'subnet_id': 'foo_subnet_id'}])
port_bar = stubs.FakeNeutronPort(
name='instance_bar', network_id='bar_id',
name='instance_bar',
dns_name='instance0',
network_id='bar_id',
fixed_ips=[{'subnet_id': 'bar_subnet_id'}])
mock_conn.network.ports.return_value = self.a2g([port_foo, port_bar])
plugin._unprovision_ports(result, mock_conn, STACK, FAKE_INSTANCE,
@ -510,8 +537,7 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
@mock.patch.object(metalsmith, 'Provisioner', autospec=True)
def test__tag_metalsmith_instance_ports(self, mock_provisioner, mock_conn):
result = {'changed': False}
tags = {'tripleo_hostname=hostname',
'tripleo_stack_name={}'.format(STACK),
tags = {'tripleo_stack_name={}'.format(STACK),
'tripleo_ironic_uuid=ironic_uuid',
'tripleo_role=role',
'tripleo_ironic_vif_port=true'}
@ -523,7 +549,7 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
mock_provisioner.show_instance.return_value = fake_instance
plugin._tag_metalsmith_instance_ports(result, mock_conn,
mock_provisioner, 'ironic_uuid',
tags)
'hostname', tags)
mock_conn.network.set_tags.assert_called_with(fake_nic, mock.ANY)
set_tags_args = mock_conn.network.set_tags.call_args.args
self.assertTrue(set(tags) == set(set_tags_args[1]))
@ -535,15 +561,13 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
def test__tag_metalsmith_instance_ports_tags_already_set(
self, mock_provisioner, mock_conn):
result = {'changed': False}
tags = {'tripleo_hostname=hostname',
'tripleo_stack_name={}'.format(STACK),
tags = {'tripleo_stack_name={}'.format(STACK),
'tripleo_ironic_uuid=ironic_uuid',
'tripleo_role=role',
'tripleo_ironic_vif_port=true'}
fake_nic = stubs.FakeNeutronPort(
name='hostname-ctlplane', id='port_uuid',
tags=['tripleo_hostname=hostname',
'tripleo_stack_name={}'.format(STACK),
name='hostname-ctlplane', dns_name='hostname', id='port_uuid',
tags=['tripleo_stack_name={}'.format(STACK),
'tripleo_ironic_uuid=ironic_uuid',
'tripleo_role=role',
'tripleo_ironic_vif_port=true'])
@ -552,7 +576,8 @@ class TestTripleoOvercloudNetworkPorts(tests_base.TestCase):
mock_provisioner.show_instance.return_value = fake_instance
plugin._tag_metalsmith_instance_ports(result, mock_conn,
mock_provisioner, 'ironic_uuid',
tags)
'hostname', tags)
mock_conn.network.update_port.assert_not_called()
mock_conn.network.set_tags.assert_not_called()
self.assertFalse(result['changed'])

Loading…
Cancel
Save