Skip to content

Commit 9f27a8f

Browse files
committed
T6686: adds container health checks
1 parent a4b72d3 commit 9f27a8f

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

interface-definitions/container.xml.in

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,52 @@
556556
</properties>
557557
<defaultValue>journald</defaultValue>
558558
</leafNode>
559+
<node name="health-check">
560+
<properties>
561+
<help>Configure health check for the container</help>
562+
</properties>
563+
<children>
564+
<leafNode name="command">
565+
<properties>
566+
<help>Health check command to run for the container</help>
567+
</properties>
568+
</leafNode>
569+
<leafNode name="interval">
570+
<properties>
571+
<help>Interval for the health checks</help>
572+
<completionHelp>
573+
<list>30s disable</list>
574+
</completionHelp>
575+
<valueHelp>
576+
<format>disable</format>
577+
<description>Run health checks manually</description>
578+
</valueHelp>
579+
<constraint>
580+
<regex>^(?:disable|(?:\d+(?:\.\d+)?(?:ms|s|m|h))+)$</regex>
581+
</constraint>
582+
</properties>
583+
</leafNode>
584+
<leafNode name="timeout">
585+
<properties>
586+
<help>Timeout for the health check to complete</help>
587+
<completionHelp>
588+
<list>30s</list>
589+
</completionHelp>
590+
<constraint>
591+
<regex>^(?:\d+(?:\.\d+)?(?:ms|s|m|h))+$</regex>
592+
</constraint>
593+
</properties>
594+
</leafNode>
595+
<leafNode name="retries">
596+
<properties>
597+
<help>The number of retries before container is consider unhealthy</help>
598+
<constraint>
599+
<validator name="numeric" argument="--range 0-255"/>
600+
</constraint>
601+
</properties>
602+
</leafNode>
603+
</children>
604+
</node>
559605
</children>
560606
</tagNode>
561607
<tagNode name="network">

smoketest/scripts/cli/test_container.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ def test_basic(self):
112112

113113
l = cmd_to_json(f'sudo podman container inspect {cont_name}')
114114
self.assertEqual(l['HostConfig']['LogConfig']['Type'], 'journald')
115+
self.assertEqual(l['Config']['Healthcheck']['Test'], ['NONE'])
116+
117+
118+
def test_healthcheck(self):
119+
cont_name = 'health-test'
120+
121+
self.cli_set(base_path + ['name', cont_name, 'allow-host-networks'])
122+
self.cli_set(base_path + ['name', cont_name, 'image', busybox_image])
123+
124+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'command', 'true'])
125+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'interval', '10s'])
126+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'timeout', '1s'])
127+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'retries', '2'])
128+
self.cli_commit()
129+
130+
l = cmd_to_json(f'sudo podman container inspect {cont_name}')
131+
self.assertEqual(l['HostConfig']['LogConfig']['Type'], 'journald')
132+
self.assertEqual(l['Config']['Healthcheck']['Test'], ['CMD-SHELL', 'true'])
133+
self.assertEqual(l['Config']['Healthcheck']['Interval'], 10000000000)
134+
self.assertEqual(l['Config']['Healthcheck']['Timeout'], 1000000000)
135+
self.assertEqual(l['Config']['Healthcheck']['Retries'], 2)
115136

116137

117138
def test_name_server(self):

src/conf_mode/container.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ def generate_run_arguments(name, container_config):
418418
entrypoint = json_write(container_config['entrypoint'].split()).replace('"', "&quot;")
419419
entrypoint = f'--entrypoint &apos;{entrypoint}&apos;'
420420

421+
healthcheck = ''
422+
if 'health_check' in container_config:
423+
if 'command' in container_config['health_check']:
424+
health_cmd = container_config['health_check']['command']
425+
healthcheck += f' --health-cmd="{health_cmd}"'
426+
if 'interval' in container_config['health_check']:
427+
health_int = container_config['health_check']['interval']
428+
healthcheck += f' --health-interval={health_int}'
429+
if 'timeout' in container_config['health_check']:
430+
health_to = container_config['health_check']['timeout']
431+
healthcheck += f' --health-timeout={health_to}'
432+
if 'retries' in container_config['health_check']:
433+
health_rt = container_config['health_check']['retries']
434+
healthcheck += f' --health-retries={health_rt}'
435+
else:
436+
healthcheck = ' --no-healthcheck'
437+
421438
command = ''
422439
if 'command' in container_config:
423440
command = container_config['command'].strip()
@@ -427,7 +444,7 @@ def generate_run_arguments(name, container_config):
427444
command_arguments = container_config['arguments'].strip()
428445

429446
if 'allow_host_networks' in container_config:
430-
return f'{container_base_cmd} --net host {entrypoint} {image} {command} {command_arguments}'.strip()
447+
return f'{container_base_cmd} {healthcheck} --net host {entrypoint} {image} {command} {command_arguments}'.strip()
431448

432449
ip_param = ''
433450
networks = ",".join(container_config['network'])
@@ -440,7 +457,7 @@ def generate_run_arguments(name, container_config):
440457
else:
441458
ip_param += f' --ip {address}'
442459

443-
return f'{container_base_cmd} --no-healthcheck --net {networks} {ip_param} {entrypoint} {image} {command} {command_arguments}'.strip()
460+
return f'{container_base_cmd} {healthcheck} --net {networks} {ip_param} {entrypoint} {image} {command} {command_arguments}'.strip()
444461

445462

446463
def generate(container):

0 commit comments

Comments
 (0)