Skip to content

Commit

Permalink
continue waiting when an exception is raised (ansible-collections#408)
Browse files Browse the repository at this point in the history
Continue waiting when an exception is raised

SUMMARY
When an exception is raised and the wait_timeout is not reached, we should continue waiting as this may occurs due to temporary issue on cluster

Fixes ansible-collections#407

ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Mike Graves <[email protected]>
Reviewed-by: Abhijeet Kasurde <None>
(cherry picked from commit f418353)
  • Loading branch information
abikouo authored and gravesm committed May 2, 2022
1 parent 321b6dc commit ecf5a76
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/408-fix-wait-on-exception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Catch expectation raised when the process is waiting for resources (https://github.com/ansible-collections/kubernetes.core/issues/407).
39 changes: 26 additions & 13 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,25 @@ def kubernetes_facts(
field_selectors = []

result = None
params = dict(
name=name,
namespace=namespace,
label_selector=",".join(label_selectors),
field_selector=",".join(field_selectors),
)
try:
result = resource.get(
name=name,
namespace=namespace,
label_selector=",".join(label_selectors),
field_selector=",".join(field_selectors),
)
result = resource.get(**params)
except BadRequestError:
return dict(resources=[], api_found=True)
except NotFoundError:
if not wait or name is None:
return dict(resources=[], api_found=True)
except Exception as e:
if not wait or name is None:
err = "Exception '{0}' raised while trying to get resource using {1}".format(
e, params
)
return dict(resources=[], msg=err, api_found=True)

if not wait:
result = result.to_dict()
Expand All @@ -452,21 +459,25 @@ def result_empty(result):
and not result.get("items")
)

last_exception = None
while result_empty(result) and _elapsed() < wait_timeout:
try:
result = resource.get(
name=name,
namespace=namespace,
label_selector=",".join(label_selectors),
field_selector=",".join(field_selectors),
)
result = resource.get(**params)
except NotFoundError:
pass
except Exception as e:
last_exception = e
if not result_empty(result):
break
time.sleep(wait_sleep)
if result_empty(result):
return dict(resources=[], api_found=True)
res = dict(resources=[], api_found=True)
if last_exception is not None:
res["msg"] = (
"Exception '%s' raised while trying to get resource using %s"
% (last_exception, params)
)
return res

if isinstance(result, ResourceInstance):
satisfied_by = []
Expand Down Expand Up @@ -583,6 +594,8 @@ def _wait_for_elapsed():
except NotFoundError:
if state == "absent":
return True, {}, _wait_for_elapsed()
except Exception:
pass
if response:
response = response.to_dict()
return False, response, _wait_for_elapsed()
Expand Down

0 comments on commit ecf5a76

Please sign in to comment.