Add wait_sleep parameter for the k8s module (#59714) (#59854)

pull/60204/head
Marat Sharafutdinov 5 years ago committed by Will Thames
parent 8c0ec5fd82
commit 8751319820

@ -0,0 +1,2 @@
minor_changes:
- k8s - add `wait_sleep` parameter (number of seconds to sleep between checks).

@ -78,6 +78,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
argument_spec.update(copy.deepcopy(AUTH_ARG_SPEC)) argument_spec.update(copy.deepcopy(AUTH_ARG_SPEC))
argument_spec['merge_type'] = dict(type='list', choices=['json', 'merge', 'strategic-merge']) argument_spec['merge_type'] = dict(type='list', choices=['json', 'merge', 'strategic-merge'])
argument_spec['wait'] = dict(type='bool', default=False) argument_spec['wait'] = dict(type='bool', default=False)
argument_spec['wait_sleep'] = dict(type='int', default=5)
argument_spec['wait_timeout'] = dict(type='int', default=120) argument_spec['wait_timeout'] = dict(type='int', default=120)
argument_spec['wait_condition'] = dict(type='dict', default=None, options=self.condition_spec) argument_spec['wait_condition'] = dict(type='dict', default=None, options=self.condition_spec)
argument_spec['validate'] = dict(type='dict', default=None, options=self.validate_spec) argument_spec['validate'] = dict(type='dict', default=None, options=self.validate_spec)
@ -226,6 +227,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
namespace = definition['metadata'].get('namespace') namespace = definition['metadata'].get('namespace')
existing = None existing = None
wait = self.params.get('wait') wait = self.params.get('wait')
wait_sleep = self.params.get('wait_sleep')
wait_timeout = self.params.get('wait_timeout') wait_timeout = self.params.get('wait_timeout')
wait_condition = None wait_condition = None
if self.params.get('wait_condition') and self.params['wait_condition'].get('type'): if self.params.get('wait_condition') and self.params['wait_condition'].get('type'):
@ -274,7 +276,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
self.fail_json(msg="Failed to delete object: {0}".format(exc.body), self.fail_json(msg="Failed to delete object: {0}".format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason) error=exc.status, status=exc.status, reason=exc.reason)
if wait: if wait:
success, resource, duration = self.wait(resource, definition, wait_timeout, 'absent') success, resource, duration = self.wait(resource, definition, wait_sleep, wait_timeout, 'absent')
result['duration'] = duration result['duration'] = duration
if not success: if not success:
self.fail_json(msg="Resource deletion timed out", **result) self.fail_json(msg="Resource deletion timed out", **result)
@ -294,7 +296,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
success = True success = True
result['result'] = k8s_obj result['result'] = k8s_obj
if wait: if wait:
success, result['result'], result['duration'] = self.wait(resource, definition, wait_timeout) success, result['result'], result['duration'] = self.wait(resource, definition, wait_sleep, wait_timeout)
if existing: if existing:
existing = existing.to_dict() existing = existing.to_dict()
else: else:
@ -328,7 +330,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
success = True success = True
result['result'] = k8s_obj result['result'] = k8s_obj
if wait and not self.check_mode: if wait and not self.check_mode:
success, result['result'], result['duration'] = self.wait(resource, definition, wait_timeout, condition=wait_condition) success, result['result'], result['duration'] = self.wait(resource, definition, wait_sleep, wait_timeout, condition=wait_condition)
result['changed'] = True result['changed'] = True
result['method'] = 'create' result['method'] = 'create'
if not success: if not success:
@ -353,7 +355,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
success = True success = True
result['result'] = k8s_obj result['result'] = k8s_obj
if wait: if wait:
success, result['result'], result['duration'] = self.wait(resource, definition, wait_timeout, condition=wait_condition) success, result['result'], result['duration'] = self.wait(resource, definition, wait_sleep, wait_timeout, condition=wait_condition)
match, diffs = self.diff_objects(existing.to_dict(), result['result']) match, diffs = self.diff_objects(existing.to_dict(), result['result'])
result['changed'] = not match result['changed'] = not match
result['method'] = 'replace' result['method'] = 'replace'
@ -381,7 +383,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
success = True success = True
result['result'] = k8s_obj result['result'] = k8s_obj
if wait: if wait:
success, result['result'], result['duration'] = self.wait(resource, definition, wait_timeout, condition=wait_condition) success, result['result'], result['duration'] = self.wait(resource, definition, wait_sleep, wait_timeout, condition=wait_condition)
match, diffs = self.diff_objects(existing.to_dict(), result['result']) match, diffs = self.diff_objects(existing.to_dict(), result['result'])
result['changed'] = not match result['changed'] = not match
result['method'] = 'patch' result['method'] = 'patch'
@ -422,7 +424,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
result['method'] = 'create' result['method'] = 'create'
return result return result
def _wait_for(self, resource, name, namespace, predicate, timeout, state): def _wait_for(self, resource, name, namespace, predicate, sleep, timeout, state):
start = datetime.now() start = datetime.now()
def _wait_for_elapsed(): def _wait_for_elapsed():
@ -437,7 +439,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
return True, response.to_dict(), _wait_for_elapsed() return True, response.to_dict(), _wait_for_elapsed()
else: else:
return True, {}, _wait_for_elapsed() return True, {}, _wait_for_elapsed()
time.sleep(timeout // 20) time.sleep(sleep)
except NotFoundError: except NotFoundError:
if state == 'absent': if state == 'absent':
return True, {}, _wait_for_elapsed() return True, {}, _wait_for_elapsed()
@ -445,7 +447,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
response = response.to_dict() response = response.to_dict()
return False, response, _wait_for_elapsed() return False, response, _wait_for_elapsed()
def wait(self, resource, definition, timeout, state='present', condition=None): def wait(self, resource, definition, sleep, timeout, state='present', condition=None):
def _deployment_ready(deployment): def _deployment_ready(deployment):
# FIXME: frustratingly bool(deployment.status) is True even if status is empty # FIXME: frustratingly bool(deployment.status) is True even if status is empty
@ -500,4 +502,4 @@ class KubernetesRawModule(KubernetesAnsibleModule):
predicate = _custom_condition predicate = _custom_condition
else: else:
predicate = _resource_absent predicate = _resource_absent
return self._wait_for(resource, definition['metadata']['name'], definition['metadata'].get('namespace'), predicate, timeout, state) return self._wait_for(resource, definition['metadata']['name'], definition['metadata'].get('namespace'), predicate, sleep, timeout, state)

@ -76,6 +76,11 @@ options:
default: no default: no
type: bool type: bool
version_added: "2.8" version_added: "2.8"
wait_sleep:
description:
- Number of seconds to sleep between checks.
default: 5
version_added: "2.9"
wait_timeout: wait_timeout:
description: description:
- How long in seconds to wait for the resource to end up in the desired state. Ignored if C(wait) is not set. - How long in seconds to wait for the resource to end up in the desired state. Ignored if C(wait) is not set.

@ -53,6 +53,7 @@
app: "{{ k8s_pod_name }}" app: "{{ k8s_pod_name }}"
template: "{{ k8s_pod_template }}" template: "{{ k8s_pod_template }}"
wait: yes wait: yes
wait_sleep: 3
wait_timeout: 180 wait_timeout: 180
vars: vars:
k8s_pod_name: wait-ds k8s_pod_name: wait-ds
@ -80,6 +81,7 @@
type: RollingUpdate type: RollingUpdate
template: "{{ k8s_pod_template }}" template: "{{ k8s_pod_template }}"
wait: yes wait: yes
wait_sleep: 3
wait_timeout: 180 wait_timeout: 180
vars: vars:
k8s_pod_name: wait-ds k8s_pod_name: wait-ds
@ -107,6 +109,7 @@
type: RollingUpdate type: RollingUpdate
template: "{{ k8s_pod_template }}" template: "{{ k8s_pod_template }}"
wait: yes wait: yes
wait_sleep: 3
wait_timeout: 180 wait_timeout: 180
vars: vars:
k8s_pod_name: wait-ds k8s_pod_name: wait-ds
@ -138,6 +141,7 @@
namespace: "{{ wait_namespace }}" namespace: "{{ wait_namespace }}"
spec: "{{ k8s_pod_spec }}" spec: "{{ k8s_pod_spec }}"
wait: yes wait: yes
wait_sleep: 1
wait_timeout: 30 wait_timeout: 30
vars: vars:
k8s_pod_name: wait-crash-pod k8s_pod_name: wait-crash-pod
@ -162,6 +166,7 @@
namespace: "{{ wait_namespace }}" namespace: "{{ wait_namespace }}"
spec: "{{ k8s_pod_spec }}" spec: "{{ k8s_pod_spec }}"
wait: yes wait: yes
wait_sleep: 1
wait_timeout: 30 wait_timeout: 30
vars: vars:
k8s_pod_name: wait-no-image-pod k8s_pod_name: wait-no-image-pod
@ -330,6 +335,7 @@
namespace: "{{ wait_namespace }}" namespace: "{{ wait_namespace }}"
state: absent state: absent
wait: yes wait: yes
wait_sleep: 2
wait_timeout: 5 wait_timeout: 5
ignore_errors: yes ignore_errors: yes
register: short_wait_remove_pod register: short_wait_remove_pod

Loading…
Cancel
Save