Skip to content

Commit 67fb469

Browse files
author
Frans Fürst
committed
host_labels_interfaces: fix subnet identification
.. by using collapse_addresses() instead of comparing `.network` attributes CMK-31375 Change-Id: Ib390795450fc23ee036e48f023e7efb506908efc
1 parent d6d44c1 commit 67fb469

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

packages/cmk-plugins/cmk/plugins/lib/host_labels_interfaces.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
from collections.abc import Iterable, Mapping
77
from ipaddress import (
8+
collapse_addresses,
89
IPv4Interface,
10+
IPv4Network,
911
IPv6Interface,
1012
IPv6Network,
1113
)
@@ -36,31 +38,41 @@ def host_labels_if(
3638
"""Return host labels describing the host's network 'single-/multihomed' property based on
3739
the IP addresses of its interfaces.
3840
Filters away temporary interfaces and local IP addresses (based on interface name and IP's
39-
ULA prefix, link-local/unspecified/local-host properties) and checks if the remaining IPs
40-
belong to one or more subnets.
41+
ULA prefix, link-local/unspecified/local-host properties) and counts non-overlapping
42+
networks of the remaining addresses.
4143
"""
4244
# Original author: thl-cmk[at]outlook[dot]com
4345

4446
for version in (4, 6):
45-
if valid_networks := set(
46-
# note: contains subnet mask, e.g. 1.2.3.4/24 != 1.2.3.4/32
47-
# for now we define this to be different in terms of single-/multihomed
48-
# for sake of simplicity, but we might want to change this in the future
49-
str(interface_ip.network)
47+
valid_networks = [
48+
interface_ip.network
5049
for name, interface_ips in interfaces.items()
5150
if not any(name.startswith(prefix) for prefix in TEMP_DEVICES)
5251
for interface_ip in interface_ips
53-
if interface_ip.version == version
54-
and not any(
52+
if not any(
5553
(
5654
interface_ip.is_loopback,
5755
interface_ip.is_link_local,
5856
interface_ip.is_unspecified,
5957
interface_ip in IPV6_ULA_NETWORK,
6058
)
6159
)
60+
]
61+
62+
if (
63+
network_count := len(
64+
list(
65+
collapse_addresses([ip for ip in valid_networks if isinstance(ip, IPv4Network)])
66+
)
67+
)
68+
if version == 4
69+
else len(
70+
list(
71+
collapse_addresses([ip for ip in valid_networks if isinstance(ip, IPv6Network)])
72+
)
73+
)
6274
):
6375
yield HostLabel(
6476
name=f"cmk/l3v{version}_topology",
65-
value="singlehomed" if len(valid_networks) == 1 else "multihomed",
77+
value="singlehomed" if network_count == 1 else "multihomed",
6678
)

packages/cmk-plugins/tests/cmk/plugins/lib/test_host_labels_interfaces.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
pytest.param(
2929
{
3030
"eth0": [
31-
IPv4Interface("10.86.60.1/27"),
31+
IPv4Interface("10.86.60.1/24"),
3232
IPv6Interface("fe80::200:5efe:515c:6232/64"),
3333
],
3434
"eth1": [
3535
IPv6Interface("fe80::200:5efe:515c:6232/64"),
36-
IPv4Interface("12.12.12.1/3"),
36+
IPv4Interface("12.12.12.1/24"),
3737
],
3838
},
3939
[
@@ -65,12 +65,12 @@
6565
],
6666
"enp0s31f6": [
6767
IPv4Interface("95.216.118.249"),
68-
IPv6Interface("2a01:4f9:2b:1c86::2/128"), # note: different subnet than..
68+
IPv6Interface("2a01:4f9:2b:1c86::2/128"),
6969
IPv6Interface("fe80::921b:eff:fefe:8a16/64"),
7070
],
7171
"vmbr0": [
7272
IPv4Interface("95.217.149.46/32"),
73-
IPv6Interface("2a01:4f9:2b:1c86::2/64"), # .. this one -> multihomed for now
73+
IPv6Interface("2a01:4f9:2b:1c86::2/64"),
7474
IPv6Interface("fe80::6871:a0ff:fefe:cd60/64"),
7575
],
7676
"vmbr1": [
@@ -81,7 +81,7 @@
8181
},
8282
[
8383
HostLabel("cmk/l3v4_topology", "multihomed"),
84-
HostLabel("cmk/l3v6_topology", "multihomed"),
84+
HostLabel("cmk/l3v6_topology", "singlehomed"),
8585
],
8686
id="host_labels_03",
8787
),

tests/unit/cmk/plugins/network/agent_based/test_ip_addresses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ def test_parse_ip_addresses(
198198
(None, []),
199199
(
200200
(
201-
{"16": IPv4Interface("10.86.60.1/27")},
201+
{"16": IPv4Interface("10.86.60.1/24")},
202202
{"20": IPv6Interface("fe80::200:5efe:515c:6232/64")},
203-
{"if_index1": IPv4Interface("12.12.12.1/3")},
203+
{"if_index1": IPv4Interface("12.12.12.1/24")},
204204
),
205205
[
206206
HostLabel("cmk/l3v4_topology", "multihomed"),

0 commit comments

Comments
 (0)