Skip to content

Commit ff09884

Browse files
committed
add components status
1 parent 86ed463 commit ff09884

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

cwm_cdn_api/api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import tempfile
4+
import datetime
45

56
import orjson
67

@@ -87,3 +88,36 @@ async def list_iterator():
8788
yield name.split('/', 1)[1]
8889
else:
8990
raise Exception(output)
91+
92+
93+
def parse_pod_status(pod):
94+
creation_timestamp = pod['metadata']['creationTimestamp']
95+
image = pod['spec']['containers'][0]['image']
96+
image_tag = image.split(':')[-1] if ':' in image else 'latest'
97+
status_phase = pod['status']['phase']
98+
return {
99+
'creation_timestamp': creation_timestamp,
100+
'image_tag': image_tag,
101+
'status_phase': status_phase,
102+
}
103+
104+
105+
async def components_status():
106+
res = {
107+
'cache': {},
108+
'edge': {},
109+
'operator': [],
110+
}
111+
for pod in orjson.loads(await async_subprocess_check_output(
112+
'kubectl', '-n', 'cdn-cache', 'get', 'pods', '-o', 'json'
113+
))['items']:
114+
res['cache'].setdefault(pod['metadata']['name'].split('-')[0], []).append(parse_pod_status(pod))
115+
for pod in orjson.loads(await async_subprocess_check_output(
116+
'kubectl', '-n', 'cdn-edge', 'get', 'pods', '-o', 'json'
117+
))['items']:
118+
res['edge'].setdefault(pod['metadata']['name'].split('-')[2], []).append(parse_pod_status(pod))
119+
for pod in orjson.loads(await async_subprocess_check_output(
120+
'kubectl', '-n', 'cwm-cdn-operator-system', 'get', 'pods', '-o', 'json'
121+
))['items']:
122+
res['operator'].append(parse_pod_status(pod))
123+
return res

cwm_cdn_api/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,11 @@ async def start_zone_writer(zones_dir):
6565
await zone_writer.main(zones_dir, daemon=True)
6666

6767

68+
@main.command()
69+
async def components_status():
70+
from . import api
71+
common.json_print(await api.components_status())
72+
73+
6874
if __name__ == '__main__':
6975
main()

cwm_cdn_api/router.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ async def list_tenants():
7676
@router.get('/reserved-names')
7777
async def reserved_names():
7878
return [name async for name in api.reserved_names_iterator()]
79+
80+
81+
@router.get('/components-status')
82+
async def components_status():
83+
return await api.components_status()

0 commit comments

Comments
 (0)