Skip to content

Commit

Permalink
net/network_manager: do not set "may-fail" to False for both ipv6 and…
Browse files Browse the repository at this point in the history
… ipv6

If "may-fail" set set to False in the Network Manager keyfile for both ipv4
and ipv6, it essentially means both ipv4 and ipv6 network initialization must
succeed for the overall network configuration to succeed. This means, for
environments where only ipv4 or ipv6 is available but not both, the overall
network configuration will fail. This is not what we want. When both ipv4 and
ipv6 are configured, it is enough for the overall configuration to succeed if
any one succeeds. Therefore, set "may-fail" to True when both ipv4 and ipv6
are configured in the Network Manager keyfile for both ipv4 and ipv6.

Please see discussions in PR canonical#4474.

Signed-off-by: Ani Sinha <anisinha@redhat.com>
  • Loading branch information
ani-sinha committed Nov 23, 2023
1 parent 2945615 commit 6888b49
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
47 changes: 47 additions & 0 deletions cloudinit/net/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,46 @@ def _set_default(self, section, option, value):
if not self.config.has_option(section, option):
self.config[section][option] = value

def _config_option_is_set(self, section, option):
"""
Checks if a config option is set.
"""
return self.config.has_section(section) and self.config.has_option(
section, option
)

def _get_config_option(self, section, option):
"""
Returns a config option if its set, else returns
None.
"""
if self._config_option_is_set(section, option):
return self.config[section][option]
else:
return None

def _change_set_config_option(self, section, option, value):
"""
Overrides the value of a config option if its already set.
Else, if the config option is not set, it does nothing.
"""
if self._config_option_is_set(section, option):
self.config[section][option] = value

def _set_mayfail_true_if_both_false(self):
"""
If for both ipv4 and ipv6, 'may-fail' is set to be False,
set it to True for both of them.
"""
for family in ["ipv4", "ipv6"]:
if not self._config_option_is_set(family, "may-fail"):
return
if self._get_config_option(family, "may-fail") == "true":
return

for family in ["ipv4", "ipv6"]:
self._change_set_config_option(family, "may-fail", "true")

def _set_ip_method(self, family, subnet_type):
"""
Ensures there's appropriate [ipv4]/[ipv6] for given family
Expand Down Expand Up @@ -115,6 +155,13 @@ def _set_ip_method(self, family, subnet_type):

self.config[family]["method"] = method
self._set_default(family, "may-fail", "false")
# we do not want to set may-fail to false for both ipv4 and ipv6 at the
# at the same time. This will make the network configuration work only
# when both ipv4 and ipv6 succeeds which may not be true for most
# environments. We want any one to succeed in those cases. So in the
# case where "may-fail" is set to False for both ipv4 and ipv6, set
# them both to True.
self._set_mayfail_true_if_both_false()

def _add_numbered(self, section, key_prefix, value):
"""
Expand Down
28 changes: 14 additions & 14 deletions tests/unittests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,11 +1425,11 @@
[ipv4]
method=auto
may-fail=false
may-fail=true
[ipv6]
method=auto
may-fail=false
may-fail=true
"""
),
Expand Down Expand Up @@ -1558,12 +1558,12 @@
[ipv4]
method=manual
may-fail=false
may-fail=true
address1=192.168.14.2/24
[ipv6]
method=manual
may-fail=false
may-fail=true
address1=2001:1::1/64
"""
Expand Down Expand Up @@ -1598,11 +1598,11 @@
[ipv6]
method=auto
may-fail=false
may-fail=true
[ipv4]
method=auto
may-fail=false
may-fail=true
"""
),
Expand Down Expand Up @@ -2876,12 +2876,12 @@
[ipv4]
method=manual
may-fail=false
may-fail=true
address1=192.168.14.2/24
[ipv6]
method=manual
may-fail=false
may-fail=true
address1=2001:1::1/64
route1=::/0,2001:4800:78ff:1b::1
Expand Down Expand Up @@ -3537,15 +3537,15 @@
[ipv4]
method=manual
may-fail=false
may-fail=true
address1=192.168.0.2/24
gateway=192.168.0.1
route1=10.1.3.0/24,192.168.0.3
address2=192.168.1.2/24
[ipv6]
method=manual
may-fail=false
may-fail=true
address1=2001:1::1/92
route1=2001:67c::/32,2001:67c:1562::1
route2=3001:67c::/32,3001:67c:15::1
Expand Down Expand Up @@ -3659,14 +3659,14 @@
[ipv4]
method=manual
may-fail=false
may-fail=true
address1=192.168.2.2/24
address2=192.168.1.2/24
gateway=192.168.1.1
[ipv6]
method=manual
may-fail=false
may-fail=true
address1=2001:1::bbbb/96
route1=::/0,2001:1::1
Expand Down Expand Up @@ -5984,11 +5984,11 @@ def test_default_generation(
[ipv4]
method=auto
may-fail=false
may-fail=true
[ipv6]
method=auto
may-fail=false
may-fail=true
"""
),
Expand Down

0 comments on commit 6888b49

Please sign in to comment.