Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions packages/esxi-netinit/esxi_netinit/esxconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ def configure_management_interface(self, mgmt_portgroup: str):
else:
raise NotImplementedError(f"net type {net.type}")

def configure_vswitch(self, uplink: NIC, switch_name: str, mtu: int):
def configure_vswitch(self, switch_name: str, mtu: int):
"""Sets up vSwitch."""
uplinks: list[NIC] = self.identify_uplinks()

self.host.create_vswitch(switch_name)
self.host.uplink_add(nic=uplink.name, switch_name=switch_name)
for uplink in uplinks:
self.host.uplink_add(nic=uplink.name, switch_name=switch_name)

self.host.vswitch_failover_uplinks(
active_uplinks=[uplink.name], name=switch_name
active_uplinks=[uplink.name for uplink in uplinks], name=switch_name
)

self.host.vswitch_security(name=switch_name)
self.host.vswitch_settings(mtu=mtu, name=switch_name)

Expand All @@ -92,17 +97,15 @@ def configure_requested_dns(self):

return self.host.configure_dns(servers=dns_servers)

def identify_uplink(self) -> NIC:
def identify_uplinks(self) -> list[NIC]:
eligible_networks = [
net for net in self.network_data.networks if net.default_routes()
]
if len(eligible_networks) != 1:
raise ValueError(
"the network_data.json should only contain a single default route."
"Unable to identify uplink interface"
)
link = eligible_networks[0].link
return self.nics.find_by_mac(link.ethernet_mac_address)

return [
self.nics.find_by_mac(n.link.ethernet_mac_address)
for n in eligible_networks
]

@cached_property
def nics(self):
Expand Down
4 changes: 1 addition & 3 deletions packages/esxi-netinit/esxi_netinit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def main(config_dir, dry_run):
esx = ESXConfig(network_data, meta_data, dry_run=dry_run)
esx.configure_hostname()
esx.clean_default_network_setup(OLD_MGMT_PG, OLD_VSWITCH)
esx.configure_vswitch(
uplink=esx.identify_uplink(), switch_name=NEW_VSWITCH, mtu=9000
)
esx.configure_vswitch(switch_name=NEW_VSWITCH, mtu=9000)

# this configures the Management Network to the default vSwitch
esx.configure_portgroups(NEW_VSWITCH, [NEW_MGMT_PG])
Expand Down
54 changes: 54 additions & 0 deletions packages/esxi-netinit/tests/test_esxconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from esxi_netinit.esxhost import ESXHost
from esxi_netinit.meta_data import MetaDataData
from esxi_netinit.network_data import NetworkData
from esxi_netinit.nic import NIC


@pytest.fixture
Expand Down Expand Up @@ -66,3 +67,56 @@ def test_set_host_name(network_data_single, meta_data, host_mock):
ec.host = host_mock
ec.configure_hostname()
host_mock.set_hostname.assert_called_once_with("test.novalocal")


def test_configure_vswitch(mocker, network_data_single, meta_data, host_mock):
ndata = NetworkData(network_data_single)
meta = MetaDataData(meta_data)
ec = ESXConfig(ndata, meta, dry_run=False)
ec.host = host_mock

uplinks = [
NIC(name="vmnic0", status="Up", link="Up", mac="14:23:f3:f5:21:50"),
NIC(name="vmnic1", status="Up", link="Up", mac="14:23:f3:f5:21:51"),
]

mocker.patch.object(ec, "identify_uplinks", return_value=uplinks)

ec.configure_vswitch("vSwitch42", mtu=9000)

host_mock.create_vswitch.assert_called_once_with("vSwitch42")

host_mock.uplink_add.assert_has_calls(
[
mocker.call(nic="vmnic0", switch_name="vSwitch42"),
mocker.call(nic="vmnic1", switch_name="vSwitch42"),
]
)
assert host_mock.uplink_add.call_count == 2

host_mock.vswitch_failover_uplinks.assert_called_once_with(
active_uplinks=["vmnic0", "vmnic1"], name="vSwitch42"
)
host_mock.vswitch_security.assert_called_once_with(name="vSwitch42")
host_mock.vswitch_settings.assert_called_once_with(mtu=9000, name="vSwitch42")


def test_identify_uplinks(network_data_single, meta_data, mocker):
ndata = NetworkData(network_data_single)
meta = MetaDataData(meta_data)

mocker.patch("esxi_netinit.nic_list.NICList.__init__", return_value=None)

ec = ESXConfig(ndata, meta, dry_run=False)

mock_nic = NIC(name="vmnic0", status="Up", link="Up", mac="00:11:22:33:44:55")
ec._nics = [mock_nic]

mock_find_by_mac = mocker.patch.object(
ec.nics, "find_by_mac", return_value=mock_nic
)

uplinks = ec.identify_uplinks()

assert uplinks == [mock_nic]
mock_find_by_mac.assert_called_once_with(mock_nic.mac)