From ba1a1fe83b360f2f1a4a31e274e85876e4f4373a Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Sat, 30 Sep 2023 10:10:37 -0400 Subject: [PATCH] cephadm: add SidecarContainer type Add a new type representing sidecar containers. Signed-off-by: John Mulligan --- src/cephadm/cephadmlib/container_types.py | 69 +++++++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/src/cephadm/cephadmlib/container_types.py b/src/cephadm/cephadmlib/container_types.py index 7bddecb6516..0bdbb5c80bb 100644 --- a/src/cephadm/cephadmlib/container_types.py +++ b/src/cephadm/cephadmlib/container_types.py @@ -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 -- 2.39.5