diff options
author | James Falcon <james.falcon@canonical.com> | 2022-11-10 15:39:44 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 15:39:44 -0600 |
commit | 60e2b2845b7a28a3bfecf42901380faedcfc751c (patch) | |
tree | 812556648434880af400d389432d56dc6b35a6c3 | |
parent | e0076e0d2e993ac8b7297ddc99e0187eb74cf152 (diff) | |
download | cloud-init-git-60e2b2845b7a28a3bfecf42901380faedcfc751c.tar.gz |
Stop using devices endpoint for LXD network config (#1819)
When using the LXD datasource, we switched to using the "devices"
endpoint to configure network config. On LXD VMs, this endpoint can
contain incorrect device names, so instead switch to setting up the
fallback NIC until the devices endpoint can surface MAC addresses.
-rw-r--r-- | cloudinit/sources/DataSourceLXD.py | 31 | ||||
-rw-r--r-- | tests/integration_tests/datasources/test_lxd_hotplug.py | 3 | ||||
-rw-r--r-- | tests/unittests/sources/test_lxd.py | 20 |
3 files changed, 39 insertions, 15 deletions
diff --git a/cloudinit/sources/DataSourceLXD.py b/cloudinit/sources/DataSourceLXD.py index d873cd3d..49a7567c 100644 --- a/cloudinit/sources/DataSourceLXD.py +++ b/cloudinit/sources/DataSourceLXD.py @@ -73,13 +73,27 @@ def generate_network_config( nics: Optional[List[str]] = None, ) -> Dict[str, Any]: """Return network config V1 dict representing instance network config.""" - if not nics: - primary_nic = _get_fallback_interface_name() - elif len(nics) > 1: - fallback_nic = find_fallback_nic() - primary_nic = nics[0] if fallback_nic not in nics else fallback_nic + # TODO: The original intent of this function was to use the nics retrieved + # from LXD's devices endpoint to determine the primary nic and write + # that out to network config. However, for LXD VMs, the device name + # may differ from the interface name in the VM, so we'll instead rely + # on our fallback nic code. Once LXD's devices endpoint grows the + # ability to provide a MAC address, we should rely on that information + # rather than just the glorified guessing that we're doing here. + primary_nic = find_fallback_nic() + if primary_nic: + LOG.debug( + "LXD datasource generating network from discovered active" + " device: %s", + primary_nic, + ) else: - primary_nic = nics[0] + primary_nic = _get_fallback_interface_name() + LOG.debug( + "LXD datasource generating network from systemd-detect-virt" + " platform default device: %s", + primary_nic, + ) return { "version": 1, @@ -241,11 +255,6 @@ class DataSourceLXD(sources.DataSource): for k, v in self._crawled_metadata["devices"].items() if v["type"] == "nic" ] - LOG.debug( - "LXD datasource generating network config using " - "devices: %s", - ", ".join(devices), - ) self._network_config = generate_network_config(devices) if self._network_config == sources.UNSET: # We know nothing about network, so setup fallback diff --git a/tests/integration_tests/datasources/test_lxd_hotplug.py b/tests/integration_tests/datasources/test_lxd_hotplug.py index 0768b902..8c403e04 100644 --- a/tests/integration_tests/datasources/test_lxd_hotplug.py +++ b/tests/integration_tests/datasources/test_lxd_hotplug.py @@ -56,8 +56,9 @@ def _prefer_lxd_datasource_over_nocloud(client: IntegrationInstance): client.restart() +# TODO: Once LXD adds MACs to the devices endpoint, support LXD VMs here +# Currently the names are too unpredictable to be worth testing on VMs. @pytest.mark.lxd_container -@pytest.mark.lxd_vm @pytest.mark.user_data(USER_DATA) class TestLxdHotplug: @pytest.fixture(autouse=True, scope="class") diff --git a/tests/unittests/sources/test_lxd.py b/tests/unittests/sources/test_lxd.py index 96bd37a0..b02ed177 100644 --- a/tests/unittests/sources/test_lxd.py +++ b/tests/unittests/sources/test_lxd.py @@ -153,8 +153,10 @@ class TestGenerateFallbackNetworkConfig: @mock.patch(DS_PATH + "util.system_info") @mock.patch(DS_PATH + "subp.subp") @mock.patch(DS_PATH + "subp.which") + @mock.patch(DS_PATH + "find_fallback_nic") def test_net_v2_based_on_network_mode_virt_type_and_uname_machine( self, + m_fallback, m_which, m_subp, m_system_info, @@ -163,6 +165,7 @@ class TestGenerateFallbackNetworkConfig: expected, ): """Return network config v2 based on uname -m, systemd-detect-virt.""" + m_fallback.return_value = None if systemd_detect_virt is None: m_which.return_value = None m_system_info.return_value = {"uname": ["", "", "", "", uname_machine]} @@ -212,7 +215,7 @@ class TestNetworkConfig: "version": 1, "config": [ { - "name": "enp1s0", + "name": "eth0", "subnets": [{"control": "auto", "type": "dhcp"}], "type": "physical", } @@ -227,7 +230,7 @@ class TestNetworkConfig: "version": 1, "config": [ { - "name": "enp1s1", + "name": "eth0", "subnets": [{"control": "auto", "type": "dhcp"}], "type": "physical", } @@ -242,7 +245,7 @@ class TestNetworkConfig: "version": 1, "config": [ { - "name": "enp1s0", + "name": "eth0", "subnets": [{"control": "auto", "type": "dhcp"}], "type": "physical", } @@ -270,6 +273,16 @@ class TestNetworkConfig: def test_provided_devices( self, devices_to_remove, expected_config, lxd_ds, mocker ): + # TODO: The original point of these tests was to ensure that when + # presented nics by the LXD devices endpoint, that we setup the correct + # device accordingly. Once LXD provides us MAC addresses for these + # devices, we can continue this functionality, but these tests have + # been modified to ensure that regardless of the number of devices + # present, we generate the proper fallback + m_fallback = mocker.patch( + "cloudinit.sources.DataSourceLXD.find_fallback_nic", + return_value=None, + ) devices = copy.deepcopy(DEVICES) for name in devices_to_remove: del devices["devices"][name] @@ -284,6 +297,7 @@ class TestNetworkConfig: side_effect=_get_data, ) assert lxd_ds.network_config == expected_config + assert m_fallback.call_count == 1 class TestDataSourceLXD: |