Skip to content

Commit

Permalink
net/nm: Apply global DNS options for static ifaces without DNS.
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Jan 3, 2024
1 parent 492f1be commit 86e25ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cloudinit/net/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def mac_addr(addr):
"""
return addr.replace("-", ":").upper()

def render_interface(self, iface, renderer):
def render_interface(self, iface, network_state, renderer):
"""
Integrate information from network state interface information
into the connection. Most of the work is done here.
Expand Down Expand Up @@ -307,6 +307,7 @@ def render_interface(self, iface, renderer):

device_mtu = iface["mtu"]
ipv4_mtu = None
has_address = False
found_nameservers = []
found_dns_search = []

Expand All @@ -317,6 +318,7 @@ def render_interface(self, iface, renderer):
self._set_ip_method(family, subnet["type"])
if "address" in subnet:
self._add_address(family, subnet)
has_address = True
if "gateway" in subnet:
self.config[family]["gateway"] = subnet["gateway"]
for route in subnet["routes"]:
Expand All @@ -334,6 +336,14 @@ def render_interface(self, iface, renderer):
found_nameservers = iface["dns"]["nameservers"]
found_dns_search = iface["dns"]["search"]

# Now add in the global DNS information if we have an address but no
# DNS information yet
if has_address:
if not found_nameservers and network_state.dns_nameservers:
found_nameservers = network_state.dns_nameservers
if not found_dns_search and network_state.dns_searchdomains:
found_dns_search = network_state.dns_searchdomains

for nameserver in found_nameservers:
self._add_nameserver(nameserver)
if found_dns_search:
Expand Down Expand Up @@ -453,7 +463,7 @@ def render_network_state(
# Now render the actual interface configuration
for iface in network_state.iter_interfaces():
conn = self.connections[iface["name"]]
conn.render_interface(iface, self)
conn.render_interface(iface, network_state, self)

# And finally write the files
for con_id, conn in self.connections.items():
Expand Down
6 changes: 6 additions & 0 deletions tests/unittests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@
may-fail=false
address1=172.19.1.34/22
route1=0.0.0.0/0,172.19.3.254
dns=172.19.0.12;
""".lstrip(),
),
Expand Down Expand Up @@ -2822,6 +2823,8 @@
method=manual
may-fail=false
address1=192.168.200.7/24
dns=8.8.8.8;4.4.4.4;8.8.4.4;
dns-search=barley.maas;wark.maas;foobar.maas;
"""
),
Expand Down Expand Up @@ -2930,12 +2933,15 @@
method=manual
may-fail=false
address1=192.168.14.2/24
dns=8.8.8.8;4.4.4.4;8.8.4.4;
dns-search=barley.maas;wark.maas;foobar.maas;
[ipv6]
method=manual
may-fail=false
address1=2001:1::1/64
route1=::/0,2001:4800:78ff:1b::1
dns-search=barley.maas;wark.maas;foobar.maas;
"""
),
Expand Down

0 comments on commit 86e25ca

Please sign in to comment.