]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add daemon_to_container utility function
authorJohn Mulligan <jmulligan@redhat.com>
Sun, 5 Nov 2023 18:36:41 +0000 (13:36 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Fri, 17 Nov 2023 13:10:11 +0000 (08:10 -0500)
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 <jmulligan@redhat.com>
src/cephadm/cephadmlib/container_daemon_form.py

index ae8b6afe3c63d470247c0e326dc0d3bd02600aa4..693b0cf8df0bc0d43b9b27cf53785d88d5958afc 100644 (file)
@@ -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,
+    )