Filter secondary IP address by type (#61434)

* Add filter for secondary ip addresses

Should improve #61370

* Remove unnecessary space

* Add test case for secondary IP address

Test data for IPv6 secondary IP looks wrong but this is what _interfaces_ip() could return looking at the current code

* Change order of tests because of caching issues

Change order of test_network_grains_secondary_ip and test_network_grains_cache because of caching issues when running after test_network_grains_cache

* Unify style in _interfaces_ip

Unify coding style in _interfaces_ip for secondary ip addresses with the style for regular addresses. Also align the attributes for IPv6 secondary ip addresses with regular ipv6 addresses

* Align IPv6 secondary IP attributes with changes to _interfaces_ip

* Add changelog for fix of issue 61370

* Use salt.loader.grain_funcs for secondary ip test

To work around caching issues when changing order of test_network_grains_cache and test_network_grains_secondary_ip use use salt.loader.grain_funcs in both functions. Also we hope this solves the issue, that this test worked in my local dev environment but not on the saltstack jenkins instances.

* Use side_effect to simulate test data

I don't understand what is different when these tests are run on the Jenkins infrastructure. Hope copying this from test_network_grains_cache make the tests work on them.

* Changed checking for secondaryip address type

* Add filter for secondary ip addresses

Should improve #61370

* Remove unnecessary space

* Add test case for secondary IP address

Test data for IPv6 secondary IP looks wrong but this is what _interfaces_ip() could return looking at the current code

* Change order of tests because of caching issues

Change order of test_network_grains_secondary_ip and test_network_grains_cache because of caching issues when running after test_network_grains_cache

* Unify style in _interfaces_ip

Unify coding style in _interfaces_ip for secondary ip addresses with the style for regular addresses. Also align the attributes for IPv6 secondary ip addresses with regular ipv6 addresses

* Align IPv6 secondary IP attributes with changes to _interfaces_ip

* Add changelog for fix of issue 61370

* Use salt.loader.grain_funcs for secondary ip test

To work around caching issues when changing order of test_network_grains_cache and test_network_grains_secondary_ip use use salt.loader.grain_funcs in both functions. Also we hope this solves the issue, that this test worked in my local dev environment but not on the saltstack jenkins instances.

* Use side_effect to simulate test data

I don't understand what is different when these tests are run on the Jenkins infrastructure. Hope copying this from test_network_grains_cache make the tests work on them.

* Changed checking for secondaryip address type

* Satisfy black code formatting

Co-authored-by: Shane Lee <leesh@vmware.com>
Co-authored-by: mayrstefan <stefan+github@mayr-stefan.de>
This commit is contained in:
Stefan Mayr 2022-02-09 04:48:23 +01:00 committed by GitHub
parent f62de898dd
commit 75c0cb7181
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 18 deletions

1
changelog/61370.fixed Normal file
View file

@ -0,0 +1 @@
Fix secondary ip addresses being added to ip4_interfaces and ip6_interfaces at the same time

View file

@ -2528,7 +2528,7 @@ def ip4_interfaces():
if "address" in inet:
iface_ips.append(inet["address"])
for secondary in ifaces[face].get("secondary", []):
if "address" in secondary:
if "address" in secondary and secondary.get("type") == "inet":
iface_ips.append(secondary["address"])
ret[face] = iface_ips
return {"ip4_interfaces": ret}
@ -2553,7 +2553,7 @@ def ip6_interfaces():
if "address" in inet:
iface_ips.append(inet["address"])
for secondary in ifaces[face].get("secondary", []):
if "address" in secondary:
if "address" in secondary and secondary.get("type") == "inet6":
iface_ips.append(secondary["address"])
ret[face] = iface_ips
return {"ip6_interfaces": ret}

View file

@ -743,12 +743,12 @@ def _interfaces_ip(out):
type_, value = tuple(cols[0:2])
iflabel = cols[-1:][0]
if type_ in ("inet", "inet6"):
ipaddr, netmask, broadcast, scope = parse_network(value, cols)
addr_obj = dict()
if "secondary" not in cols:
ipaddr, netmask, broadcast, scope = parse_network(value, cols)
if type_ == "inet":
if "inet" not in data:
data["inet"] = list()
addr_obj = dict()
addr_obj["address"] = ipaddr
addr_obj["netmask"] = netmask
addr_obj["broadcast"] = broadcast
@ -757,25 +757,28 @@ def _interfaces_ip(out):
elif type_ == "inet6":
if "inet6" not in data:
data["inet6"] = list()
addr_obj = dict()
addr_obj["address"] = ipaddr
addr_obj["prefixlen"] = netmask
addr_obj["scope"] = scope
data["inet6"].append(addr_obj)
else:
if "secondary" not in data:
data["secondary"] = list()
ip_, mask, brd, scp = parse_network(value, cols)
data["secondary"].append(
{
"type": type_,
"address": ip_,
"netmask": mask,
"broadcast": brd,
"label": iflabel,
}
)
del ip_, mask, brd, scp
if type_ == "inet":
if "secondary" not in data:
data["secondary"] = list()
addr_obj["type"] = type_
addr_obj["address"] = ipaddr
addr_obj["netmask"] = netmask
addr_obj["broadcast"] = broadcast
addr_obj["label"] = iflabel
data["secondary"].append(addr_obj)
elif type_ == "inet6":
if "secondary" not in data:
data["secondary"] = list()
addr_obj["type"] = type_
addr_obj["address"] = ipaddr
addr_obj["prefixlen"] = netmask
addr_obj["scope"] = scope
data["secondary"].append(addr_obj)
elif type_.startswith("link"):
data["hwaddr"] = value
if iface:

View file

@ -88,6 +88,73 @@ def test_parse_etc_os_release(os_release_dir):
}
def test_network_grains_secondary_ip(tmp_path):
"""
Secondary IP should be added to IPv4 or IPv6 address list depending on type
"""
data = {
"wlo1": {
"up": True,
"hwaddr": "29:9f:9f:e9:67:f4",
"inet": [
{
"address": "172.16.13.85",
"netmask": "255.255.248.0",
"broadcast": "172.16.15.255",
"label": "wlo1",
}
],
"inet6": [
{
"address": "2001:4860:4860::8844",
"prefixlen": "64",
"scope": "fe80::6238:e0ff:fe06:3f6b%enp2s0",
}
],
"secondary": [
{
"type": "inet",
"address": "172.16.13.86",
"netmask": "255.255.248.0",
"broadcast": "172.16.15.255",
"label": "wlo1",
},
{
"type": "inet6",
"address": "2001:4860:4860::8888",
"prefixlen": "64",
"scope": "fe80::6238:e0ff:fe06:3f6b%enp2s0",
},
],
}
}
cache_dir = tmp_path / "cache"
extmods = tmp_path / "extmods"
opts = {
"cachedir": str(cache_dir),
"extension_modules": str(extmods),
"optimization_order": [0],
}
with patch("salt.utils.network.interfaces", side_effect=[data]):
grains = salt.loader.grain_funcs(opts)
ret_ip4 = grains["core.ip4_interfaces"]()
assert ret_ip4["ip4_interfaces"]["wlo1"] == ["172.16.13.85", "172.16.13.86"]
ret_ip6 = grains["core.ip6_interfaces"]()
assert ret_ip6["ip6_interfaces"]["wlo1"] == [
"2001:4860:4860::8844",
"2001:4860:4860::8888",
]
ret_ip = grains["core.ip_interfaces"]()
assert ret_ip["ip_interfaces"]["wlo1"] == [
"172.16.13.85",
"2001:4860:4860::8844",
"172.16.13.86",
"2001:4860:4860::8888",
]
def test_network_grains_cache(tmp_path):
"""
Network interfaces are cache is cleared by the loader