From: John Mulligan Date: Sun, 5 Nov 2023 18:36:41 +0000 (-0500) Subject: cephadm: add daemon_to_container utility function X-Git-Tag: v19.0.0~24^2~13 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a0e3ff0962c737e6a5321157fcfea0062f60dd1f;p=ceph.git cephadm: add daemon_to_container utility function Add the function daemon_to_container in the container_daemon_form.py function. This function is roughly equivalent to `get_container` in cephadm.py but entirely relies on subclasses implementing ContainerDaemonForm methods and avoids special casing of daemon types. It duplicates the functionality provided by functions in cephadm.py with regards to special podman specific behaviors. I feel that this is a minor issue because due to recent refactoring those functions are now the one- or two-liners that are copied here. I gated them behind arguments `auto_podman_mounts` and `auto_podman_args` that default to true in order to make the function more reusable - however I don't need them that way and if it seems like extra complexity for no win, we can remove them later. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadmlib/container_daemon_form.py b/src/cephadm/cephadmlib/container_daemon_form.py index ae8b6afe3c6..693b0cf8df0 100644 --- a/src/cephadm/cephadmlib/container_daemon_form.py +++ b/src/cephadm/cephadmlib/container_daemon_form.py @@ -4,6 +4,7 @@ import abc from typing import List, Tuple, Optional, Dict +from .container_engines import Podman from .container_types import CephContainer, InitContainer from .context import CephadmContext from .daemon_form import DaemonForm @@ -110,3 +111,67 @@ class ContainerDaemonForm(DaemonForm): in a container. """ return '' + + +def daemon_to_container( + ctx: CephadmContext, + daemon: ContainerDaemonForm, + *, + privileged: bool = False, + ptrace: bool = False, + host_network: bool = True, + entrypoint: Optional[str] = None, + container_args: Optional[List[str]] = None, + container_mounts: Optional[Dict[str, str]] = None, + container_binds: Optional[List[List[str]]] = None, + envs: Optional[List[str]] = None, + args: Optional[List[str]] = None, + auto_podman_args: bool = True, + auto_podman_mounts: bool = True, +) -> CephContainer: + """daemon_to_container is a utility function that serves to create + CephContainer instances from a container daemon form's customize and + entrypoint methods. + Most of the parameters (like mounts, container_args, etc) can be passed in + to "pre customize" the values. + The auto_podman_args argument enables adding default arguments expected on + all podman daemons (true by default). + The auto_podman_mounts argument enables adding mounts expected on all + daemons running on podman (true by default). + """ + container_args = container_args if container_args else [] + container_mounts = container_mounts if container_mounts else {} + container_binds = container_binds if container_binds else [] + envs = envs if envs else [] + args = args if args else [] + + if entrypoint is None: + entrypoint = daemon.default_entrypoint() + daemon.customize_container_args(ctx, container_args) + daemon.customize_container_mounts(ctx, container_mounts) + daemon.customize_container_binds(ctx, container_binds) + daemon.customize_container_envs(ctx, envs) + daemon.customize_process_args(ctx, args) + + _is_podman = isinstance(ctx.container_engine, Podman) + if auto_podman_mounts and _is_podman: + ctx.container_engine.update_mounts(ctx, container_mounts) + if auto_podman_args and _is_podman: + service_name = f'{daemon.identity.unit_name}.service' + container_args.extend( + ctx.container_engine.service_args(ctx, service_name) + ) + + return CephContainer.for_daemon( + ctx, + ident=daemon.identity, + entrypoint=entrypoint, + args=args, + container_args=container_args, + volume_mounts=container_mounts, + bind_mounts=container_binds, + envs=envs, + privileged=privileged, + ptrace=ptrace, + host_network=host_network, + )