Skip to content

Commit

Permalink
chore: Add feature flag for manual network waiting (#5977)
Browse files Browse the repository at this point in the history
Controls the behavior added in e30549e for easier downstream patching
  • Loading branch information
TheRealFalcon authored Jan 15, 2025
1 parent 6226277 commit 5f290f5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 deletions.
35 changes: 19 additions & 16 deletions cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import yaml
from typing import Optional, Tuple, Callable, Union

from cloudinit import netinfo
from cloudinit import features, netinfo
from cloudinit import signal_handler
from cloudinit import sources
from cloudinit import socket
Expand Down Expand Up @@ -486,7 +486,9 @@ def main_init(name, args):
mode = sources.DSMODE_LOCAL if args.local else sources.DSMODE_NETWORK

if mode == sources.DSMODE_NETWORK:
if not os.path.exists(init.paths.get_runpath(".skip-network")):
if features.MANUAL_NETWORK_WAIT and not os.path.exists(
init.paths.get_runpath(".skip-network")
):
LOG.debug("Will wait for network connectivity before continuing")
init.distro.wait_for_network()
existing = "trust"
Expand Down Expand Up @@ -560,20 +562,21 @@ def main_init(name, args):
init.apply_network_config(bring_up=bring_up_interfaces)

if mode == sources.DSMODE_LOCAL:
should_wait, reason = _should_wait_on_network(init.datasource)
if should_wait:
LOG.debug(
"Network connectivity determined necessary for "
"cloud-init's network stage. Reason: %s",
reason,
)
else:
LOG.debug(
"Network connectivity determined unnecessary for "
"cloud-init's network stage. Reason: %s",
reason,
)
util.write_file(init.paths.get_runpath(".skip-network"), "")
if features.MANUAL_NETWORK_WAIT:
should_wait, reason = _should_wait_on_network(init.datasource)
if should_wait:
LOG.debug(
"Network connectivity determined necessary for "
"cloud-init's network stage. Reason: %s",
reason,
)
else:
LOG.debug(
"Network connectivity determined unnecessary for "
"cloud-init's network stage. Reason: %s",
reason,
)
util.write_file(init.paths.get_runpath(".skip-network"), "")

if init.datasource.dsmode != mode:
LOG.debug(
Expand Down
11 changes: 11 additions & 0 deletions cloudinit/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@
to write /etc/apt/sources.list directly.
"""

MANUAL_NETWORK_WAIT = True
"""
On Ubuntu systems, cloud-init-network.service will start immediately after
cloud-init-local.service and manually wait for network online when necessary.
If False, rely on systemd ordering to ensure network is available before
starting cloud-init-network.service.
Note that in addition to this flag, downstream patches are also likely needed
to modify the systemd unit files.
"""

DEPRECATION_INFO_BOUNDARY = "devel"
"""
DEPRECATION_INFO_BOUNDARY is used by distros to configure at which upstream
Expand Down
7 changes: 5 additions & 2 deletions tests/integration_tests/datasources/test_nocloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from pycloudlib.lxd.instance import LXDInstance

from cloudinit import lifecycle
from cloudinit import features, lifecycle
from cloudinit.subp import subp
from tests.integration_tests.instances import IntegrationInstance
from tests.integration_tests.integration_settings import PLATFORM
Expand Down Expand Up @@ -100,7 +100,10 @@ def test_nocloud_seedfrom_vendordata(client: IntegrationInstance):
client.restart()
assert client.execute("cloud-init status").ok
assert "seeded_vendordata_test_file" in client.execute("ls /var/tmp")
assert network_wait_logged(client.execute("cat /var/log/cloud-init.log"))
assert (
network_wait_logged(client.execute("cat /var/log/cloud-init.log"))
== features.MANUAL_NETWORK_WAIT
)


SMBIOS_USERDATA = """\
Expand Down
8 changes: 6 additions & 2 deletions tests/integration_tests/modules/test_boothook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from cloudinit import features
from tests.integration_tests.instances import IntegrationInstance
from tests.integration_tests.util import (
network_wait_logged,
Expand Down Expand Up @@ -55,6 +56,9 @@ def test_boothook_waits_for_network(
):
"""Test boothook handling waits for network before running."""
client = class_client
assert network_wait_logged(
client.read_from_file("/var/log/cloud-init.log")
assert (
network_wait_logged(
client.read_from_file("/var/log/cloud-init.log")
)
== features.MANUAL_NETWORK_WAIT
)
4 changes: 2 additions & 2 deletions tests/unittests/cmd/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pytest

from cloudinit import safeyaml, util
from cloudinit import features, safeyaml, util
from cloudinit.cmd import main
from cloudinit.util import ensure_dir, load_text_file, write_file

Expand Down Expand Up @@ -363,7 +363,7 @@ def test_distro_wait_for_network(
skip_log_setup=False,
)
main.main_init("init", cmdargs)
if expected_add_wait:
if features.MANUAL_NETWORK_WAIT and expected_add_wait:
m_nm.assert_called_once()
m_subp.assert_called_with(
["systemctl", "start", "systemd-networkd-wait-online.service"]
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def test_shellscript(self, init_tmp, tmpdir, caplog):
"DEPRECATION_INFO_BOUNDARY": "devel",
"NOCLOUD_SEED_URL_APPEND_FORWARD_SLASH": False,
"APT_DEB822_SOURCE_LIST_FILE": True,
"MANUAL_NETWORK_WAIT": True,
},
"system_info": {
"default_user": {"name": "ubuntu"},
Expand Down
2 changes: 2 additions & 0 deletions tests/unittests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_feature_without_override(self):
DEPRECATION_INFO_BOUNDARY="devel",
NOCLOUD_SEED_URL_APPEND_FORWARD_SLASH=False,
APT_DEB822_SOURCE_LIST_FILE=True,
MANUAL_NETWORK_WAIT=False,
):
assert {
"ERROR_ON_USER_DATA_FAILURE": True,
Expand All @@ -31,4 +32,5 @@ def test_feature_without_override(self):
"NOCLOUD_SEED_URL_APPEND_FORWARD_SLASH": False,
"APT_DEB822_SOURCE_LIST_FILE": True,
"DEPRECATION_INFO_BOUNDARY": "devel",
"MANUAL_NETWORK_WAIT": False,
} == features.get_features()

0 comments on commit 5f290f5

Please sign in to comment.