-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tyler Gu <[email protected]>
- Loading branch information
Showing
5 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
import logging | ||
import os | ||
from typing import List | ||
|
||
import kubernetes | ||
from kubernetes.client import ApiClient | ||
|
||
|
||
class MetricsApiWatcher(): | ||
|
||
def __init__(self, output_dir: str) -> None: | ||
"""Initialize the WatchPodStats class | ||
Args: | ||
apiclient: kubernetes client | ||
operator_name: name of the operator | ||
output_dir: output directory | ||
""" | ||
self._output_dir = output_dir | ||
self._sequence = 0 | ||
self._stop = False | ||
|
||
def write_stats(self, stats_buf: List[dict], sequence: int): | ||
with open(os.path.join(self._output_dir, f'pods_stats.{sequence:08d}.json'), 'w') as f: | ||
json.dump(stats_buf, f, indent=4) | ||
|
||
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: | ||
if self._stop: | ||
break | ||
pod_metrics = {} | ||
for pod in stats['items']: | ||
pod_name = pod['metadata']['name'] | ||
if pod_name == "etcd-kind-control-plane": | ||
pod_metrics = pod | ||
pod_metrics['timestamp'] = pod['timestamp'] | ||
elif pod_name == operator_name: | ||
pod_metrics = pod | ||
pod_metrics['timestamp'] = pod['timestamp'] | ||
|
||
stats_buf.append(pod_metrics) | ||
if len(stats_buf) >= 1000: | ||
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 | ||
return | ||
|
||
def stop(self): | ||
self._stop = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters