Skip to content

Commit c78e2d9

Browse files
authored
feat: added logic to stop waiting early to wait_for_condition method (#2585)
* added stop_condition to wait_for_condition method * changed exception message according to review * combined conditions, updated docstring after review
1 parent cfda4bd commit c78e2d9

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ocp_resources/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,9 @@ def __str__(self) -> str:
8888
if self.path:
8989
return f"Validation error at '{self.path}': {self.message}"
9090
return f"Validation error: {self.message}"
91+
92+
93+
class ConditionError(Exception):
94+
"""Raised when a resource condition reaches an unexpected state."""
95+
96+
pass

ocp_resources/resource.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from ocp_resources.event import Event
4242
from ocp_resources.exceptions import (
4343
ClientWithBasicAuthError,
44+
ConditionError,
4445
MissingRequiredArgumentError,
4546
MissingResourceResError,
4647
ResourceTeardownError,
@@ -1239,6 +1240,8 @@ def wait_for_condition(
12391240
sleep_time: int = 1,
12401241
reason: str | None = None,
12411242
message: str = "",
1243+
stop_condition: str | None = None,
1244+
stop_status: str = "True",
12421245
) -> None:
12431246
"""
12441247
Wait for Resource condition to be in desire status.
@@ -1250,9 +1253,14 @@ def wait_for_condition(
12501253
message (str): Expected condition text inclusion.
12511254
timeout (int): Time to wait for the resource.
12521255
sleep_time(int): Interval between each retry when checking the resource's condition.
1256+
stop_condition (str | None): Condition which should stop the wait and fail.
1257+
Note: Matching for stop_condition only uses condition type and status.
1258+
The reason and message fields are ignored when checking for stop_condition.
1259+
stop_status (str): Status of the stop condition which should stop the wait and fail.
12531260
12541261
Raises:
1255-
TimeoutExpiredError: If Resource condition in not in desire status.
1262+
TimeoutExpiredError: If Resource condition is not in desired status within timeout.
1263+
ConditionError: If the desired condition is not met and stop_condition is detected before timeout.
12561264
"""
12571265
self.logger.info(f"Wait for {self.kind}/{self.name}'s '{condition}' condition to be '{status}'")
12581266

@@ -1266,6 +1274,12 @@ def wait_for_condition(
12661274
if sample:
12671275
for cond in sample.get("status", {}).get("conditions", []):
12681276
actual_condition = {"type": cond["type"], "status": cond["status"]}
1277+
1278+
if stop_condition and actual_condition == {"type": stop_condition, "status": stop_status}:
1279+
raise ConditionError(
1280+
f"{self.kind} {self.name} reached stop_condition '{stop_condition}' in status '{stop_status}':\n{cond}"
1281+
)
1282+
12691283
expected_condition = {"type": condition, "status": status}
12701284
if reason is not None:
12711285
actual_condition["reason"] = cond.get("reason", "")

0 commit comments

Comments
 (0)