Skip to content

Commit

Permalink
retry_cluster_exceptions return real last exception (#1728)
Browse files Browse the repository at this point in the history
* retry_cluster_exceptions return real last exception

* Use real exception object

* Use 10 sec constant

* exists() >> except NotFoundError

* update timeout-sampler

* update timeout-sampler

* Fix call initial_resource_version
  • Loading branch information
myakove authored Mar 21, 2024
1 parent 630d344 commit 65a4d2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
33 changes: 21 additions & 12 deletions ocp_resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ConflictError,
MethodNotAllowedError,
NotFoundError,
ForbiddenError,
)
from kubernetes.dynamic.resource import ResourceField
from packaging.version import Version
Expand All @@ -26,6 +27,7 @@
PROTOCOL_ERROR_EXCEPTION_DICT,
TIMEOUT_1MINUTE,
TIMEOUT_4MINUTES,
TIMEOUT_10SEC,
)
from ocp_resources.event import Event
from timeout_sampler import (
Expand Down Expand Up @@ -612,7 +614,7 @@ def exists(self):
"""
try:
return self.instance
except TimeoutExpiredError:
except NotFoundError:
return None

def client_wait_deleted(self, timeout):
Expand Down Expand Up @@ -699,7 +701,7 @@ def create(self, wait=False):
self.logger.info(f"Posting {hashed_res}")
self.logger.debug(f"\n{yaml.dump(hashed_res)}")
resource_ = self.api.create(body=self.res, namespace=self.namespace, dry_run=self.dry_run)
with contextlib.suppress(TimeoutExpiredError):
with contextlib.suppress(ForbiddenError, AttributeError):
# some resources do not support get() (no instance) or the client do not have permissions
self.initial_resource_version = self.instance.metadata.resourceVersion

Expand Down Expand Up @@ -765,16 +767,23 @@ def update_replace(self, resource_dict):

@staticmethod
def retry_cluster_exceptions(func, exceptions_dict=DEFAULT_CLUSTER_RETRY_EXCEPTIONS, **kwargs):
sampler = TimeoutSampler(
wait_timeout=10,
sleep=1,
func=func,
print_log=False,
exceptions_dict=exceptions_dict,
**kwargs,
)
for sample in sampler:
return sample
try:
sampler = TimeoutSampler(
wait_timeout=TIMEOUT_10SEC,
sleep=1,
func=func,
print_log=False,
exceptions_dict=exceptions_dict,
**kwargs,
)
for sample in sampler:
return sample

except TimeoutExpiredError as exp:
if exp.last_exp:
raise exp.last_exp

raise

@classmethod
def get(
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65a4d2f

Please sign in to comment.