Skip to content

Commit

Permalink
net/renderer/sysconfig: also use dns information from interface config
Browse files Browse the repository at this point in the history
sysconfig renderer currently only uses global dns and search domain
configuration. This means it ignores interface specific dns configuration
completely. Network manager renderer on the other hand uses global
dns config along with interface and subnet specific dns information to build
complete dns configuration which is then used to generate appropriate keyfiles.
Do the same for sysconfig so that /etc/resolv.conf can have complete dns and
search domain information populated.

Fixes: GH-5400
Signed-off-by: Ani Sinha <anisinha@redhat.com>
  • Loading branch information
ani-sinha committed Jun 13, 2024
1 parent a01b8d3 commit 94ad377
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions cloudinit/net/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,53 @@ def _render_vlan_interfaces(cls, network_state, iface_contents, flavor):

@staticmethod
def _render_dns(network_state, existing_dns_path=None):
# skip writing resolv.conf if network_state doesn't include any input.

found_nameservers = []
found_dns_search = []

for iface in network_state.iter_interfaces():
for subnet in iface["subnets"]:
# Add subnet-level DNS
if "dns_nameservers" in subnet:
found_nameservers.extend(subnet["dns_nameservers"])
if "dns_search" in subnet:
found_dns_search.extend(subnet["dns_search"])

# Add interface-level DNS
if "dns" in iface:
found_nameservers += [
dns
for dns in iface["dns"]["nameservers"]
if dns not in found_nameservers
]
found_dns_search += [
search
for search in iface["dns"]["search"]
if search not in found_dns_search
]

# When both global and interface specific entries are present,
# use them both to generate /etc/resolv.conf eliminating duplicate
# entries. Otherwise use global or interface specific entries whichever
# is provided.
if network_state.dns_nameservers:
found_nameservers += [
nameserver
for nameserver in network_state.dns_nameservers
if nameserver not in found_nameservers
]
if network_state.dns_searchdomains:
found_dns_search += [
search
for search in network_state.dns_searchdomains
if search not in found_dns_search
]

# skip writing resolv.conf if no dns information is provided in conf.
if not any(
[
len(network_state.dns_nameservers),
len(network_state.dns_searchdomains),
len(found_nameservers),
len(found_dns_search),
]
):
return None
Expand All @@ -837,9 +879,9 @@ def _render_dns(network_state, existing_dns_path=None):
content = resolv_conf.ResolvConf(
util.load_text_file(existing_dns_path)
)
for nameserver in network_state.dns_nameservers:
for nameserver in found_nameservers:
content.add_nameserver(nameserver)
for searchdomain in network_state.dns_searchdomains:
for searchdomain in found_dns_search:
content.add_search_domain(searchdomain)
header = _make_header(";")
content_str = str(content)
Expand Down

0 comments on commit 94ad377

Please sign in to comment.