Skip to content

Commit

Permalink
update ephemeral DCHPv4 to receive list of connectivty url data inste…
Browse files Browse the repository at this point in the history
…ad of single dictionary
  • Loading branch information
a-dubs committed Oct 9, 2024
1 parent f950064 commit d7c1435
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 117 deletions.
52 changes: 27 additions & 25 deletions cloudinit/net/ephemeral.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import contextlib
import logging
from functools import partial
from typing import Any, Callable, Dict, List, Optional
from typing import Any, Callable, Dict, List, Optional, Tuple

import cloudinit.net as net
import cloudinit.netinfo as netinfo
Expand All @@ -20,7 +20,7 @@ class EphemeralIPv4Network:
No operations are performed if the provided interface already has the
specified configuration.
This can be verified with the connectivity_url_data.
This can be verified with the connectivity_urls_data.
If unconnected, bring up the interface with valid ip, prefix and broadcast.
If router is provided setup a default route for that interface. Upon
context exit, clean up the interface leaving no configuration behind.
Expand Down Expand Up @@ -281,28 +281,21 @@ def __init__(
self,
distro,
iface=None,
# TODO: combine these. keeping connectivity_url_data for backwards compatibility
connectivity_url_data: Optional[Dict[str, Any]] = None,
connectivity_urls: Optional[List[Dict[str, Any]]] = None,
connectivity_urls_data: Optional[List[Dict[str, Any]]] = None,
dhcp_log_func=None,
):
self.iface = iface
self._ephipv4: Optional[EphemeralIPv4Network] = None
self.lease: Optional[Dict[str, Any]] = None
self.dhcp_log_func = dhcp_log_func
self.connectivity_url_data = connectivity_url_data
self.connectivity_urls = connectivity_urls
self.connectivity_urls_data = connectivity_urls_data or []
self.distro = distro
self.interface_addrs_before_dhcp = netinfo.netdev_info()

def __enter__(self):
"""Setup sandboxed dhcp context, unless connectivity_url can already be
reached."""
# combine the connectivity_url_data and connectivity_urls into a single list
urls_data = self.connectivity_urls or []
if self.connectivity_url_data:
urls_data.append(self.connectivity_url_data)
for url_data in urls_data:
for url_data in self.connectivity_urls_data:
if net.has_url_connectivity(url_data):
LOG.debug(
"Skip ephemeral DHCP setup, instance has connectivity"
Expand Down Expand Up @@ -411,7 +404,7 @@ def __init__(
interface,
ipv6: bool = False,
ipv4: bool = True,
connectivity_url_data: Optional[Dict[str, Any]] = None,
connectivity_urls_data: Optional[Dict[str, Any]] = None,
ipv6_connectivity_check_callback: Optional[Callable] = None,
):
"""
Expand All @@ -420,8 +413,8 @@ def __init__(
interface: The interface to bring up
ipv6: Whether to bring up an ipv6 network
ipv4: Whether to bring up an ipv4 network
connectivity_url_data: Url data to use for connectivity check
before bringing up DHCPv4 ephemeral network.
connectivity_urls_data: List of url data to use for connectivity
check before bringing up DHCPv4 ephemeral network.
ipv6_connectivity_check_callback: A callback to check for ipv6
connectivity. If provided, it is assumed that ipv6 networking
is preferred and ipv4 networking will be skipped if ipv6
Expand All @@ -435,7 +428,7 @@ def __init__(
self.stack = contextlib.ExitStack()
self.state_msg: str = ""
self.distro = distro
self.connectivity_url_data = connectivity_url_data
self.connectivity_urls_data = connectivity_urls_data
self.ipv6_connectivity_check_callback = (
ipv6_connectivity_check_callback
)
Expand Down Expand Up @@ -493,7 +486,7 @@ def __enter__(self):

def _do_ipv4(
self, ephemeral_obtained, exceptions
) -> tuple[str, list[Exception]]:
) -> Tuple[str, List[Exception]]:
"""
Attempt to bring up an ephemeral network for ipv4 on the interface.
Expand All @@ -503,30 +496,36 @@ def _do_ipv4(
exceptions: List of exceptions encountered so far
Returns:
tuple: A tuple containing the updated ephemeral_obtained and
A tuple containing the updated ephemeral_obtained and
exceptions values
"""
try:
self.stack.enter_context(
EphemeralDHCPv4(
distro=self.distro,
iface=self.interface,
connectivity_url_data=self.connectivity_url_data,
connectivity_urls_data=self.connectivity_urls_data,
)
)
ephemeral_obtained = True
LOG.debug(
"[CPC-3194] Successfully brought up %s for ipv4.",
"Successfully brought up %s for ephemeral ipv4 networking.",
self.interface,
)
except (ProcessExecutionError, NoDHCPLeaseError) as e:
LOG.debug("[CPC-3194] Failed to bring up %s for ipv4.", self)
LOG.debug(
"Failed to bring up %s for ephemeral ipv4 networking.",
self.interface,
)
# we don't set ephemeral_obtained to False here because we want to
# retain a potential true value from any previous successful
# ephemeral network setup
exceptions.append(e)
return ephemeral_obtained, exceptions

def _do_ipv6(
self, ephemeral_obtained, exceptions
) -> tuple[str, list[Exception]]:
) -> Tuple[str, List[Exception]]:
"""
Attempt to bring up an ephemeral network for ipv6 on the interface.
Expand All @@ -536,7 +535,7 @@ def _do_ipv6(
exceptions: List of exceptions encountered so far
Returns:
tuple: A tuple containing the updated ephemeral_obtained and
tupleA tuple containing the updated ephemeral_obtained and
exceptions values
"""
try:
Expand All @@ -548,11 +547,14 @@ def _do_ipv6(
)
ephemeral_obtained = True
LOG.debug(
"Successfully brought up %s for ipv6.",
"Successfully brought up %s for ephemeral ipv6 networking.",
self.interface,
)
except ProcessExecutionError as e:
LOG.debug("Failed to bring up %s for ipv6.", self)
LOG.debug(
"Failed to bring up %s for ephemeral ipv6 networking.",
self.interface,
)
# we don't set ephemeral_obtained to False here because we want to
# retain a potential true value from any previous successful
# ephemeral network setup
Expand Down
8 changes: 5 additions & 3 deletions cloudinit/sources/DataSourceHetzner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ def _get_data(self):
with EphemeralDHCPv4(
self.distro,
iface=net.find_fallback_nic(),
connectivity_url_data={
"url": BASE_URL_V1 + "/metadata/instance-id",
},
connectivity_urls_data=[
{
"url": BASE_URL_V1 + "/metadata/instance-id",
}
],
):
md = hc_helper.read_metadata(
self.metadata_address,
Expand Down
8 changes: 5 additions & 3 deletions cloudinit/sources/DataSourceNWCS.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ def get_metadata(self):
with EphemeralDHCPv4(
self.distro,
iface=net.find_fallback_nic(),
connectivity_url_data={
"url": BASE_URL_V1 + "/metadata/instance-id",
},
connectivity_urls_data=[
{
"url": BASE_URL_V1 + "/metadata/instance-id",
}
],
):
return read_metadata(
self.metadata_address,
Expand Down
Loading

0 comments on commit d7c1435

Please sign in to comment.