Skip to content

Commit

Permalink
Pass complete netplan configuration data structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrassler committed Sep 17, 2024
1 parent daacae7 commit 88cdc11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 55 deletions.
36 changes: 4 additions & 32 deletions cloudinit/sources/DataSourceCloudCIX.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import json
import logging
from typing import Any, Dict, Optional
from typing import Optional

from cloudinit import dmi, net, sources, url_helper, util
from cloudinit import dmi, sources, url_helper, util

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,7 +45,7 @@ def _get_data(self):
func=self.crawl_metadata_service,
)
except sources.InvalidMetaDataException as error:
LOG.debug(
LOG.error(
"Failed to read data from CloudCIX datasource: %s", error
)
return False
Expand Down Expand Up @@ -111,37 +111,9 @@ def network_config(self):

if not self.metadata:
return None
self._net_cfg = self._generate_net_cfg(self.metadata)
self._net_cfg = self.metadata["network"]
return self._net_cfg

def _generate_net_cfg(self, metadata):
netcfg: Dict[str, Any] = {"version": 2, "ethernets": {}}
macs_to_nics = net.get_interfaces_by_mac()
nameservers = metadata["network"].get("nameservers", None)

for iface in metadata["network"]["interfaces"]:
name = macs_to_nics.get(iface["mac_address"])
routes = iface.get("routes", None)
if name is None:
LOG.warning(
"Metadata MAC address %s not found.", iface["mac_address"]
)
continue
netcfg["ethernets"][name] = {
"set-name": name,
"match": {
"macaddress": iface["mac_address"].lower(),
},
"addresses": iface["addresses"],
}

if nameservers is not None:
netcfg["ethernets"][name]["nameservers"] = nameservers
if routes is not None:
netcfg["ethernets"][name]["routes"] = routes

return netcfg


def is_platform_viable() -> bool:
return dmi.read_dmi_data("system-product-name") == CLOUDCIX_DMI_NAME
Expand Down
48 changes: 25 additions & 23 deletions tests/unittests/sources/test_cloudcix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,42 @@
METADATA = {
"instance_id": "12_34",
"network": {
"nameservers": {"addresses": ["10.0.0.1"], "search": ["cloudcix.com"]},
"interfaces": [
{
"mac_address": "ab:cd:ef:00:01:02",
"routes": [{"to": "default", "via": "10.0.0.1"}],
"version": 2,
"ethernets": {
"eth0": {
"set-name": "eth0",
"match": {"macaddress": "ab:cd:ef:00:01:02"},
"addresses": [
"10.0.0.2/24",
"192.168.0.2/24",
],
"nameservers": {
"addresses": ["10.0.0.1"],
"search": ["cloudcix.com"],
},
"routes": [{"to": "default", "via": "10.0.0.1"}],
},
{
"mac_address": "12:34:56:ab:cd:ef",
"eth1": {
"set-name": "eth1",
"match": {"macaddress": "12:34:56:ab:cd:ef"},
"addresses": [
"10.10.10.2/24",
],
"nameservers": {
"addresses": ["10.0.0.1"],
"search": ["cloudcix.com"],
},
},
],
},
},
}

# Expected network config resulting from METADATA
NETWORK_CONFIG = {
"version": 2,
"ethernets": {
"enp1s0": {
"set-name": "enp1s0",
"eth0": {
"set-name": "eth0",
"addresses": [
"10.0.0.2/24",
"192.168.0.2/24",
Expand All @@ -53,8 +63,8 @@
},
"routes": [{"to": "default", "via": "10.0.0.1"}],
},
"enp2s0": {
"set-name": "enp2s0",
"eth1": {
"set-name": "eth1",
"addresses": [
"10.10.10.2/24",
],
Expand Down Expand Up @@ -109,14 +119,6 @@ def setup(self, mocker, tmpdir, paths):
new_callable=mock.PropertyMock,
)
self._m_find_fallback_nic.return_value = "cixnic0"
self._m_get_interfaces_by_mac = mocker.patch(
"cloudinit.net.get_interfaces_by_mac",
new_callable=mock.PropertyMock,
)
self._m_get_interfaces_by_mac.return_value = {
"ab:cd:ef:00:01:02": "enp1s0",
"12:34:56:ab:cd:ef": "enp2s0",
}

def _get_ds(self):
distro_cls = distros.fetch("ubuntu")
Expand Down Expand Up @@ -220,9 +222,9 @@ def test_reading_metadata_on_cloudcix(self):
assert self.datasource.get_data()
assert self.datasource.metadata == METADATA
assert self.datasource.userdata_raw == USERDATA
assert json_dumps(
self.datasource._generate_net_cfg(METADATA)
) == json_dumps(NETWORK_CONFIG)
assert json_dumps(self.datasource.network_config) == json_dumps(
NETWORK_CONFIG
)

@responses.activate
def test_failing_imds_endpoints(self, mocker):
Expand Down

0 comments on commit 88cdc11

Please sign in to comment.