Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions ocp_resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,13 +1222,23 @@ def watcher(self, timeout: int, resource_version: str = "") -> Generator[dict[st
resource_version=resource_version or self.initial_resource_version,
)

def wait_for_condition(self, condition: str, status: str, timeout: int = 300, sleep_time: int = 1) -> None:
def wait_for_condition(
self,
condition: str,
status: str,
timeout: int = 300,
sleep_time: int = 1,
reason: str | None = None,
message: str = "",
) -> None:
"""
Wait for Resource condition to be in desire status.

Args:
condition (str): Condition to query.
status (str): Expected condition status.
reason (None): Expected condition reason.
message (str): Expected condition text inclusion.
timeout (int): Time to wait for the resource.
sleep_time(int): Interval between each retry when checking the resource's condition.

Expand All @@ -1238,22 +1248,21 @@ def wait_for_condition(self, condition: str, status: str, timeout: int = 300, sl
self.logger.info(f"Wait for {self.kind}/{self.name}'s '{condition}' condition to be '{status}'")

timeout_watcher = TimeoutWatch(timeout=timeout)
for sample in TimeoutSampler(
wait_timeout=timeout,
sleep=sleep_time,
func=lambda: self.exists,
):
if sample:
break

self.wait(timeout=timeout, sleep=sleep_time)
for sample in TimeoutSampler(
wait_timeout=timeout_watcher.remaining_time(),
sleep=sleep_time,
func=lambda: self.instance,
):
if sample:
for cond in sample.get("status", {}).get("conditions", []):
if cond["type"] == condition and cond["status"] == status:
actual_condition = {"type": cond["type"], "status": cond["status"]}
expected_condition = {"type": condition, "status": status}
if reason is not None:
actual_condition["reason"] = cond.get("reason", "")
expected_condition["reason"] = reason

if actual_condition == expected_condition and message in cond.get("message", ""):
return

def api_request(
Expand Down