-
Notifications
You must be signed in to change notification settings - Fork 71
net: NAD live update VM info test #4962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
252125a
net, libs: extend cloudinit UserData with runcmd
azhivovk 7224f07
net, libs: Add set_networks to BaseVirtualMachine
azhivovk abb0db2
net, libs: Add wait for condition helpers
azhivovk 735f22e
net, l2_bridge: Move LINUX_BRIDGE_IFACE_NAME constants to libl2bridge
azhivovk 2d48c5d
net: NAD live-update metadata preservation test
azhivovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| from collections.abc import Generator | ||
|
azhivovk marked this conversation as resolved.
|
||
|
|
||
| import pytest | ||
| from kubernetes.dynamic import DynamicClient | ||
| from ocp_resources.namespace import Namespace | ||
|
|
||
| from libs.net.ip import random_cidr_addresses_by_family | ||
| from libs.net.netattachdef import CNIPluginBridgeConfig, NetConfig, NetworkAttachmentDefinition | ||
| from libs.net.vmspec import wait_for_ifaces_status | ||
| from libs.vm.vm import BaseVirtualMachine | ||
| from tests.network.l2_bridge.libl2bridge import LINUX_BRIDGE_IFACE_NAME_1, LINUX_BRIDGE_IFACE_NAME_2 | ||
| from tests.network.l2_bridge.nad_ref_change.lib_helpers import ( | ||
| NET_SEED, | ||
| two_secondary_bridge_vm, | ||
| ) | ||
| from tests.network.libs import nodenetworkconfigurationpolicy as libnncp | ||
| from tests.network.libs.connectivity import ARP_ISOLATION_SYSCTL_CMD | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def bridge_nad_a( | ||
| admin_client: DynamicClient, | ||
| namespace: Namespace, | ||
| bridge_nncp: libnncp.NodeNetworkConfigurationPolicy, | ||
| vlan_index_number: Generator[int], | ||
| ) -> Generator[NetworkAttachmentDefinition]: | ||
| bridge = bridge_nncp.desired_state_spec.interfaces[0].name # type: ignore | ||
|
azhivovk marked this conversation as resolved.
|
||
| with NetworkAttachmentDefinition( | ||
| name="nad-vlan-a", | ||
| namespace=namespace.name, | ||
| config=NetConfig( | ||
| name="nad-vlan-a", plugins=[CNIPluginBridgeConfig(bridge=bridge, vlan=next(vlan_index_number))] | ||
| ), | ||
| client=admin_client, | ||
| ) as nad: | ||
| yield nad | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def bridge_nad_b( | ||
| admin_client: DynamicClient, | ||
| namespace: Namespace, | ||
| bridge_nncp: libnncp.NodeNetworkConfigurationPolicy, | ||
| vlan_index_number: Generator[int], | ||
| ) -> Generator[NetworkAttachmentDefinition]: | ||
| bridge = bridge_nncp.desired_state_spec.interfaces[0].name # type: ignore[union-attr, index] | ||
| with NetworkAttachmentDefinition( | ||
| name="nad-vlan-b", | ||
| namespace=namespace.name, | ||
| config=NetConfig( | ||
| name="nad-vlan-b", plugins=[CNIPluginBridgeConfig(bridge=bridge, vlan=next(vlan_index_number))] | ||
| ), | ||
| client=admin_client, | ||
| ) as nad: | ||
| yield nad | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class") | ||
| def under_test_vm_two_ifaces( | ||
| namespace: Namespace, | ||
| unprivileged_client: DynamicClient, | ||
| bridge_nad_a: NetworkAttachmentDefinition, | ||
| ) -> Generator[BaseVirtualMachine]: | ||
| iface_a_ips = random_cidr_addresses_by_family(net_seed=NET_SEED, host_address=3) | ||
| iface_b_ips = random_cidr_addresses_by_family(net_seed=NET_SEED, host_address=4) | ||
| with two_secondary_bridge_vm( | ||
| namespace=namespace.name, | ||
| name="under-test-vm-two-ifaces", | ||
| client=unprivileged_client, | ||
| nad_names=[bridge_nad_a.name, bridge_nad_a.name], | ||
| ip_addresses=[iface_a_ips, iface_b_ips], | ||
| iface_names=[LINUX_BRIDGE_IFACE_NAME_1, LINUX_BRIDGE_IFACE_NAME_2], | ||
| runcmd=ARP_ISOLATION_SYSCTL_CMD, | ||
| ) as vm: | ||
| vm.start(wait=True) | ||
| vm.wait_for_agent_connected() | ||
| wait_for_ifaces_status( | ||
| vm=vm, | ||
| ip_addresses_by_spec_net_name={ | ||
| LINUX_BRIDGE_IFACE_NAME_1: [addr.split("/")[0] for addr in iface_a_ips], | ||
| LINUX_BRIDGE_IFACE_NAME_2: [addr.split("/")[0] for addr in iface_b_ips], | ||
| }, | ||
| ) | ||
| yield vm | ||
|
azhivovk marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from copy import deepcopy | ||
|
coderabbitai[bot] marked this conversation as resolved.
azhivovk marked this conversation as resolved.
|
||
| from typing import Final | ||
|
|
||
| from kubernetes.dynamic import DynamicClient | ||
|
|
||
| from libs.net.vmspec import wait_for_no_vmi_condition, wait_for_vmi_condition_status | ||
| from libs.vm.factory import base_vmspec, fedora_vm | ||
| from libs.vm.spec import ( | ||
| CloudInitNoCloud, | ||
| Devices, | ||
| Interface, | ||
| Multus, | ||
| Network, | ||
| ) | ||
| from libs.vm.vm import BaseVirtualMachine, add_volume_disk, cloudinitdisk_storage | ||
| from tests.network.libs import cloudinit | ||
| from tests.network.libs.cloudinit import primary_iface_cloud_init | ||
|
|
||
| NET_SEED: Final[int] = 0 | ||
|
|
||
|
|
||
| GUEST_IFACE_1: Final[str] = "eth1" | ||
| GUEST_IFACE_2: Final[str] = "eth2" | ||
|
azhivovk marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def update_nad_references(vm: BaseVirtualMachine, nad_name_by_net: dict[str, str]) -> None: | ||
|
azhivovk marked this conversation as resolved.
|
||
| """Update secondary network NAD references and wait for the change to be fully applied. | ||
|
|
||
| Patches the VM spec atomically, then waits for the MigrationRequired condition to | ||
| appear (change detected) and disappear (migration completed). | ||
|
|
||
| Args: | ||
| vm: The virtual machine to update. | ||
| nad_name_by_net: Mapping of interface name to new NAD name. | ||
| """ | ||
| resource_version = vm.vmi.instance.metadata.resourceVersion | ||
| networks = deepcopy(vm.template_spec.networks) or [] | ||
| for network in networks: | ||
| if network.name in nad_name_by_net and network.multus: | ||
| network.multus.networkName = nad_name_by_net[network.name] | ||
| vm.set_networks(networks=networks) | ||
|
azhivovk marked this conversation as resolved.
|
||
| wait_for_vmi_condition_status(vm=vm, condition="MigrationRequired", resource_version=resource_version) | ||
| wait_for_no_vmi_condition(vm=vm, condition="MigrationRequired") | ||
|
|
||
|
|
||
| def two_secondary_bridge_vm( | ||
|
azhivovk marked this conversation as resolved.
|
||
| namespace: str, | ||
| name: str, | ||
| client: DynamicClient, | ||
| nad_names: list[str], | ||
| ip_addresses: list[list[str]], | ||
| iface_names: list[str], | ||
| runcmd: list[str] | None = None, | ||
| ) -> BaseVirtualMachine: | ||
| """Create a Fedora VM with a masquerade primary interface and bridge-bound secondary interfaces. | ||
|
|
||
| Interface layout in guest OS: | ||
| eth0 = masquerade (pod network, primary — handles default route and IPv6) | ||
| eth1 = first secondary bridge interface | ||
| eth2 = second secondary bridge interface (if present) | ||
|
|
||
| Args: | ||
| namespace: Namespace to deploy the VM in. | ||
| name: VM name. | ||
| client: Kubernetes dynamic client. | ||
| nad_names: NAD names (multus networkName) for the secondary interfaces, in spec order. | ||
| ip_addresses: Per-interface CIDR address lists, aligned with nad_names. | ||
|
azhivovk marked this conversation as resolved.
|
||
| Each inner list contains one address per supported IP family. | ||
| iface_names: Logical interface names for the VM spec, aligned with nad_names. | ||
| runcmd: Commands to run on first boot via cloud-init runcmd. None means no extra commands. | ||
| """ | ||
| spec = base_vmspec() | ||
| spec.template.spec.domain.devices = Devices( | ||
| interfaces=[ | ||
| Interface(name="default", masquerade={}), | ||
| *[Interface(name=iface_name, bridge={}) for iface_name in iface_names], | ||
| ] | ||
| ) | ||
| spec.template.spec.networks = [ | ||
| Network(name="default", pod={}), | ||
| *[ | ||
| Network(name=iface_name, multus=Multus(networkName=nad_name)) | ||
| for iface_name, nad_name in zip(iface_names, nad_names) | ||
|
azhivovk marked this conversation as resolved.
|
||
| ], | ||
|
azhivovk marked this conversation as resolved.
|
||
| ] | ||
| ethernets = {} | ||
| if primary := primary_iface_cloud_init(): | ||
| ethernets["eth0"] = primary | ||
| for i, addresses in enumerate(ip_addresses): | ||
| ethernets[f"eth{i + 1}"] = cloudinit.EthernetDevice(addresses=addresses) | ||
|
azhivovk marked this conversation as resolved.
|
||
| userdata = cloudinit.UserData(users=[], runcmd=runcmd) | ||
| disk, volume = cloudinitdisk_storage( | ||
| data=CloudInitNoCloud( | ||
| networkData=cloudinit.asyaml(no_cloud=cloudinit.NetworkData(ethernets=ethernets)) if ethernets else "", | ||
| userData=cloudinit.format_cloud_config(userdata=userdata), | ||
| ) | ||
| ) | ||
| spec.template.spec = add_volume_disk(vmi_spec=spec.template.spec, volume=volume, disk=disk) | ||
| return fedora_vm(namespace=namespace, name=name, client=client, spec=spec) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.