From 6e8201f696aa0c5d4495cb981dfb28bef44a5912 Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Fri, 13 Aug 2021 08:26:18 +0200 Subject: [PATCH] Only configure one interface on MAAS provider The Zaza MAAS code selects interfaces that are attached to the provided CIDR, set up as unconfigured and has link. In the event a machine has multiple unconfigured interfaces attached to the same physical network, adding them all to the configuration might lead to undesired side effects such as network loops. --- unit_tests/__init__.py | 5 ---- .../test_zaza_utilities_openstack.py | 29 +++++++++++++++++++ zaza/openstack/utilities/openstack.py | 19 ++++++++---- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index 03c4879a7..8203d13e1 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -11,8 +11,3 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -import sys -import unittest.mock as mock - -sys.modules['zaza.utilities.maas'] = mock.MagicMock() diff --git a/unit_tests/utilities/test_zaza_utilities_openstack.py b/unit_tests/utilities/test_zaza_utilities_openstack.py index 0434a2be8..2dd3fcdbc 100644 --- a/unit_tests/utilities/test_zaza_utilities_openstack.py +++ b/unit_tests/utilities/test_zaza_utilities_openstack.py @@ -24,6 +24,7 @@ import unit_tests.utils as ut_utils from zaza.openstack.utilities import openstack as openstack_utils from zaza.openstack.utilities import exceptions +from zaza.utilities.maas import LinkMode, MachineInterfaceMac class TestOpenStackUtils(ut_utils.BaseTestCase): @@ -1428,6 +1429,34 @@ def test_get_remote_ca_cert_file(self): self.move.assert_called_once_with( 'tempfilename', '/tmp/default/ca1.cert') + def test_configure_charmed_openstack_on_maas(self): + self.patch_object(openstack_utils, 'get_charm_networking_data') + self.patch_object(openstack_utils.zaza.utilities.maas, + 'get_macs_from_cidr') + self.patch_object(openstack_utils.zaza.utilities.maas, + 'get_maas_client_from_juju_cloud_data') + self.patch_object(openstack_utils.zaza.model, 'get_cloud_data') + self.patch_object(openstack_utils, 'configure_networking_charms') + self.get_charm_networking_data.return_value = 'fakenetworkingdata' + self.get_macs_from_cidr.return_value = [ + MachineInterfaceMac('id_a', 'ens6', '00:53:00:00:00:01', + '192.0.2.0/24', LinkMode.LINK_UP), + MachineInterfaceMac('id_a', 'ens7', '00:53:00:00:00:02', + '192.0.2.0/24', LinkMode.LINK_UP), + MachineInterfaceMac('id_b', 'ens6', '00:53:00:00:01:01', + '192.0.2.0/24', LinkMode.LINK_UP), + + ] + network_config = {'external_net_cidr': '192.0.2.0/24'} + expect = [ + '00:53:00:00:00:01', + '00:53:00:00:01:01', + ] + openstack_utils.configure_charmed_openstack_on_maas( + network_config) + self.configure_networking_charms.assert_called_once_with( + 'fakenetworkingdata', expect, use_juju_wait=False) + class TestAsyncOpenstackUtils(ut_utils.AioTestCase): diff --git a/zaza/openstack/utilities/openstack.py b/zaza/openstack/utilities/openstack.py index a5ba6f623..fc0d91451 100644 --- a/zaza/openstack/utilities/openstack.py +++ b/zaza/openstack/utilities/openstack.py @@ -1056,14 +1056,23 @@ def configure_charmed_openstack_on_maas(network_config, limit_gws=None): :type limit_gws: Optional[int] """ networking_data = get_charm_networking_data(limit_gws=limit_gws) - macs = [ - mim.mac - for mim in zaza.utilities.maas.get_macs_from_cidr( + macs = [] + machines = set() + for mim in zaza.utilities.maas.get_macs_from_cidr( zaza.utilities.maas.get_maas_client_from_juju_cloud_data( zaza.model.get_cloud_data()), network_config['external_net_cidr'], - link_mode=zaza.utilities.maas.LinkMode.LINK_UP) - ] + link_mode=zaza.utilities.maas.LinkMode.LINK_UP): + if mim.machine_id in machines: + logging.warning("Machine {} has multiple unconfigured interfaces, " + "ignoring interface {} ({})." + .format(mim.machine_id, mim.ifname, mim.mac)) + continue + logging.info("Machine {} selected {} ({}) for external networking." + .format(mim.machine_id, mim.ifname, mim.mac)) + machines.add(mim.machine_id) + macs.append(mim.mac) + if macs: configure_networking_charms( networking_data, macs, use_juju_wait=False)