Skip to content

Commit

Permalink
Fix configuration of DNS servers via OpenStack (#5384)
Browse files Browse the repository at this point in the history
Ensure DNS server addresses are parsed from the proper location
of network_data.json

Fixes #5386

Co-authored-by: Alberto Contreras <[email protected]>
  • Loading branch information
2 people authored and holmanb committed Aug 6, 2024
1 parent ed80746 commit e4c6604
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cloudinit/sources/helpers/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ def convert_net_json(network_json=None, known_macs=None):
"scope",
"dns_nameservers",
"dns_search",
"routes",
],
"routes": ["network", "destination", "netmask", "gateway", "metric"],
}

links = network_json.get("links", [])
Expand Down Expand Up @@ -620,6 +620,20 @@ def convert_net_json(network_json=None, known_macs=None):
(k, v) for k, v in network.items() if k in valid_keys["subnet"]
)

# Filter the route entries as they may contain extra elements such
# as DNS which are required elsewhere by the cloudinit schema
routes = [
dict(
(k, v)
for k, v in route.items()
if k in valid_keys["routes"]
)
for route in network.get("routes", [])
]

if routes:
subnet.update({"routes": routes})

if network["type"] == "ipv4_dhcp":
subnet.update({"type": "dhcp4"})
elif network["type"] == "ipv6_dhcp":
Expand All @@ -646,11 +660,22 @@ def convert_net_json(network_json=None, known_macs=None):
}
)

# Look for either subnet or network specific DNS servers
# and add them as subnet level DNS entries.
# Subnet specific nameservers
dns_nameservers = [
service["address"]
for service in network.get("services", [])
for route in network.get("routes", [])
for service in route.get("services", [])
if service.get("type") == "dns"
]
# Network specific nameservers
for service in network.get("services", []):
if service.get("type") != "dns":
continue
if service["address"] in dns_nameservers:
continue
dns_nameservers.append(service["address"])
if dns_nameservers:
subnet["dns_nameservers"] = dns_nameservers

Expand Down
126 changes: 126 additions & 0 deletions tests/unittests/sources/helpers/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,129 @@ def test_bond_mac(self):
assert expected == openstack.convert_net_json(
network_json=network_json, known_macs=macs
)

def test_dns_servers(self):
"""
Verify additional properties under subnet.routes are not rendered
"""
network_json = {
"links": [
{
"id": "ens1f0np0",
"name": "ens1f0np0",
"type": "phy",
"ethernet_mac_address": "xx:xx:xx:xx:xx:00",
"mtu": 9000,
},
{
"id": "ens1f1np1",
"name": "ens1f1np1",
"type": "phy",
"ethernet_mac_address": "xx:xx:xx:xx:xx:01",
"mtu": 9000,
},
{
"id": "bond0",
"name": "bond0",
"type": "bond",
"bond_links": ["ens1f0np0", "ens1f1np1"],
"mtu": 9000,
"ethernet_mac_address": "xx:xx:xx:xx:xx:00",
"bond_mode": "802.3ad",
"bond_xmit_hash_policy": "layer3+4",
"bond_miimon": 100,
},
{
"id": "bond0.123",
"name": "bond0.123",
"type": "vlan",
"vlan_link": "bond0",
"vlan_id": 123,
"vlan_mac_address": "xx:xx:xx:xx:xx:00",
},
],
"networks": [
{
"id": "publicnet-ipv4",
"type": "ipv4",
"link": "bond0.123",
"ip_address": "x.x.x.x",
"netmask": "255.255.255.0",
"routes": [
{
"network": "0.0.0.0",
"netmask": "0.0.0.0",
"gateway": "x.x.x.1",
"services": [
{"type": "dns", "address": "1.1.1.1"},
{"type": "dns", "address": "8.8.8.8"},
],
}
],
"network_id": "00000000-0000-0000-0000-000000000000",
}
],
"services": [],
}
expected = {
"version": 1,
"config": [
{
"name": "ens1f0np0",
"type": "physical",
"mtu": 9000,
"subnets": [],
"mac_address": "xx:xx:xx:xx:xx:00",
},
{
"name": "ens1f1np1",
"type": "physical",
"mtu": 9000,
"subnets": [],
"mac_address": "xx:xx:xx:xx:xx:01",
},
{
"name": "bond0",
"type": "bond",
"mtu": 9000,
"subnets": [],
"mac_address": "xx:xx:xx:xx:xx:00",
"params": {
"bond-mode": "802.3ad",
"bond-xmit_hash_policy": "layer3+4",
"bond-miimon": 100,
},
"bond_interfaces": ["ens1f0np0", "ens1f1np1"],
},
{
"name": "bond0.123",
"type": "vlan",
"subnets": [
{
"type": "static",
"netmask": "255.255.255.0",
"routes": [
{
"network": "0.0.0.0",
"netmask": "0.0.0.0",
"gateway": "x.x.x.1",
}
],
"address": "x.x.x.x",
"dns_nameservers": ["1.1.1.1", "8.8.8.8"],
"ipv4": True,
}
],
"vlan_id": 123,
"vlan_link": "bond0",
},
],
}
macs = {
"xx:xx:xx:xx:xx:00": "ens1f0np0",
"xx:xx:xx:xx:xx:01": "ens1f1np1",
}
netcfg = openstack.convert_net_json(
network_json=network_json, known_macs=macs
)
assert expected == netcfg

0 comments on commit e4c6604

Please sign in to comment.