Skip to content

Commit e63cae2

Browse files
authored
Merge pull request #177 from hydrogenbond007/fix/openebs-ready-succeeded-pods
Handle succeeded pods in readiness wait
2 parents 0e8d1fb + 5f6e5e4 commit e63cae2

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

aiopslab/service/kubectl.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ def get_deployment(self, name: str, namespace: str):
8686
"""Fetch the deployment configuration."""
8787
return self.apps_v1_api.read_namespaced_deployment(name, namespace)
8888

89+
@staticmethod
90+
def _pod_is_ready_or_succeeded(pod):
91+
"""Return True when a pod is ready or has completed successfully."""
92+
status = getattr(pod, "status", None)
93+
if getattr(status, "phase", None) == "Succeeded":
94+
return True
95+
96+
container_statuses = getattr(status, "container_statuses", None)
97+
return bool(container_statuses) and all(
98+
getattr(container_status, "ready", False)
99+
for container_status in container_statuses
100+
)
101+
89102
def wait_for_ready(self, namespace, sleep=2, max_wait=300):
90103
"""Wait for all pods in a namespace to be in a Ready state before proceeding."""
91104

@@ -102,8 +115,7 @@ def wait_for_ready(self, namespace, sleep=2, max_wait=300):
102115
if pod_list.items:
103116
ready_pods = [
104117
pod for pod in pod_list.items
105-
if pod.status.container_statuses and
106-
all(cs.ready for cs in pod.status.container_statuses)
118+
if self._pod_is_ready_or_succeeded(pod)
107119
]
108120

109121
if len(ready_pods) == len(pod_list.items):

tests/service/test_kubectl.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from types import SimpleNamespace
2+
import unittest
3+
4+
from aiopslab.service.kubectl import KubeCtl
5+
6+
7+
def _pod(phase, ready_values=None):
8+
container_statuses = None
9+
if ready_values is not None:
10+
container_statuses = [
11+
SimpleNamespace(ready=ready) for ready in ready_values
12+
]
13+
14+
return SimpleNamespace(
15+
status=SimpleNamespace(
16+
phase=phase,
17+
container_statuses=container_statuses,
18+
)
19+
)
20+
21+
22+
class PodReadinessTest(unittest.TestCase):
23+
24+
def test_running_pod_with_ready_containers_satisfies_readiness(self):
25+
pod = _pod("Running", [True, True])
26+
27+
self.assertTrue(KubeCtl._pod_is_ready_or_succeeded(pod))
28+
29+
def test_succeeded_cleanup_pod_satisfies_readiness(self):
30+
pod = _pod("Succeeded", [False])
31+
32+
self.assertTrue(KubeCtl._pod_is_ready_or_succeeded(pod))
33+
34+
def test_running_pod_with_unready_container_blocks_readiness(self):
35+
pod = _pod("Running", [True, False])
36+
37+
self.assertFalse(KubeCtl._pod_is_ready_or_succeeded(pod))
38+
39+
def test_pending_pod_without_container_statuses_blocks_readiness(self):
40+
pod = _pod("Pending")
41+
42+
self.assertFalse(KubeCtl._pod_is_ready_or_succeeded(pod))
43+
44+
def test_failed_pod_with_unready_container_blocks_readiness(self):
45+
pod = _pod("Failed", [False])
46+
47+
self.assertFalse(KubeCtl._pod_is_ready_or_succeeded(pod))

0 commit comments

Comments
 (0)