1- from kubernetes import client , config , utils
1+ # from kubernetes import client, config, utils
22from flask import Flask , json , jsonify , request
33from os import path
44import yaml
55import pandas as pd
6-
6+ import kr8s
7+ from kr8s .objects import objects_from_files
8+ import kr8s .asyncio
9+ import os
10+ import subprocess
711app = Flask (__name__ )
812
9- config .load_kube_config ()
13+ #config.load_kube_config()
14+
15+ #core_v1 = client.CoreV1Api()
16+ #apps_v1 = client.AppsV1Api()
17+ #api = client.ApiClient()
18+
19+ def otel_collector_init ():
20+ get_chart = subprocess .check_output (["helm" , "repo" , "add" , "splunk-otel-collector-chart" , "https://signalfx.github.io/splunk-otel-collector-chart" ])
21+ helm_update = subprocess .check_output (["helm" , "repo" , "update" ])
22+ print (get_chart )
23+ print (helm_update )
1024
11- core_v1 = client .CoreV1Api ()
12- apps_v1 = client .AppsV1Api ()
13- api = client .ApiClient ()
1425
26+ @app .route ('start_collector' , methods = ['GET' ])
27+ def start_collector ():
28+ start = subprocess .check_output
1529@app .route ('/pods' , methods = ['GET' ])
1630def 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" ])
31+ df = pd .DataFrame (columns = ["Namespace" , "Name" , "Status" , "HostIP" ])
32+ pods = kr8s .get ("pods" , namespace = kr8s .ALL )
33+ for pod in pods :
34+ print (pod .raw )
35+ row = (pod .raw ["metadata" ]["namespace" ], pod .raw ["metadata" ]["name" ], pod .raw ["status" ]["phase" ], pod .raw ["status" ]["hostIP" ])
2336 df .loc [len (df )] = row
2437
38+
2539 return df .to_dict (orient = 'records' )
2640
2741
@@ -30,21 +44,38 @@ def health():
3044 return "OK"
3145
3246
33- @app .route ('/apply_deployment' , methods = ['POST ' ])
47+ @app .route ('/apply_deployment' , methods = ['GET ' ])
3448def deployment ():
49+ try :
3550 deployment = "deployment.yaml"
3651 #resp = apps_v1.create_namespaced_deployment(
3752 # body=deployment, namespace=namespace, _preload_content=False)
38- resp = utils .create_from_yaml (api , deployment , namespace = "default" )
39- print (resp )
53+ #resp = utils.create_from_yaml(api, deployment, namespace="default")
54+ #print(resp)
55+ resources = objects_from_files (deployment )
56+ for r in resources :
57+ print (r )
58+ r .create ()
4059 return "200"
60+ except Exception as e :
61+ return str (e )
4162
4263@app .route ('/delete_deployment' , methods = ['GET' ])
4364def delete_deployment ():
44- filename = request .args .get ('type' , default = 'deployment.yaml' , type = str )
45- namespace = request .args .get ('namespace' , default = 'default' , type = str )
46- resp = apps_v1 .delete_namespaced_deployment (name = "nginx-deployment" , namespace = namespace , _preload_content = False )
47- return resp
65+ try :
66+ deployment = "deployment.yaml"
67+ resources = objects_from_files (deployment )
68+ for r in resources :
69+ print (r )
70+ r .delete ()
71+ return "200"
72+ except Exception as e :
73+ return str (e )
74+ #filename = request.args.get('type', default = 'deployment.yaml', type = str)
75+ #namespace = request.args.get('namespace', default = 'default', type = str)
76+ #resp = apps_v1.delete_namespaced_deployment(name="nginx-deployment", namespace=namespace, _preload_content=False)
77+ #return resp
4878
4979if __name__ == '__main__' :
50- app .run (host = "0.0.0.0" , port = 8083 , debug = True )
80+ otel_collector_init ()
81+ app .run (host = "0.0.0.0" , port = 8083 , debug = True )
0 commit comments