From e2ab7512acf9545c80a6a66194e0e2c234f1a368 Mon Sep 17 00:00:00 2001 From: Ilia Baikov Date: Wed, 18 Nov 2026 04:02:21 +0300 Subject: [PATCH] windows: retry finding the IP interface before setting DHCP NetworkConfigPlugin creates a team NIC for a VLAN link and sets its static address right away. Windows creates the new adapter's IP interface a moment later, so _fix_network_adapter_dhcp can find no MSFT_NetIPInterface and the plugin fails, leaving the adapter without its address: Creating bond network adapter "net1.1010" on team "bond_net1-team" with VLAN: 1010 plugin 'NetworkConfigPlugin' failed with error 'Network interface with name "net1.1010" not found' Seen on Windows Server 2025 with a VLAN on a one-card LBFO team, half a second after the team NIC was created; a run with 2.3 seconds in between worked with the same names. Retry the lookup on ItemNotFoundException, as set_network_adapter_mtu already does, for up to about 40 seconds. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Ilia Baikov --- cloudbaseinit/osutils/windows.py | 5 +++++ cloudbaseinit/tests/osutils/test_windows.py | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index f406ec69..af89e94a 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -938,7 +938,12 @@ def _set_static_network_config_legacy(name, address, netmask, gateway, return reboot_required + # A network adapter created just before, like a team NIC, gets its IP + # interface a moment later. @staticmethod + @retry_decorator.retry_decorator( + max_retry_count=10, max_sleep_time=5, + exceptions=exception.ItemNotFoundException) def _fix_network_adapter_dhcp(interface_name, enable_dhcp, address_family): diff --git a/cloudbaseinit/tests/osutils/test_windows.py b/cloudbaseinit/tests/osutils/test_windows.py index 9b64890f..9359cc0e 100644 --- a/cloudbaseinit/tests/osutils/test_windows.py +++ b/cloudbaseinit/tests/osutils/test_windows.py @@ -2022,7 +2022,12 @@ def test_fix_network_adapter_dhcp(self): def test_fix_network_adapter_dhcp_no_network_adapter(self): self._test_fix_network_adapter_dhcp(False) - def _test_fix_network_adapter_dhcp(self, no_net_interface_found): + def test_fix_network_adapter_dhcp_network_adapter_late(self): + self._test_fix_network_adapter_dhcp(False, found_after=2) + + @mock.patch('time.sleep') + def _test_fix_network_adapter_dhcp(self, no_net_interface_found, + mock_sleep, found_after=0): mock_interface_name = "eth12" mock_enable_dhcp = True mock_address_family = self.windows_utils.AF_INET @@ -2031,22 +2036,30 @@ def _test_fix_network_adapter_dhcp(self, no_net_interface_found): existing_net_interface = mock.Mock() existing_net_interface.Dhcp = 0 - if not no_net_interface_found: + if found_after: + conn.MSFT_NetIPInterface.side_effect = ( + [[]] * found_after + [[existing_net_interface]]) + elif not no_net_interface_found: conn.MSFT_NetIPInterface.return_value = [existing_net_interface] - if no_net_interface_found: + if no_net_interface_found and not found_after: with self.assertRaises(exception.ItemNotFoundException): self._winutils._fix_network_adapter_dhcp( mock_interface_name, mock_enable_dhcp, mock_address_family) + self.assertEqual(11, conn.MSFT_NetIPInterface.call_count) + self.assertEqual(10, mock_sleep.call_count) else: self._winutils._fix_network_adapter_dhcp( mock_interface_name, mock_enable_dhcp, mock_address_family) - conn.MSFT_NetIPInterface.assert_called_once_with( + conn.MSFT_NetIPInterface.assert_called_with( InterfaceAlias=mock_interface_name, AddressFamily=mock_address_family) + self.assertEqual(found_after + 1, + conn.MSFT_NetIPInterface.call_count) + self.assertEqual(found_after, mock_sleep.call_count) self.assertEqual(existing_net_interface.Dhcp, 1) existing_net_interface.put.assert_called_once()