From ae6d3437dc3247c396444b7f65eb49a98b0dd800 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 2 Nov 2025 09:30:05 +0100 Subject: container: T7305: fix VRF loss when restarting pods Container networks are only started when there is at least one active consumer. If a network is created without any attached containers, it does not need to be assigned to a VRF yet. When the last container in a pod is stopped, its associated container network is removed. Upon container restart, the kernel recreates the network, but the VRF assignment may be lost in the process. This change ensures that all container networks are correctly reattached to their designated VRFs when a pod restarts. --- src/conf_mode/container.py | 25 +++++-------------------- src/op_mode/container.py | 9 +++++++++ 2 files changed, 14 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/conf_mode/container.py b/src/conf_mode/container.py index a57a9d054..af09ac52a 100755 --- a/src/conf_mode/container.py +++ b/src/conf_mode/container.py @@ -29,7 +29,7 @@ from vyos.configdict import dict_merge from vyos.configdict import node_changed from vyos.configdict import is_node_changed from vyos.configverify import verify_vrf -from vyos.ifconfig import Interface +from vyos.container import restart_network from vyos.utils.configfs import delete_cli_node from vyos.utils.configfs import add_cli_node from vyos.utils.cpu import get_core_count @@ -161,7 +161,7 @@ def verify(container): if 'name_server' in container_config and 'no_name_server' not in container['network'][network_name]: raise ConfigError(f'Setting name server has no effect when attached container network has DNS enabled!') - mac = dict_search(f'network.{network_name}.mac', container_config) + mac = dict_search(f'network.{network_name}.mac', container_config) if mac: if mac in net_dict['mac'].keys(): raise ConfigError(f'MAC address "{mac}" is already used by container "{net_dict["mac"][mac]}"!') @@ -194,7 +194,7 @@ def verify(container): if ip_address(address) == ip_network(network)[1]: raise ConfigError(f'IP address "{address}" can not be used for a container, ' \ 'reserved for the container engine!') - + if address in net_dict['address'].keys(): raise ConfigError(f'IP address "{address}" is already used by container "{net_dict["address"][address]}"!') net_dict['address'][address] = name @@ -673,23 +673,8 @@ def apply(container): if disabled_new: call('systemctl daemon-reload') - # Start network and assign it to given VRF if requested. this can only be done - # after the containers got started as the podman network interface will - # only be enabled by the first container and yet I do not know how to enable - # the network interface in advance - if 'network' in container: - for network, network_config in container['network'].items(): - type_config = dict_search('type', network_config) - if not dict_search('macvlan', type_config): - network_name = f'pod-{network}' - # T5147: Networks are started only as soon as there is a consumer. - # If only a network is created in the first place, no need to assign - # it to a VRF as there's no consumer, yet. - if interface_exists(network_name): - tmp = Interface(network_name) - tmp.set_vrf(network_config.get('vrf', '')) - tmp.add_ipv6_eui64_address('fe80::/64') - + # Re-Start network and assign it to given VRF if requested. + restart_network(container) return None diff --git a/src/op_mode/container.py b/src/op_mode/container.py index a34901cb4..281c95e2c 100755 --- a/src/op_mode/container.py +++ b/src/op_mode/container.py @@ -166,6 +166,8 @@ def show_network(raw: bool): def restart(name: str): from vyos.utils.process import rc_cmd + from vyos.config import Config + from vyos.container import restart_network rc, output = rc_cmd(f'systemctl restart vyos-container-{name}.service') if rc != 0: @@ -173,6 +175,13 @@ def restart(name: str): if rc2 != 0: print(output) return None + if rc == 0: + conf = Config() + container = conf.get_config_dict(['container'], key_mangling=('-', '_'), + no_tag_node_value_mangle=True, + get_first_key=True, + with_recursive_defaults=True) + restart_network(container) print(f'Container "{name}" restarted!') return output -- cgit v1.2.3