-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube-assistant.py
417 lines (343 loc) · 16.4 KB
/
kube-assistant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import io
import os
import schedule
import threading
import time
import uuid
from datetime import datetime, timedelta
import kubernetes
import yaml
from dotenv import load_dotenv, find_dotenv
from flask import (Flask,
render_template,
redirect,
url_for,
flash,
request,
session)
from flask_bootstrap import Bootstrap5
from secret_key_generator import secret_key_generator
from forms import (CreateCodeGenForm,
QuestionForm,
ReviewForm,
ExplainForm,
K8sClusterForm,
K8sNamespaceForm,
K8sPodsForm,
ScanK8sDeploySts)
from k8scluster import K8sCluster
from src.kube_debugger_crew.crew import KubeDebuggerCrew
from src.kube_chatter_crew.crew import KubeChatterCrew
from src.kube_reviewer_crew.crew import KubeReviewerCrew
from src.kube_security_crew.crew import KubeSecurityCrew
from src.kube_developer_crew.crew import KubeDeveloperCrew
from src.kube_explainer_crew.crew import KubeExplainerCrew
app = Flask(__name__)
# Generate an SHH key
app.config['SECRET_KEY'] = secret_key_generator.generate(len_of_secret_key=32)
Bootstrap5(app)
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Create the uploads directory if it doesn't exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
cleanup_lock = threading.Lock()
# Load the environment variables to connect to watsonx and serper
if load_dotenv(find_dotenv()):
# Env variables exist in .env file, use them. Else these env variables must be set
os.environ["GRANITE31_DENSE"] = os.getenv("GRANITE31_DENSE")
os.environ["GRANITE3_MOE"] = os.getenv("GRANITE3_MOE")
os.environ["OLLAMA_URL"] = os.getenv("OLLAMA_URL")
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
os.environ["TEAM_NAME"] = os.getenv("TEAM_NAME")
os.environ["CONTACT_EMAILS"] = os.getenv("CONTACT_EMAILS")
else:
# Only hardcode model names. Others env variables should be injected
os.environ["GRANITE31_DENSE"] = "ollama/granite3.1-dense:8b"
os.environ["GRANITE3_MOE"] = "ollama/granite3-moe:3b"
team_name = os.environ.get('TEAM_NAME')
contact_emails = os.environ.get('CONTACT_EMAILS')
def periodic_cleanup():
"""Function to clean up files older than a certain time."""
with cleanup_lock:
current_time = datetime.now()
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
modified_time = datetime.fromtimestamp(os.path.getmtime(file_path))
age = current_time - modified_time
# Define the time threshold for file deletion (e.g., 1 hour)
max_age = timedelta(hours=8)
if age > max_age:
os.remove(file_path)
print(f"File {filename} deleted due to age.")
# Schedule the cleanup function to run every minute
schedule.every().minute.do(periodic_cleanup)
# Continuously run the scheduled tasks in a separate thread
def scheduled_task():
while True:
schedule.run_pending()
time.sleep(3600) # Sleep for 1 second to avoid excessive CPU usage
cleanup_thread = threading.Thread(target=scheduled_task, daemon=True)
cleanup_thread.start()
@app.route("/", methods=['GET'])
def home():
return render_template("index.html", team_name=team_name)
@app.route("/about", methods=['GET'])
def about():
return render_template("about.html", team_name=team_name, contact_emails=contact_emails)
@app.route("/contact", methods=['GET'])
def contact():
return render_template("contact.html", team_name=team_name,
contact_emails=contact_emails)
@app.route("/generate", methods=['GET', 'POST'])
def generate_code():
codegen_form = CreateCodeGenForm()
if codegen_form.validate_on_submit():
# generate_code = GenerateCode(codegen_form.instruction.data)
inputs = {
"instruction": codegen_form.instruction.data,
}
codegen_form.result.data = KubeDeveloperCrew().crew().kickoff(inputs=inputs)
return render_template("generate.html", form=codegen_form, team_name=team_name)
@app.route("/question", methods=['GET', 'POST'])
def question():
question_form = QuestionForm()
if question_form.validate_on_submit():
# ask_question = AskQuestion(question_form.question.data)
inputs = {
"question": question_form.question.data,
}
question_form.result.data = KubeChatterCrew().crew().kickoff(inputs=inputs)
return render_template("question.html", form=question_form, team_name=team_name)
@app.route("/review", methods=['GET', 'POST'])
def review():
review_form = ReviewForm()
if review_form.validate_on_submit():
#review_code = ReviewCode(review_form.code_input.data)
inputs = {
"review_content": review_form.code_input.data,
}
review_form.result.data = KubeReviewerCrew().crew().kickoff(inputs=inputs)
return render_template("review.html", form=review_form, team_name=team_name)
@app.route("/explain", methods=['GET', 'POST'])
def explain():
explain_form = ExplainForm()
if explain_form.validate_on_submit():
#explain_code = ExplainCode(explain_form.code_input.data)
inputs = {
"explain_code": explain_form.code_input.data,
}
explain_form.result.data = KubeExplainerCrew().crew().kickoff(inputs=inputs)
return render_template("explain.html", form=explain_form, team_name=team_name)
@app.route("/cluster/", methods=['GET', 'POST'])
def connect_cluster():
k8cluster_form = K8sClusterForm()
if k8cluster_form.validate_on_submit():
kubeconfig_file = k8cluster_form.kubeconfig.data
filename = str(uuid.uuid4()) + '_' + kubeconfig_file.filename
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
kubeconfig_file.save(file_path)
# kubeconfig_content = kubeconfig_file.read().decode('utf-8')
# kubeconfig_file_object = io.BytesIO(kubeconfig_content.encode('utf-8'))
session['kubeconfig'] = file_path
# config_data = yaml.safe_load(kubeconfig_content)
# global current_context
# current_context = config_data.get('current-context', None)
# global k8scluster
# k8scluster = K8sCluster(configfile=kubeconfig_file_object)
return redirect(url_for('pull_namespaces'))
return render_template('k8sissue.html', form=k8cluster_form, team_name=team_name)
# Below method added for Kubernetes Cluster security scanning
@app.route("/k8scluster/", methods=['GET', 'POST'])
def connect_k8scluster():
k8cluster_form = K8sClusterForm()
if k8cluster_form.validate_on_submit():
kubeconfig_file = k8cluster_form.kubeconfig.data
filename = str(uuid.uuid4()) + '_' + kubeconfig_file.filename
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
kubeconfig_file.save(file_path)
# kubeconfig_content = kubeconfig_file.read().decode('utf-8')
# kubeconfig_file_object = io.BytesIO(kubeconfig_content.encode('utf-8'))
session['kubeconfig'] = file_path
# config_data = yaml.safe_load(kubeconfig_content)
# global current_context
# current_context = config_data.get('current-context', None)
# global k8scluster
# k8scluster = K8sCluster(configfile=kubeconfig_file_object)
return redirect(url_for('pull_k8snamespaces'))
return render_template('k8ssecurity.html', form=k8cluster_form, team_name=team_name)
@app.route("/cluster/namespaces/", methods=['GET', 'POST'])
def pull_namespaces():
k8sns_form = K8sNamespaceForm()
file_path = session.get('kubeconfig')
if file_path is None:
flash(f"Error connecting to cluster. Please re-upload valid kubeconfig file")
return redirect(url_for('connect_cluster'))
with open(file_path, 'r') as file:
file_content = file.read()
kubeconfig_file_object = io.BytesIO(file_content.encode('utf-8'))
config_data = yaml.safe_load(file_content)
current_context = config_data.get('current-context', None)
k8scluster = K8sCluster(configfile=kubeconfig_file_object)
k8sns_form.cluster_api_url.data = current_context
try:
namespaces = k8scluster.list_namespaces()
except kubernetes.client.exceptions.ApiException:
flash('Unauthorized exception. Update the Kubeconfig file and retry.')
return redirect(url_for('connect_cluster'))
except Exception as e:
flash(f"Error connecting to cluster: {str(e)}")
return redirect(url_for('connect_cluster'))
k8sns_form.namespaces.choices = namespaces
if k8sns_form.validate_on_submit():
selected_ns = k8sns_form.namespaces.data
return redirect(url_for('pull_unhealthy_pods', namespace=selected_ns))
return render_template('k8sns.html', form=k8sns_form, team_name=team_name)
# Below method added for pulling namespaces for cluster security scan
@app.route("/k8scluster/namespaces/", methods=['GET', 'POST'])
def pull_k8snamespaces():
k8sns_form = K8sNamespaceForm()
file_path = session.get('kubeconfig')
if file_path is None:
flash(f"Error connecting to cluster. Please re-upload valid kubeconfig file")
return redirect(url_for('connect_k8scluster'))
with open(file_path, 'r') as file:
file_content = file.read()
kubeconfig_file_object = io.BytesIO(file_content.encode('utf-8'))
config_data = yaml.safe_load(file_content)
current_context = config_data.get('current-context', None)
k8scluster = K8sCluster(configfile=kubeconfig_file_object)
k8sns_form.cluster_api_url.data = current_context
try:
namespaces = k8scluster.list_namespaces()
except kubernetes.client.exceptions.ApiException:
flash('Unauthorized exception. Update the Kubeconfig file and retry.')
return redirect(url_for('connect_k8scluster'))
except Exception as e:
flash(f"Error connecting to cluster: {str(e)}")
return redirect(url_for('connect_k8scluster'))
k8sns_form.namespaces.choices = namespaces
if k8sns_form.validate_on_submit():
selected_ns = k8sns_form.namespaces.data
return redirect(url_for('pull_deploy_sts', namespace=selected_ns))
return render_template('k8ssecurityns.html', form=k8sns_form, team_name=team_name)
@app.route("/cluster/namespaces/<namespace>/deploysts", methods=['GET', 'POST'])
def pull_deploy_sts(namespace: str):
scank8s_form = ScanK8sDeploySts()
file_path = session.get('kubeconfig')
if file_path is None:
flash(f"Error connecting to cluster. Please re-upload valid kubeconfig file")
return redirect(url_for('connect_k8scluster'))
with open(file_path, 'r') as file:
file_content = file.read()
kubeconfig_file_object = io.BytesIO(file_content.encode('utf-8'))
config_data = yaml.safe_load(file_content)
current_context = config_data.get('current-context', None)
k8scluster = K8sCluster(configfile=kubeconfig_file_object)
scank8s_form.cluster_api_url.data = current_context
try:
scank8s_form.namespaces.choices = k8scluster.list_namespaces()
deployments = k8scluster.list_deploys(ns=namespace)
statefulsts = k8scluster.list_sts(ns=namespace)
list_deploy_sts = []
for deployment in deployments:
entity = {
"name": deployment,
"kind": "Deployment"
}
list_deploy_sts.append(entity)
for sts in statefulsts:
entity = {
"name": sts,
"kind": "StatefulSet"
}
list_deploy_sts.append(entity)
if len(list_deploy_sts) == 0:
flash(
f'No Deployments or Statefulsets found in the namespace: {namespace}. Select another namespace to check.')
return redirect(url_for('pull_k8snamespaces'))
scank8s_form.deploysts.choices = list_deploy_sts
except kubernetes.client.exceptions.ApiException:
flash('Unauthorized exception. Update the Kubeconfig file and retry.')
return redirect(url_for('connect_k8scluster'))
except Exception as e:
flash(f"Error connecting to cluster: {str(e)}")
return redirect(url_for('connect_k8scluster'))
scank8s_form.namespaces.data = namespace
if scank8s_form.validate_on_submit():
if request.form.get('submit'):
selected_ns = namespace
selected_obj = eval(scank8s_form.deploysts.data)
if selected_obj['kind'] == "Deployment":
deploy_name = selected_obj['name']
deploy_yaml = k8scluster.get_deploy_yaml(ns=selected_ns, deploy_name=deploy_name)
inputs = {
"deploy_sts_yaml": deploy_yaml,
}
scank8s_form.result.data = KubeSecurityCrew().crew().kickoff(inputs=inputs)
elif selected_obj['kind'] == "StatefulSet":
sts_name = selected_obj['name']
sts_yaml = k8scluster.get_sts_yaml(ns=selected_ns, sts_name=sts_name)
inputs = {
"deploy_sts_yaml": sts_yaml,
}
scank8s_form.result.data = KubeSecurityCrew().crew().kickoff(inputs=inputs)
elif request.form.get('show_yaml'):
selected_ns = namespace
selected_obj = eval(scank8s_form.deploysts.data)
if selected_obj['kind'] == "Deployment":
deploy_name = selected_obj['name']
deploy_yaml = k8scluster.get_deploy_yaml(ns=selected_ns, deploy_name=deploy_name)
scank8s_form.result.data = deploy_yaml
elif selected_obj['kind'] == "StatefulSet":
sts_name = selected_obj['name']
sts_yaml = k8scluster.get_sts_yaml(ns=selected_ns, sts_name=sts_name)
scank8s_form.result.data = sts_yaml
return render_template('k8sscandeploysts.html', form=scank8s_form, team_name=team_name)
@app.route("/cluster/namespaces/<namespace>", methods=['GET', 'POST'])
def pull_unhealthy_pods(namespace: str):
k8spod_form = K8sPodsForm()
file_path = session.get('kubeconfig')
if file_path is None:
flash(f"Error connecting to cluster. Please re-upload valid kubeconfig file")
return redirect(url_for('connect_cluster'))
with open(file_path, 'r') as file:
file_content = file.read()
kubeconfig_file_object = io.BytesIO(file_content.encode('utf-8'))
config_data = yaml.safe_load(file_content)
current_context = config_data.get('current-context', None)
k8scluster = K8sCluster(configfile=kubeconfig_file_object)
k8spod_form.cluster_api_url.data = current_context
try:
k8spod_form.namespaces.choices = k8scluster.list_namespaces()
unhealthy_pods = k8scluster.unhealthy_pods(ns=namespace)
if len(unhealthy_pods) == 0:
flash(f'No unhealthy pod found in namespace: {namespace}. Select another namespace to check.')
return redirect(url_for('pull_namespaces'))
k8spod_form.pods.choices = unhealthy_pods
except kubernetes.client.exceptions.ApiException:
flash('Unauthorized exception. Update the Kubeconfig file and retry.')
return redirect(url_for('connect_cluster'))
except Exception as e:
flash(f"Error connecting to cluster: {str(e)}")
return redirect(url_for('connect_cluster'))
k8spod_form.namespaces.data = namespace
if k8spod_form.validate_on_submit():
if request.form.get('submit'):
selected_ns = namespace
selected_pod = k8spod_form.pods.data
# Hand over to Crew to diagnose the issue and provide recommendation
pod_status = k8scluster.pod_status_details(ns=selected_ns, podname=selected_pod)
inputs = {
"selected_ns": namespace,
"selected_pod": k8spod_form.pods.data,
"issue": pod_status.__str__(),
}
k8spod_form.result.data = KubeDebuggerCrew().crew().kickoff(inputs=inputs)
elif request.form.get('show_yaml'):
selected_ns = namespace
selected_pod = k8spod_form.pods.data
pod_yaml = k8scluster.get_pod_yaml(ns=selected_ns, podname=selected_pod)
k8spod_form.result.data = pod_yaml
return render_template('k8spods.html', form=k8spod_form, team_name=team_name)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)