From 30a644256ad8f7ad01d321a41c8fba151415dc59 Mon Sep 17 00:00:00 2001 From: Tyler Gu Date: Mon, 20 Nov 2023 18:11:35 -0600 Subject: [PATCH] Fix Signed-off-by: Tyler Gu --- .../metrics_api_watcher.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/performance_measurement/metrics_api_watcher.py b/performance_measurement/metrics_api_watcher.py index 2c2d65e5d..27390a5b7 100644 --- a/performance_measurement/metrics_api_watcher.py +++ b/performance_measurement/metrics_api_watcher.py @@ -1,6 +1,7 @@ import json import logging import os +import time from typing import List import kubernetes @@ -28,15 +29,13 @@ def write_stats(self, stats_buf: List[dict], sequence: int): def start(self, apiclient: ApiClient, operator_name: str, ): stats_buf: List[dict] = [] custom_api = kubernetes.client.CustomObjectsApi(apiclient) - watch = kubernetes.watch.Watch() - stream = watch.stream(func=custom_api.list_cluster_custom_object, - group="metrics.k8s.io", - version="v1beta1", - plural="pods",) - for stats in stream: + while True: if self._stop: break + stats = custom_api.list_cluster_custom_object(group="metrics.k8s.io", + version="v1beta1", + plural="pods",) pod_metrics = {} for pod in stats['items']: pod_name = pod['metadata']['name'] @@ -52,10 +51,11 @@ def start(self, apiclient: ApiClient, operator_name: str, ): self.write_stats(stats_buf, self._sequence) stats_buf = [] self._sequence += 1 - if len(stats_buf) > 0: - logging.info(f"Stopped, Writing {len(stats_buf)} stats to file") - self.write_stats(stats_buf, self._sequence) - self._sequence += 1 + time.sleep(1) + if len(stats_buf) > 0: + logging.info(f"Stopped, Writing {len(stats_buf)} stats to file") + self.write_stats(stats_buf, self._sequence) + self._sequence += 1 return def stop(self):