Skip to content

Commit fff6de4

Browse files
authored
Fix for set-name bug in networkd renderer (canonical#1100)
This patch address an issue where the use of the "set-name" directive caused the networkd renderer to fail. LP: #1949407
1 parent 3d15068 commit fff6de4

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

cloudinit/net/networkd.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def update_section(self, sec, key, val):
4141

4242
def get_final_conf(self):
4343
contents = ''
44-
for k, v in self.conf_dict.items():
44+
for k, v in sorted(self.conf_dict.items()):
4545
if not v:
4646
continue
4747
contents += '['+k+']\n'
48-
for e in v:
48+
for e in sorted(v):
4949
contents += e + '\n'
5050
contents += '\n'
5151

@@ -242,6 +242,19 @@ def _render_content(self, ns):
242242
name = iface['name']
243243
# network state doesn't give dhcp domain info
244244
# using ns.config as a workaround here
245+
246+
# Check to see if this interface matches against an interface
247+
# from the network state that specified a set-name directive.
248+
# If there is a device with a set-name directive and it has
249+
# set-name value that matches the current name, then update the
250+
# current name to the device's name. That will be the value in
251+
# the ns.config['ethernets'] dict below.
252+
for dev_name, dev_cfg in ns.config['ethernets'].items():
253+
if 'set-name' in dev_cfg:
254+
if dev_cfg.get('set-name') == name:
255+
name = dev_name
256+
break
257+
245258
self.dhcp_domain(ns.config['ethernets'][name], cfg)
246259

247260
ret_dict.update({link: cfg.get_final_conf()})
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This file is part of cloud-init. See LICENSE file for license information.
2+
3+
from cloudinit import safeyaml
4+
from cloudinit.net import networkd, network_state
5+
6+
V2_CONFIG_SET_NAME = """\
7+
network:
8+
version: 2
9+
ethernets:
10+
eth0:
11+
match:
12+
macaddress: '00:11:22:33:44:55'
13+
nameservers:
14+
search: [spam.local, eggs.local]
15+
addresses: [8.8.8.8]
16+
eth1:
17+
match:
18+
macaddress: '66:77:88:99:00:11'
19+
set-name: "ens92"
20+
nameservers:
21+
search: [foo.local, bar.local]
22+
addresses: [4.4.4.4]
23+
"""
24+
25+
V2_CONFIG_SET_NAME_RENDERED_ETH0 = """[Match]
26+
MACAddress=00:11:22:33:44:55
27+
Name=eth0
28+
29+
[Network]
30+
DHCP=no
31+
DNS=8.8.8.8
32+
Domains=spam.local eggs.local
33+
34+
"""
35+
36+
V2_CONFIG_SET_NAME_RENDERED_ETH1 = """[Match]
37+
MACAddress=66:77:88:99:00:11
38+
Name=ens92
39+
40+
[Network]
41+
DHCP=no
42+
DNS=4.4.4.4
43+
Domains=foo.local bar.local
44+
45+
"""
46+
47+
48+
class TestNetworkdRenderState:
49+
def _parse_network_state_from_config(self, config):
50+
yaml = safeyaml.load(config)
51+
return network_state.parse_net_config_data(yaml["network"])
52+
53+
def test_networkd_render_with_set_name(self):
54+
ns = self._parse_network_state_from_config(V2_CONFIG_SET_NAME)
55+
renderer = networkd.Renderer()
56+
rendered_content = renderer._render_content(ns)
57+
58+
assert "eth0" in rendered_content
59+
assert rendered_content["eth0"] == V2_CONFIG_SET_NAME_RENDERED_ETH0
60+
assert "ens92" in rendered_content
61+
assert rendered_content["ens92"] == V2_CONFIG_SET_NAME_RENDERED_ETH1
62+
63+
64+
# vi: ts=4 expandtab

0 commit comments

Comments
 (0)