Skip to content

Commit 37a12cd

Browse files
committed
T6686: adds container health checks
1 parent c035c4d commit 37a12cd

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ 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+
def test_healthcheck(self):
118+
cont_name = 'health-test'
119+
120+
self.cli_set(base_path + ['name', cont_name, 'allow-host-networks'])
121+
self.cli_set(base_path + ['name', cont_name, 'image', busybox_image])
122+
123+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'command', 'true'])
124+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'interval', '10s'])
125+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'timeout', '1s'])
126+
self.cli_set(base_path + ['name', cont_name, 'health-check', 'retries', '2'])
127+
self.cli_commit()
128+
129+
l = cmd_to_json(f'sudo podman container inspect {cont_name}')
130+
self.assertEqual(l['HostConfig']['LogConfig']['Type'], 'journald')
131+
self.assertEqual(l['Config']['Healthcheck']['Test'], ['CMD-SHELL', 'true'])
132+
self.assertEqual(l['Config']['Healthcheck']['Interval'], 10000000000)
133+
self.assertEqual(l['Config']['Healthcheck']['Timeout'], 1000000000)
134+
self.assertEqual(l['Config']['Healthcheck']['Retries'], 2)
115135

116136

117137
def test_name_server(self):

src/conf_mode/container.py

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

465+
healthcheck = ''
466+
if 'health_check' in container_config:
467+
if 'command' in container_config['health_check']:
468+
health_cmd = container_config['health_check']['command']
469+
healthcheck += f' --health-cmd="{health_cmd}"'
470+
if 'interval' in container_config['health_check']:
471+
health_int = container_config['health_check']['interval']
472+
healthcheck += f' --health-interval={health_int}'
473+
if 'timeout' in container_config['health_check']:
474+
health_to = container_config['health_check']['timeout']
475+
healthcheck += f' --health-timeout={health_to}'
476+
if 'retries' in container_config['health_check']:
477+
health_rt = container_config['health_check']['retries']
478+
healthcheck += f' --health-retries={health_rt}'
479+
else:
480+
healthcheck = ' --no-healthcheck'
481+
465482
command = ''
466483
if 'command' in container_config:
467484
command = container_config['command'].strip()
@@ -471,7 +488,7 @@ def generate_run_arguments(name, container_config, host_ident):
471488
command_arguments = container_config['arguments'].strip()
472489

473490
if 'allow_host_networks' in container_config:
474-
return f'{container_base_cmd} --net host {entrypoint} {image} {command} {command_arguments}'.strip()
491+
return f'{container_base_cmd} {healthcheck} --net host {entrypoint} {image} {command} {command_arguments}'.strip()
475492

476493
ip_param = ''
477494
addr_info = ''
@@ -489,7 +506,7 @@ def generate_run_arguments(name, container_config, host_ident):
489506

490507
mac_address = f'--mac-address {gen_mac(name, addr_info, host_ident)}'
491508

492-
return f'{container_base_cmd} --no-healthcheck --net {networks} {ip_param} {mac_address} {entrypoint} {image} {command} {command_arguments}'.strip()
509+
return f'{container_base_cmd} {healthcheck} --net {networks} {ip_param} {mac_address} {entrypoint} {image} {command} {command_arguments}'.strip()
493510

494511

495512
def generate(container):

0 commit comments

Comments
 (0)