image-builder: [Ubuntu] Cloud-init failing with KeyError: id0

What steps did you take and what happened: with new ubuntu builds i am seeing cloud-init failing to assign networking with KeyError: id0

[2021-10-06 17:03:44] Cloud-init v. 21.3-1-g6803368d-0ubuntu1~20.04.3 running 'init-local' at Wed, 06 Oct 2021 17:03:44 +0000. Up 2.82 seconds.
[2021-10-06 17:03:44] 2021-10-06 17:03:44,289 - util.py[WARNING]: failed stage init-local
[2021-10-06 17:03:44] failed run of stage init-local
[2021-10-06 17:03:44] ------------------------------------------------------------
[2021-10-06 17:03:44] Traceback (most recent call last):
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 682, in status_wrapper
[2021-10-06 17:03:44]     ret = functor(name, args)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 391, in main_init
[2021-10-06 17:03:44]     init.apply_network_config(bring_up=bool(mode != sources.DSMODE_LOCAL))
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/stages.py", line 824, in apply_network_config
[2021-10-06 17:03:44]     return self.distro.apply_network_config(
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/distros/__init__.py", line 220, in apply_network_config
[2021-10-06 17:03:44]     network_state = parse_net_config_data(netconfig)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/net/network_state.py", line 1074, in parse_net_config_data
[2021-10-06 17:03:44]     nsi.parse_config(skip_broken=skip_broken)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/net/network_state.py", line 261, in parse_config
[2021-10-06 17:03:44]     self.parse_config_v2(skip_broken=skip_broken)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/net/network_state.py", line 310, in parse_config_v2
[2021-10-06 17:03:44]     self._v2_common(command)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/net/network_state.py", line 722, in _v2_common
[2021-10-06 17:03:44]     self._handle_individual_nameserver(name_cmd, iface)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/net/network_state.py", line 91, in decorator
[2021-10-06 17:03:44]     return func(self, command, *args, **kwargs)
[2021-10-06 17:03:44]   File "/usr/lib/python3/dist-packages/cloudinit/net/network_state.py", line 546, in _handle_individual_nameserver
[2021-10-06 17:03:44]     _iface[iface]['dns'] = {'nameservers': nameservers, 'search': search}
[2021-10-06 17:03:44] KeyError: 'id0'
[2021-10-06 17:03:44] ------------------------------------------------------------
[2021-10-06 17:03:44] Cloud-init v. 21.3-1-g6803368d-0ubuntu1~20.04.3 running 'init' at Wed, 06 Oct 2021 17:03:44 +0000. Up 3.19 seconds.

metadata:

instance-id: "wlan-1-md-0-775d8846bf-9bfrd"
local-hostname: "wlan-1-md-0-775d8846bf-9bfrd"
wait-on-network:
  ipv4: false
  ipv6: false
network:
  version: 2
  ethernets:
    id0:
      match:
        macaddress: "00:50:56:a1:d8:a7"
      set-name: "eth0"
      wakeonlan: true
      addresses:
      - "10.196.27.122/28"
      gateway4: "10.196.27.126"
      nameservers:
        addresses:
        - "10.102.102.132"
        - "10.102.102.133"
        - "10.90.24.1"
        search:
        - "refsa1.bn.schiff.telekom.de"

What did you expect to happen: Cloud init is working properly

Environment:

Project: CAPI

Additional info for Image Builder for Cluster API related issues:

  • OS (e.g. from /etc/os-release, or cmd /c ver): Ubuntu 2004

/kind bug [One or more /area label. See https://github.com/kubernetes-sigs/cluster-api/labels?q=area for the list of labels]

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 36 (30 by maintainers)

Commits related to this issue

Most upvoted comments

Cloud-Init engineers have merged the fix (https://github.com/canonical/cloud-init/pull/1058). I am unclear when it will make its way into the update channel.

I fixed the issue introduced in https://github.com/canonical/cloud-init/commit/abd2da5777195e7e432b0d53a3f7f29d071dd50e with the following patch:

diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
index 95b064f0..4862bf91 100644
--- a/cloudinit/net/network_state.py
+++ b/cloudinit/net/network_state.py
@@ -710,6 +710,10 @@ class NetworkStateInterpreter(metaclass=CommandHandlerMeta):
     def _v2_common(self, cfg):
         LOG.debug('v2_common: handling config:\n%s', cfg)
         for iface, dev_cfg in cfg.items():
+            if 'set-name' in dev_cfg:
+                set_name_iface = dev_cfg.get('set-name')
+                if set_name_iface:
+                    iface = set_name_iface
             if 'nameservers' in dev_cfg:
                 search = dev_cfg.get('nameservers').get('search', [])
                 dns = dev_cfg.get('nameservers').get('addresses', [])
diff --git a/cloudinit/net/tests/test_network_state.py b/cloudinit/net/tests/test_network_state.py
index 84e8308a..45e99171 100644
--- a/cloudinit/net/tests/test_network_state.py
+++ b/cloudinit/net/tests/test_network_state.py
@@ -52,6 +52,7 @@ network:
     eth1:
       match:
         macaddress: '66:77:88:99:00:11'
+      set-name: "ens92"
       nameservers:
         search: [foo.local, bar.local]
         addresses: [4.4.4.4]

The tests now run fine:

$ make clean_pyc && PYTHONPATH="$(pwd)" python3 -m pytest -v cloudinit/net/tests/test_network_state.py
====================================================================== test session starts ======================================================================
platform darwin -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /usr/local/opt/python@3.9/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/akutz/Projects/cloud-init, configfile: tox.ini
collected 10 items                                                                                                                                              

cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfig::test_empty_v1_config_gets_network_state PASSED                                    [ 10%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfig::test_empty_v2_config_gets_network_state PASSED                                    [ 20%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfig::test_missing_version_returns_none PASSED                                          [ 30%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfig::test_unknown_versions_returns_none PASSED                                         [ 40%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfig::test_valid_config_gets_network_state PASSED                                       [ 50%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfig::test_version_2_passes_self_as_config PASSED                                       [ 60%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseConfigV2::test_version_2_ignores_renderer_key PASSED                                      [ 70%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseNameservers::test_v1_nameservers_valid PASSED                                             [ 80%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseNameservers::test_v1_nameservers_invalid PASSED                                           [ 90%]
cloudinit/net/tests/test_network_state.py::TestNetworkStateParseNameservers::test_v2_nameservers PASSED                                                   [100%]

======================================================================= warnings summary ========================================================================
../../../../usr/local/lib/python3.9/site-packages/_pytest/config/__init__.py:1183
  /usr/local/lib/python3.9/site-packages/_pytest/config/__init__.py:1183: PytestDeprecationWarning: The --strict option is deprecated, use --strict-markers instead.
    self.issue_config_time_warning(

conftest.py:68
  /Users/akutz/Projects/cloud-init/conftest.py:68: PytestDeprecationWarning: @pytest.yield_fixture is deprecated.
  Use @pytest.fixture instead; they are the same.
    @pytest.yield_fixture(autouse=True)

conftest.py:169
  /Users/akutz/Projects/cloud-init/conftest.py:169: PytestDeprecationWarning: @pytest.yield_fixture is deprecated.
  Use @pytest.fixture instead; they are the same.
    def httpretty():

-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================================================ 10 passed, 3 warnings in 0.41s =================================================================

I will file a bug in Launchpad tomorrow and submit a PR to Cloud-Init with the fix.