Skip to content

Commit

Permalink
fix(azure): Avoid non-primary nics from having routes to DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvoM committed Apr 17, 2024
1 parent 5d587ec commit d1e179d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
36 changes: 31 additions & 5 deletions cloudinit/sources/DataSourceAzure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,12 +1488,15 @@ def _generate_network_config(self):
and self.ds_cfg.get("apply_network_config")
):
try:
return generate_network_config_from_instance_network_metadata(
self._metadata_imds["network"],
apply_network_config_for_secondary_ips=self.ds_cfg.get(
"apply_network_config_for_secondary_ips"
),
netcfg = (
generate_network_config_from_instance_network_metadata(
self._metadata_imds["network"],
apply_network_config_for_secondary_ips=self.ds_cfg.get(
"apply_network_config_for_secondary_ips"
),
)
)
return configure_ephemeral_and_hotplug_device(netcfg)
except Exception as e:
LOG.error(
"Failed generating network config "
Expand Down Expand Up @@ -1945,6 +1948,29 @@ def load_azure_ds_dir(source_dir):
return (md, ud, cfg, {"ovf-env.xml": contents})


def configure_ephemeral_and_hotplug_device(netconfig: dict) -> dict:
"""
Configure the ephemeral and hotplug details.
parameters:
netconfig: dict, Network config details with other ethernet devices.
returns the updated network configuration
"""
netconfig["ethernets"]["ephemeral"] = {
"dhcp4": True,
"dhcp4-overrides": {"use-dns": False},
"optional": True,
"match": {"driver": "hv_netvsc", "name": "!eth0"},
}

netconfig["ethernets"]["hotpluggedeth0"] = {
"dhcp4": True,
"match": {"driver": "hv_netvsc", "name": "eth0"},
}

return netconfig


@azure_ds_telemetry_reporter
def generate_network_config_from_instance_network_metadata(
network_metadata: dict,
Expand Down
2 changes: 1 addition & 1 deletion integration-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PyPI requirements for cloud-init integration testing
# https://cloudinit.readthedocs.io/en/latest/topics/integration_tests.html
#
pycloudlib>=5.10.0,<1!6
pycloudlib>=5.10.0

# Avoid breaking change in `testpaths` treatment forced
# test/unittests/conftest.py to be loaded by our integration-tests tox env
Expand Down
37 changes: 37 additions & 0 deletions tests/integration_tests/datasources/test_azure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
import pytest
from pycloudlib.cloud import ImageType
from pycloudlib.azure.util import AzureCreateParams, AzureParams

from tests.integration_tests.clouds import IntegrationCloud
from tests.integration_tests.conftest import get_validated_source
Expand Down Expand Up @@ -45,3 +47,38 @@ def test_azure_eject(session_cloud: IntegrationCloud):
session_cloud.cloud_instance.delete_image(snapshot_id)
else:
_check_for_eject_errors(instance)


@pytest.mark.skipif(PLATFORM != "azure", reason="Test is Azure specific")
def test_azure_multi_nic_setup(session_cloud: IntegrationCloud) -> None:
us = datetime.datetime.now().strftime("%f")
rg_params = AzureParams(f"ci-test-multi-nic-setup-{us}", None)
nic_one = AzureCreateParams(f"ci-nic1-test-{us}", rg_params.name, None)
nic_two = AzureCreateParams(f"ci-nic2-test-{us}", rg_params.name, None)
with session_cloud.launch(
launch_kwargs={
"image_id": session_cloud.cloud_instance.daily_image(
CURRENT_RELEASE.series, image_type=ImageType.GENERIC
),
}
) as instance:
source = get_validated_source(session_cloud)
if source.installs_new_version():
instance.install_new_cloud_init(source, clean=True)
snapshot_id = instance.snapshot()
try:
with session_cloud.launch(
launch_kwargs={
"image_id": snapshot_id,
"resource_group_params": rg_params,
"network_interfaces_params": [nic_one, nic_two],
}
) as snapshot_instance:
_check_for_eject_errors(snapshot_instance)
ret = snapshot_instance.execute("ip route")
assert ret.ok
assert "eth1 proto dhcp" not in ret.stdout
finally:
session_cloud.cloud_instance.delete_image(snapshot_id)
else:
_check_for_eject_errors(instance)
46 changes: 43 additions & 3 deletions tests/unittests/sources/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,17 @@ def test_single_ipv4_nic_configuration(
"dhcp6": False,
"match": {"macaddress": "00:0d:3a:04:75:98"},
"set-name": "eth0",
}
},
"ephemeral": {
"dhcp4": True,
"dhcp4-overrides": {"use-dns": False},
"optional": True,
"match": {"driver": "hv_netvsc", "name": "!eth0"},
},
"hotpluggedeth0": {
"dhcp4": True,
"match": {"driver": "hv_netvsc", "name": "eth0"},
},
},
"version": 2,
}
Expand Down Expand Up @@ -1542,7 +1552,17 @@ def test_network_config_set_from_imds(self):
"dhcp6": False,
"dhcp4": True,
"dhcp4-overrides": {"route-metric": 100},
}
},
"ephemeral": {
"dhcp4": True,
"dhcp4-overrides": {"use-dns": False},
"optional": True,
"match": {"driver": "hv_netvsc", "name": "!eth0"},
},
"hotpluggedeth0": {
"dhcp4": True,
"match": {"driver": "hv_netvsc", "name": "eth0"},
},
},
"version": 2,
}
Expand Down Expand Up @@ -1580,6 +1600,16 @@ def test_network_config_set_from_imds_route_metric_for_secondary_nic(self):
"dhcp4": True,
"dhcp4-overrides": {"route-metric": 300},
},
"ephemeral": {
"dhcp4": True,
"dhcp4-overrides": {"use-dns": False},
"optional": True,
"match": {"driver": "hv_netvsc", "name": "!eth0"},
},
"hotpluggedeth0": {
"dhcp4": True,
"match": {"driver": "hv_netvsc", "name": "eth0"},
},
},
"version": 2,
}
Expand Down Expand Up @@ -1611,7 +1641,17 @@ def test_network_config_set_from_imds_for_secondary_nic_no_ip(self):
"dhcp6": False,
"dhcp4": True,
"dhcp4-overrides": {"route-metric": 100},
}
},
"ephemeral": {
"dhcp4": True,
"dhcp4-overrides": {"use-dns": False},
"optional": True,
"match": {"driver": "hv_netvsc", "name": "!eth0"},
},
"hotpluggedeth0": {
"dhcp4": True,
"match": {"driver": "hv_netvsc", "name": "eth0"},
},
},
"version": 2,
}
Expand Down

0 comments on commit d1e179d

Please sign in to comment.