diff options
author | Louis Sautier <sautier.louis@gmail.com> | 2023-01-09 20:24:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-09 13:24:08 -0600 |
commit | df9f714e2fdc9b6bfea74c0dd012e345035ea1b8 (patch) | |
tree | 7545ccb7737d1505f88295a367877073b339da30 | |
parent | 2b738212582811ff472f11d36df20e9d2826494b (diff) | |
download | cloud-init-git-df9f714e2fdc9b6bfea74c0dd012e345035ea1b8.tar.gz |
network/netplan: add gateways as on-link when necessary (#1931)
network/netplan: add gateways as on-link when necessary
When the gateway isn't part of the subnet's network, the "on-link" flag
is required for the route to get added.
LP: #2000596
-rw-r--r-- | cloudinit/net/netplan.py | 23 | ||||
-rw-r--r-- | tests/unittests/test_net.py | 38 | ||||
-rw-r--r-- | tools/.github-cla-signers | 1 |
3 files changed, 62 insertions, 0 deletions
diff --git a/cloudinit/net/netplan.py b/cloudinit/net/netplan.py index 67b139dd..e0272a8c 100644 --- a/cloudinit/net/netplan.py +++ b/cloudinit/net/netplan.py @@ -1,6 +1,7 @@ # This file is part of cloud-init. See LICENSE file ... import copy +import ipaddress import os import textwrap from typing import Optional, cast @@ -120,6 +121,28 @@ def _extract_addresses(config: dict, entry: dict, ifname, features=None): "via": subnet.get("gateway"), "to": "default", } + try: + subnet_gateway = ipaddress.ip_address(subnet["gateway"]) + subnet_network = ipaddress.ip_network(addr, strict=False) + # If the gateway is not contained within the subnet's + # network, mark it as on-link so that it can still be + # reached. + if subnet_gateway not in subnet_network: + LOG.debug( + "Gateway %s is not contained within subnet %s," + " adding on-link flag", + subnet["gateway"], + addr, + ) + new_route["on-link"] = True + except ValueError as e: + LOG.warning( + "Failed to check whether gateway %s" + " is contained within subnet %s: %s", + subnet["gateway"], + addr, + e, + ) routes.append(new_route) if "dns_nameservers" in subnet: nameservers += _listify(subnet.get("dns_nameservers", [])) diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index bf6e375d..7ed041d0 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -6289,6 +6289,44 @@ class TestNetplanNetRendering: """, id="one_subnet_old_new_gateway46", ), + # Assert gateways outside of the subnet's network are added with + # the on-link flag + pytest.param( + """ + version: 1 + config: + - type: physical + name: interface0 + mac_address: '00:11:22:33:44:55' + subnets: + - type: static + address: 192.168.23.14/24 + gateway: 192.168.255.1 + - type: static + address: 2001:cafe::/64 + gateway: 2001:ffff::1 + """, + """ + network: + version: 2 + ethernets: + interface0: + addresses: + - 192.168.23.14/24 + - 2001:cafe::/64 + match: + macaddress: 00:11:22:33:44:55 + routes: + - to: default + via: 192.168.255.1 + on-link: true + - to: default + via: 2001:ffff::1 + on-link: true + set-name: interface0 + """, + id="onlink_gateways", + ), ], ) @mock.patch( diff --git a/tools/.github-cla-signers b/tools/.github-cla-signers index a7c2b17c..7ef6f737 100644 --- a/tools/.github-cla-signers +++ b/tools/.github-cla-signers @@ -103,6 +103,7 @@ s-makin SadeghHayeri SRv6d sarahwzadara +sbraz scorpion44 shaardie shell-skrimp |