]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add SidecarContainer type
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 30 Sep 2023 14:10:37 +0000 (10:10 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Tue, 2 Jan 2024 14:30:20 +0000 (09:30 -0500)
Add a new type representing sidecar containers.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/cephadm/cephadmlib/container_types.py

index 7bddecb6516cb51cb19eaa722d0edfe060cd58ea..0bdbb5c80bbe749843a10ca6dca2845e20a7a8dd 100644 (file)
@@ -3,7 +3,7 @@
 import copy
 import os
 
-from typing import Dict, List, Optional, Any, Union, Tuple
+from typing import Dict, List, Optional, Any, Union, Tuple, cast
 
 from .call_wrappers import call, call_throws, CallVerbosity
 from .constants import DEFAULT_TIMEOUT
@@ -71,9 +71,8 @@ class BasicContainer:
         assert self.identity
         return self.identity.container_name
 
-    def build_run_cmd(self) -> List[str]:
-        cmd_args: List[str] = [self._container_engine]
-        cmd_args.append('run')
+    def build_engine_run_args(self) -> List[str]:
+        cmd_args: List[str] = []
         if self.remove:
             cmd_args.append('--rm')
         if self.ipc:
@@ -154,8 +153,14 @@ class BasicContainer:
             + envs
             + vols
             + binds
+        )
+
+    def build_run_cmd(self) -> List[str]:
+        return (
+            [self._container_engine, 'run']
+            + self.build_engine_run_args()
             + [self.image]
-            + self.args
+            + list(self.args)
         )
 
     def build_rm_cmd(
@@ -488,6 +493,60 @@ class InitContainer(BasicContainer):
         return self.build_rm_cmd(storage=storage)
 
 
+class SidecarContainer(BasicContainer):
+    @classmethod
+    def from_primary_and_values(
+        cls,
+        ctx: CephadmContext,
+        primary: BasicContainer,
+        sidecar_name: str,
+        *,
+        image: str = '',
+        entrypoint: str = '',
+        args: Optional[List[str]] = None,
+        init: Optional[bool] = None,
+    ) -> 'SidecarContainer':
+        assert primary.identity
+        identity = DaemonSubIdentity.from_parent(
+            primary.identity, sidecar_name
+        )
+        ctr = cast(
+            SidecarContainer, cls.from_container(primary, ident=identity)
+        )
+        ctr.remove = True
+        if image:
+            ctr.image = image
+        if entrypoint:
+            ctr.entrypoint = entrypoint
+        if args:
+            ctr.args = args
+        if init is not None:
+            ctr.init = init
+        return ctr
+
+    def build_engine_run_args(self) -> List[str]:
+        assert isinstance(self.identity, DaemonSubIdentity)
+        cmd_args = super().build_engine_run_args()
+        if self._using_podman:
+            # sidecar containers are always services, otherwise they
+            # would not be sidecars
+            cmd_args += self.ctx.container_engine.service_args(
+                self.ctx, self.identity.sidecar_service_name
+            )
+        return cmd_args
+
+    def run_cmd(self) -> List[str]:
+        if not (self.envs and self.envs[0].startswith('NODE_NAME=')):
+            self.envs.insert(0, 'NODE_NAME=%s' % get_hostname())
+        return self.build_run_cmd()
+
+    def rm_cmd(self, storage: bool = False) -> List[str]:
+        return self.build_rm_cmd(storage=storage)
+
+    def stop_cmd(self, timeout: Optional[int] = None) -> List[str]:
+        return self.build_stop_cmd(timeout=timeout)
+
+
 def is_container_running(ctx: CephadmContext, c: 'CephContainer') -> bool:
     if ctx.name.split('.', 1)[0] in ['agent', 'cephadm-exporter']:
         # these are non-containerized daemon types