Skip to content

Commit 36ad485

Browse files
authored
Merge pull request ceph#54081 from phlogistonjohn/jjm-cephadm-podman-props
cephadm: move some podman specific logic to Podman methods Reviewed-by: Adam King <[email protected]>
2 parents 6343c51 + 19f5249 commit 36ad485

File tree

4 files changed

+70
-32
lines changed

4 files changed

+70
-32
lines changed

src/cephadm/cephadm.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
CEPH_DEFAULT_PUBKEY,
6060
CEPH_KEYRING,
6161
CEPH_PUBKEY,
62-
CGROUPS_SPLIT_PODMAN_VERSION,
6362
CONTAINER_INIT,
6463
CUSTOM_PS1,
6564
DATA_DIR,
@@ -2802,29 +2801,7 @@ def get_container(
28022801
f'--env-file={sg.conf_file_path}'
28032802
)
28042803

2805-
# if using podman, set -d, --conmon-pidfile & --cidfile flags
2806-
# so service can have Type=Forking
2807-
if isinstance(ctx.container_engine, Podman):
2808-
runtime_dir = '/run'
2809-
service_name = f'{ident.unit_name}.service'
2810-
container_args.extend([
2811-
'-d', '--log-driver', 'journald',
2812-
'--conmon-pidfile',
2813-
f'{runtime_dir}/{service_name}-pid',
2814-
'--cidfile',
2815-
f'{runtime_dir}/{service_name}-cid',
2816-
])
2817-
if ctx.container_engine.version >= CGROUPS_SPLIT_PODMAN_VERSION and not ctx.no_cgroups_split:
2818-
container_args.append('--cgroups=split')
2819-
# if /etc/hosts doesn't exist, we can be confident
2820-
# users aren't using it for host name resolution
2821-
# and adding --no-hosts avoids bugs created in certain daemons
2822-
# by modifications podman makes to /etc/hosts
2823-
# https://tracker.ceph.com/issues/58532
2824-
# https://tracker.ceph.com/issues/57018
2825-
if not os.path.exists('/etc/hosts'):
2826-
container_args.extend(['--no-hosts'])
2827-
2804+
_update_container_args_for_podman(ctx, ident, container_args)
28282805
return CephContainer.for_daemon(
28292806
ctx,
28302807
ident=ident,
@@ -2840,6 +2817,17 @@ def get_container(
28402817
)
28412818

28422819

2820+
def _update_container_args_for_podman(
2821+
ctx: CephadmContext, ident: DaemonIdentity, container_args: List[str]
2822+
) -> None:
2823+
if not isinstance(ctx.container_engine, Podman):
2824+
return
2825+
service_name = f'{ident.unit_name}.service'
2826+
container_args.extend(
2827+
ctx.container_engine.service_args(ctx, service_name)
2828+
)
2829+
2830+
28432831
def extract_uid_gid(ctx, img='', file_path='/var/lib/ceph'):
28442832
# type: (CephadmContext, str, Union[str, List[str]]) -> Tuple[int, int]
28452833

@@ -3406,7 +3394,7 @@ def get_unit_file(ctx, fsid):
34063394
'ExecStopPost=-/bin/rm -f %t/%n-pid %t/%n-cid\n'
34073395
'Type=forking\n'
34083396
'PIDFile=%t/%n-pid\n')
3409-
if ctx.container_engine.version >= CGROUPS_SPLIT_PODMAN_VERSION:
3397+
if ctx.container_engine.supports_split_cgroups:
34103398
extra_args += 'Delegate=yes\n'
34113399

34123400
docker = isinstance(ctx.container_engine, Docker)

src/cephadm/cephadmlib/container_engines.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from .call_wrappers import call_throws, CallVerbosity
88
from .context import CephadmContext
99
from .container_engine_base import ContainerEngine
10-
from .constants import DEFAULT_MODE, MIN_PODMAN_VERSION
10+
from .constants import (
11+
CGROUPS_SPLIT_PODMAN_VERSION,
12+
DEFAULT_MODE,
13+
MIN_PODMAN_VERSION,
14+
)
1115
from .exceptions import Error
1216

1317

@@ -36,6 +40,44 @@ def __str__(self) -> str:
3640
version = '.'.join(map(str, self.version))
3741
return f'{self.EXE} ({self.path}) version {version}'
3842

43+
@property
44+
def supports_split_cgroups(self) -> bool:
45+
"""Return true if this version of podman supports split cgroups."""
46+
return self.version >= CGROUPS_SPLIT_PODMAN_VERSION
47+
48+
def service_args(
49+
self, ctx: CephadmContext, service_name: str
50+
) -> List[str]:
51+
"""Return a list of arguments that should be added to the engine's run
52+
command when starting a long-term service (aka daemon) container.
53+
"""
54+
args = []
55+
# if using podman, set -d, --conmon-pidfile & --cidfile flags
56+
# so service can have Type=Forking
57+
runtime_dir = '/run'
58+
args.extend(
59+
[
60+
'-d',
61+
'--log-driver',
62+
'journald',
63+
'--conmon-pidfile',
64+
f'{runtime_dir}/{service_name}-pid',
65+
'--cidfile',
66+
f'{runtime_dir}/{service_name}-cid',
67+
]
68+
)
69+
if self.supports_split_cgroups and not ctx.no_cgroups_split:
70+
args.append('--cgroups=split')
71+
# if /etc/hosts doesn't exist, we can be confident
72+
# users aren't using it for host name resolution
73+
# and adding --no-hosts avoids bugs created in certain daemons
74+
# by modifications podman makes to /etc/hosts
75+
# https://tracker.ceph.com/issues/58532
76+
# https://tracker.ceph.com/issues/57018
77+
if not os.path.exists('/etc/hosts'):
78+
args.append('--no-hosts')
79+
return args
80+
3981

4082
class Docker(ContainerEngine):
4183
EXE = 'docker'

src/cephadm/cephadmlib/context_getters.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from typing import Any, Dict, List, Optional, Tuple, Union
88

9-
from .constants import CGROUPS_SPLIT_PODMAN_VERSION
109
from .container_engines import Podman
1110
from .context import CephadmContext
1211
from .exceptions import Error
@@ -186,5 +185,5 @@ def should_log_to_journald(ctx: CephadmContext) -> bool:
186185
return ctx.log_to_journald
187186
return (
188187
isinstance(ctx.container_engine, Podman)
189-
and ctx.container_engine.version >= CGROUPS_SPLIT_PODMAN_VERSION
188+
and ctx.container_engine.supports_split_cgroups
190189
)

src/cephadm/tests/fixtures.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@ def import_cephadm():
1717

1818

1919
def mock_docker():
20-
_cephadm = import_cephadm()
21-
docker = mock.Mock(_cephadm.Docker)
20+
from cephadmlib.container_engines import Docker
21+
22+
docker = mock.Mock(Docker)
2223
docker.path = '/usr/bin/docker'
2324
return docker
2425

2526

2627
def mock_podman():
27-
_cephadm = import_cephadm()
28-
podman = mock.Mock(_cephadm.Podman)
28+
from cephadmlib.container_engines import Podman
29+
30+
podman = mock.Mock(Podman)
2931
podman.path = '/usr/bin/podman'
3032
podman.version = (2, 1, 0)
33+
# This next little bit of black magic was adapated from the mock docs for
34+
# PropertyMock. We don't use a PropertyMock but the suggestion to call
35+
# type(...) from the doc allows us to "borrow" the real
36+
# supports_split_cgroups attribute:
37+
# https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock
38+
type(podman).supports_split_cgroups = Podman.supports_split_cgroups
39+
type(podman).service_args = Podman.service_args
3140
return podman
3241

3342

0 commit comments

Comments
 (0)