k8s-alerts
k8s-alerts copied to clipboard
[FEATURE] Get pods status from k8s api
We can use the /api/v1/watch/pods
endpoint to get and watch the status events for the pods instead of using kubectl
tool
You could assign this to me if there's no hurry to implement it.
sure @muhammednagy
Hi
For now, after first testing how we can deal with /api/v1/watch/pods
to fetch the API streaming events
Here's the first approach and what do you think @muhammednagy @MohamedMSaeed @keroloswilliam
here is the reference for more details https://requests.readthedocs.io/en/master/user/advanced/#streaming-requests
import json
import requests
r = requests.get("http://127.0.0.1:8001/api/v1/watch/pods", stream=True)
# fullback encoding for bya-data
if r.encoding is None:
r.encoding = 'utf-8'
lines = r.iter_lines(decode_unicode=True)
for line in lines:
if line:
# load the bya-json to a string
data = json.loads(line)
# dumps the json object into an element
json_data = json.dumps(data['object']['status'], indent=2)
print(json_data)