1+ from kubernetes import client , config
2+ from flask import Flask , json , jsonify , request
3+ from os import path
4+ import yaml
5+ import pandas as pd
6+
7+ app = Flask (__name__ )
8+
9+ config .load_kube_config ()
10+
11+ core_v1 = client .CoreV1Api ()
12+ apps_v1 = client .AppsV1Api ()
13+
14+
15+ @app .route ('/pods' , methods = ['GET' ])
16+ def get_pods ():
17+ result = core_v1 .list_pod_for_all_namespaces (_preload_content = False )
18+ pods = json .loads (result .data )
19+ df = pd .DataFrame (columns = ["Namespace" , "Name" , "Status" , "Pod IP" ])
20+
21+ for r in pods ['items' ]:
22+ row = (r ["metadata" ]["namespace" ], r ["metadata" ]["name" ], r ["status" ]["phase" ], r ["status" ]["podIP" ])
23+ df .loc [len (df )] = row
24+
25+ return df .to_dict (orient = 'records' )
26+
27+
28+ @app .route ('/health' , methods = ['GET' ])
29+ def health ():
30+ return "OK"
31+
32+
33+ @app .route ('/apply_deployment' , methods = ['GET' ])
34+ def deployment ():
35+ filename = request .args .get ('type' , default = 'deployment.yaml' , type = str )
36+ namespace = request .args .get ('namespace' , default = 'default' , type = str )
37+ with open (path .join (path .dirname (__file__ ), filename )) as f :
38+ deployment = yaml .safe_load (f )
39+ resp = apps_v1 .create_namespaced_deployment (
40+ body = deployment , namespace = namespace , _preload_content = False )
41+ return resp .json ()
42+
43+
44+ @app .route ('/delete_deployment' , methods = ['GET' ])
45+ def delete_deployment ():
46+ filename = request .args .get ('type' , default = 'deployment.yaml' , type = str )
47+ namespace = request .args .get ('namespace' , default = 'default' , type = str )
48+ resp = apps_v1 .delete_namespaced_deployment (name = "nginx-deployment" , namespace = namespace , _preload_content = False )
49+ return resp .json ()
50+
51+ if __name__ == '__main__' :
52+ app .run (host = "0.0.0.0" , port = 5001 , debug = True )
0 commit comments