Skip to content

Commit f83978c

Browse files
Nikhil-Singhal-06fred-labs
authored andcommitted
fix/kubernetes-pod-status (IntelLabs#216)
* fix-kubernetes-pod-status * format * fix feedback message and repeat * cleanup
1 parent 80df8d8 commit f83978c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

libs/scenario_execution_kubernetes/scenario_execution_kubernetes/kubernetes_wait_for_pod_status.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ class KubernetesWaitForPodStatusState(Enum):
3232

3333
class KubernetesWaitForPodStatus(BaseAction):
3434

35-
def __init__(self, within_cluster: bool):
35+
def __init__(self, within_cluster: bool, namespace: str):
3636
super().__init__()
3737
self.target = None
38-
self.namespace = None
38+
self.namespace = namespace
3939
self.expected_status = None
4040
self.within_cluster = within_cluster
4141
self.regex = None
4242
self.client = None
4343
self.update_queue = queue.Queue()
4444
self.current_state = KubernetesWaitForPodStatusState.IDLE
45+
self.last_state = None
4546

4647
def setup(self, **kwargs):
4748
if self.within_cluster:
@@ -53,44 +54,50 @@ def setup(self, **kwargs):
5354
self.monitoring_thread = threading.Thread(target=self.watch_pods, daemon=True)
5455
self.monitoring_thread.start()
5556

56-
def execute(self, target: str, regex: bool, status: tuple, namespace: str):
57+
def execute(self, target: str, regex: bool, status: tuple, ):
5758
self.target = target
58-
self.namespace = namespace
5959
if not isinstance(status, tuple) or not isinstance(status[0], str):
6060
raise ValueError("Status expected to be enum.")
6161
self.expected_status = status[0]
6262
self.regex = regex
6363
self.current_state = KubernetesWaitForPodStatusState.MONITORING
64+
self.last_state = None
6465

6566
def update(self) -> py_trees.common.Status:
6667
while not self.update_queue.empty():
6768
item = self.update_queue.get()
6869
if len(item) != 2:
6970
return py_trees.common.Status.FAILURE
70-
71-
self.feedback_message = f"waiting for status of pod '{self.target}'." # pylint: disable= attribute-defined-outside-init
71+
if self.last_state is None:
72+
self.feedback_message = f"waiting for status of pod '{self.target}'." # pylint: disable= attribute-defined-outside-init
7273
if not self.regex:
7374
if item[0] != self.target:
7475
continue
7576
else:
7677
if not re.search(self.target, item[0]):
7778
continue
78-
if item[1].lower() == self.expected_status:
79+
if item[1].lower() == self.expected_status and self.last_state is not None:
7980
self.feedback_message = f"Pod '{item[0]}' changed to expected status '{item[1].lower()}'." # pylint: disable= attribute-defined-outside-init
81+
self.current_state = KubernetesWaitForPodStatusState.IDLE
8082
return py_trees.common.Status.SUCCESS
8183
else:
8284
self.feedback_message = f"Pod '{item[0]}' changed to status '{item[1].lower()}', expected '{self.expected_status}'." # pylint: disable= attribute-defined-outside-init
85+
self.last_state = item[1].lower()
8386
return py_trees.common.Status.RUNNING
8487

8588
def watch_pods(self):
8689
w = watch.Watch()
8790
try:
88-
# TODO: make use of send_initial_events=false in the future
91+
initial_pods = self.client.list_namespaced_pod(namespace=self.namespace).items
92+
for pod in initial_pods:
93+
pod_name = pod.metadata.name
94+
pod_status = pod.status.phase
95+
self.update_queue.put((pod_name, pod_status))
8996
for event in w.stream(self.client.list_namespaced_pod, namespace=self.namespace):
9097
pod_name = event['object'].metadata.name
9198
pod_status = event['object'].status.phase
9299
if self.current_state == KubernetesWaitForPodStatusState.MONITORING:
93100
self.update_queue.put((pod_name, pod_status))
94101
except ApiException as e:
95-
self.logger.error(f"Error accessing kubernetes: {e}")
102+
self.logger.error(f"Error accessing Kubernetes: {e}")
96103
self.update_queue.put(())

0 commit comments

Comments
 (0)