Skip to content

Commit

Permalink
ContainerPoolManager: add netavark support
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Tluka <[email protected]>
  • Loading branch information
jtluka committed Sep 16, 2024
1 parent 6e394f9 commit 411c868
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions lnst/Controller/ContainerPoolManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import socket
from time import sleep
from json import loads
from typing import Optional
from lnst.Controller.AgentPoolManager import PoolManagerError
from lnst.Controller.Machine import Machine
from lnst.Common.DependencyError import DependencyError
Expand Down Expand Up @@ -38,10 +39,15 @@ class ContainerPoolManager(object):
:param image:
Mandatory parameter
:type image: str
:param network_plugin:
Podman network plugin, 'cni' or 'netavark', if unset, the network backend is auto-detected
:type network_plugin: Optional[str]
"""

def __init__(
self, pools, msg_dispatcher, ctl_config, podman_uri, image, pool_checks=True
self, pools, msg_dispatcher, ctl_config, podman_uri, image,
network_plugin='netavark', pool_checks=True
):
self._import_optionals()
self._pool = {}
Expand All @@ -54,6 +60,7 @@ def __init__(
self._image = ""
self._podman_connect(podman_uri)
self.image = image
self.network_plugin = network_plugin

self._networks = {}
self._network_prefix = "lnst_container_net_"
Expand Down Expand Up @@ -207,7 +214,21 @@ def _create_container(self, name: str, reqs: dict):

return container, machine

def _create_network(self, network_name: str):
def _create_network(self, network_name: str, plugin: Optional[str]):
if not plugin:
podman_info = self._podman_client.info()
plugin = podman_info["host"]["networkBackend"]

logging.debug(f"Using {plugin} network backend for containers")

if plugin == "netavark":
return self._create_network_netavark(network_name)
elif plugin == "cni":
return self._create_network_cni(network_name)
else:
raise PoolManagerError(f"Unknown podman network plugin {plugin}")

def _create_network_cni(self, network_name: str):
"""Networks are created "manually" because podman does not
support creating L2 [1] networks. IPs in these networks are managed
by controller.
Expand Down Expand Up @@ -249,6 +270,28 @@ def _create_network(self, network_name: str):

return network

def _create_network_netavark(self, network_name: str):
name = self.get_network_name(network_name)
if name in self._networks:
return self._networks[name]

logging.info(f"Creating network {name}")
try:
self._podman_client.networks.create(
name,
internal=True,
enable_ipv6=True,
)
network = self._podman_client.networks.get(name)
except APIError as e:
raise PoolManagerError(f"Could not create network {name}: {e}")
except IOError as e:
raise PoolManagerError(f"Could not create network configuration file: {e}")

self._networks[name] = network

return network

def _connect_to_network(self, container: "Container", network: "Network"):
"""There is no way to get MAC address of remote interface except
executing "ip l" inside container.
Expand Down Expand Up @@ -292,7 +335,7 @@ def _connect_to_networks(self, container: "Container", network_reqs: dict):
name = params["network"]
logging.debug(f"Connecting {container.name} to {name}")

network = self._create_network(name)
network = self._create_network(name, self.network_plugin)

self._connect_to_network(container, network)

Expand Down

0 comments on commit 411c868

Please sign in to comment.