Skip to content

Commit

Permalink
Add healthcheck start-interval option
Browse files Browse the repository at this point in the history
  • Loading branch information
x4rd0o1Vtx committed May 9, 2024
1 parent 9beac01 commit a33efb3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions plugins/module_utils/module_container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ def _compare_platform(option, param_value, container_value):
interval=dict(type='str'),
timeout=dict(type='str'),
start_period=dict(type='str'),
start_interval=dict(type='str'),
retries=dict(type='int'),
))
)
Expand Down
16 changes: 15 additions & 1 deletion plugins/module_utils/module_container/docker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ def config_value(
min_api_version=None,
preprocess_value=None,
update_parameter=None,
extra_option_minimal_versions=None,
):
def preprocess_value_(module, client, api_version, options, values):
if len(options) != 1:
Expand Down Expand Up @@ -499,6 +500,7 @@ def update_value(module, data, api_version, options, values):
set_value=set_value,
min_api_version=min_api_version,
update_value=update_value,
extra_option_minimal_versions=extra_option_minimal_versions,
)

@classmethod
Expand All @@ -512,6 +514,7 @@ def host_config_value(
min_api_version=None,
preprocess_value=None,
update_parameter=None,
extra_option_minimal_versions=None,
):
def preprocess_value_(module, client, api_version, options, values):
if len(options) != 1:
Expand Down Expand Up @@ -577,6 +580,7 @@ def update_value(module, data, api_version, options, values):
set_value=set_value,
min_api_version=min_api_version,
update_value=update_value,
extra_option_minimal_versions=extra_option_minimal_versions,
)


Expand Down Expand Up @@ -751,6 +755,7 @@ def _preprocess_healthcheck(module, client, api_version, value):
'Interval': value.get('interval'),
'Timeout': value.get('timeout'),
'StartPeriod': value.get('start_period'),
'StartInterval': value.get('start_interval'),
'Retries': value.get('retries'),
})

Expand Down Expand Up @@ -1300,7 +1305,16 @@ def _preprocess_container_names(module, client, api_version, value):
OPTION_GROUPS.add_engine('docker_api', DockerAPIEngine.host_config_value('GroupAdd'))

OPTION_HEALTHCHECK.add_engine('docker_api', DockerAPIEngine.config_value(
'Healthcheck', preprocess_value=_preprocess_healthcheck, postprocess_for_get=_postprocess_healthcheck_get_value))
'Healthcheck',
preprocess_value=_preprocess_healthcheck,
postprocess_for_get=_postprocess_healthcheck_get_value,
extra_option_minimal_versions={
'healthcheck.start_interval': {
'docker_api_version': '1.44',
'detect_usage': lambda c: any(c.module.params['healthcheck']['start_interval'] is not None),
},
},
))

OPTION_HOSTNAME.add_engine('docker_api', DockerAPIEngine.config_value('Hostname'))

Expand Down
4 changes: 2 additions & 2 deletions plugins/module_utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ def normalize_healthcheck(healthcheck, normalize_test=False):
result = dict()

# All supported healthcheck parameters
options = ('test', 'interval', 'timeout', 'start_period', 'retries')
options = ('test', 'interval', 'timeout', 'start_period', 'start_interval', 'retries')

duration_options = ('interval', 'timeout', 'start_period')
duration_options = ('interval', 'timeout', 'start_period', 'start_interval')

for key in options:
if key in healthcheck:
Expand Down
9 changes: 8 additions & 1 deletion plugins/modules/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
- Configure a check that is run to determine whether or not containers for this service are "healthy".
- "See the docs for the L(HEALTHCHECK Dockerfile instruction,https://docs.docker.com/engine/reference/builder/#healthcheck)
for details on how healthchecks work."
- "O(healthcheck.interval), O(healthcheck.timeout) and O(healthcheck.start_period) are specified as durations.
- "O(healthcheck.interval), O(healthcheck.timeout), O(healthcheck.start_period), and O(healthcheck.start_interval) are specified as durations.
They accept duration as a string in a format that look like: V(5h34m56s), V(1m30s), and so on.
The supported units are V(us), V(ms), V(s), V(m) and V(h)."
type: dict
Expand Down Expand Up @@ -399,6 +399,12 @@
- Start period for the container to initialize before starting health-retries countdown.
- The default used by the Docker daemon is V(0s).
type: str
start_interval:
description:
- Time between health checks during the start period. This option requires Docker Engine version 25.0 or later.
- The default used by the Docker daemon is V(5s).
type: str
version_added: 3.10.0
hostname:
description:
- The container's hostname.
Expand Down Expand Up @@ -1196,6 +1202,7 @@
timeout: 10s
retries: 3
start_period: 30s
start_interval: 10s
- name: Remove healthcheck from container
community.docker.docker_container:
Expand Down
23 changes: 22 additions & 1 deletion plugins/modules/docker_swarm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
- Configure a check that is run to determine whether or not containers for this service are "healthy".
See the docs for the L(HEALTHCHECK Dockerfile instruction,https://docs.docker.com/engine/reference/builder/#healthcheck)
for details on how healthchecks work.
- "O(healthcheck.interval), O(healthcheck.timeout), and O(healthcheck.start_period) are specified as durations.
- "O(healthcheck.interval), O(healthcheck.timeout), O(healthcheck.start_period), and O(healthcheck.start_interval) are specified as durations.
They accept duration as a string in a format that look like: V(5h34m56s), V(1m30s), and so on.
The supported units are V(us), V(ms), V(s), V(m) and V(h)."
type: dict
Expand All @@ -174,6 +174,11 @@
description:
- Start period for the container to initialize before starting health-retries countdown.
type: str
start_interval:
description:
- Time between health checks during the start period. This option requires Docker Engine version 25.0 or later.
type: str
version_added: 3.10.0
hostname:
description:
- Container hostname.
Expand Down Expand Up @@ -701,6 +706,7 @@
"interval": 90000000000,
"retries": 3,
"start_period": 30000000000,
"start_interval": 10000000000,
"test": [
"CMD",
"curl",
Expand Down Expand Up @@ -920,6 +926,7 @@
timeout: 10s
retries: 3
start_period: 30s
start_interval: 10s
- name: Configure service resources
community.docker.docker_swarm_service:
Expand Down Expand Up @@ -2185,6 +2192,7 @@ def get_service(self, name):
'Interval': 'interval',
'Timeout': 'timeout',
'StartPeriod': 'start_period',
'StartInterval': 'start_interval',
'Retries': 'retries'
}
healthcheck = dict(
Expand Down Expand Up @@ -2585,6 +2593,12 @@ def _detect_healthcheck_start_period(client):
return False


def _detect_healthcheck_start_interval(client):
if client.module.params['healthcheck']:
return client.module.params['healthcheck']['start_interval'] is not None
return False


def _detect_mount_tmpfs_usage(client):
for mount in client.module.params['mounts'] or []:
if mount.get('type') == 'tmpfs':
Expand Down Expand Up @@ -2684,6 +2698,7 @@ def main():
interval=dict(type='str'),
timeout=dict(type='str'),
start_period=dict(type='str'),
start_interval=dict(type='str'),
retries=dict(type='int'),
)),
hostname=dict(type='str'),
Expand Down Expand Up @@ -2779,6 +2794,12 @@ def main():
detect_usage=_detect_healthcheck_start_period,
usage_msg='set healthcheck.start_period'
),
healthcheck_start_interval=dict(
docker_py_version='2.6.0',
docker_api_version='1.44',
detect_usage=_detect_healthcheck_start_interval,
usage_msg='set healthcheck.start_interval'
),
update_config_max_failure_ratio=dict(
docker_py_version='2.1.0',
detect_usage=lambda c: (c.module.params['update_config'] or {}).get(
Expand Down

0 comments on commit a33efb3

Please sign in to comment.