Skip to content

Commit

Permalink
feat: add deployment-data
Browse files Browse the repository at this point in the history
  • Loading branch information
devopstales committed Jun 22, 2023
1 parent 0fd28f5 commit ebefedc
Show file tree
Hide file tree
Showing 5 changed files with 489 additions and 22 deletions.
95 changes: 91 additions & 4 deletions src/kubedash/functions/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,10 +1250,29 @@ def k8sDeploymentsGet(username_role, user_token, ns):
for d in deployment_list.items:
DEPLOYMENT_DATA = {
"name": d.metadata.name,
"namespace": ns,
"labels": list(),
"selectors": list(),
"replicas": d.spec.replicas,
# status
"desired": "",
"updated": "",
"ready": "",
# Environment variables
"environment_variables": [],
# Security
"security_context": d.spec.template.spec.security_context.to_dict(),
# Conditions
"conditions": d.status.conditions,
# Containers
"containers": list(),
"init_containers": list(),
# Related Resources
"image_pull_secrets": list(),
"service_account": list(),
"pvc": list(),
"cm": list(),
"secrets": list(),
}
if d.status.ready_replicas:
DEPLOYMENT_DATA['ready'] = d.status.ready_replicas
Expand All @@ -1270,6 +1289,58 @@ def k8sDeploymentsGet(username_role, user_token, ns):
if d.metadata.labels:
for key, value in d.metadata.labels.items():
DEPLOYMENT_DATA['labels'].append(key + "=" + value)
selectors = d.spec.selector.to_dict()
DEPLOYMENT_DATA['selectors'] = selectors['match_labels']
if d.spec.template.spec.image_pull_secrets:
for ips in d.spec.template.spec.image_pull_secrets:
DEPLOYMENT_DATA['image_pull_secrets'].append(ips.to_dict())
if d.spec.template.spec.service_account_name:
for ips in d.spec.template.spec.service_account_name:
DEPLOYMENT_DATA['service_account'].append(ips.to_dict())
if d.spec.template.spec.volumes:
for v in d.spec.template.spec.volumes:
if v.persistent_volume_claim:
DEPLOYMENT_DATA['pvc'].append(v.persistent_volume_claim.claim_name)
if v.config_map:
DEPLOYMENT_DATA['cm'].append(v.config_map.name)
if v.secret:
DEPLOYMENT_DATA['secrets'].append(v.secret.secret_name)
for c in d.spec.template.spec.containers:
if c.env:
for e in c.env:
ed = e.to_dict()
env_name = None
env_value = None
for name, val in ed.items():
if "value_from" in name and val is not None:
for key, value in val.items():
if "secret_key_ref" in key and value:
for n, v in value.items():
if "name" in n:
if v not in DEPLOYMENT_DATA['secrets']:
DEPLOYMENT_DATA['secrets'].append(v)
elif "name" in name and val is not None:
env_name = val
elif "value" in name and val is not None:
env_value = val

if env_name and env_value is not None:
DEPLOYMENT_DATA['environment_variables'].append({
env_name: env_value
})
CONTAINERS = {
"name": c.name,
"image": c.image,
}
DEPLOYMENT_DATA['containers'].append(CONTAINERS)
if d.spec.template.spec.init_containers:
for ic in d.spec.template.spec.init_containers:
CONTAINERS = {
"name": ic.name,
"image": ic.image,
}
DEPLOYMENT_DATA['init_containers'].append(CONTAINERS)

DEPLOYMENT_LIST.append(DEPLOYMENT_DATA)
return DEPLOYMENT_LIST
except ApiException as error:
Expand Down Expand Up @@ -1360,6 +1431,8 @@ def k8sPodGet(username_role, user_token, ns, po):
"priority": pod_data.spec.priority,
"priority_class_name": pod_data.spec.priority_class_name,
"runtime_class_name": pod_data.spec.runtime_class_name,
# Environment variables
"environment_variables": [],
# Containers
"containers": list(),
"init_containers": list(),
Expand Down Expand Up @@ -1387,13 +1460,25 @@ def k8sPodGet(username_role, user_token, ns, po):
if c.env:
for e in c.env:
ed = e.to_dict()
env_name = None
env_value = None
for name, val in ed.items():
if "value_from" in name and val is not None:
for key, value in val.items():
if "secret_key_ref" in key and value:
for n, v in value.items():
if "name" in n:
POD_DATA['secrets'].append(v)
if v not in POD_DATA['secrets']:
POD_DATA['secrets'].append(v)
elif "name" in name and val is not None:
env_name = val
elif "value" in name and val is not None:
env_value = val

if env_name and env_value is not None:
POD_DATA['environment_variables'].append({
env_name: env_value
})
for cs in pod_data.status.container_statuses:
if cs.name == c.name:
if cs.ready:
Expand Down Expand Up @@ -1470,8 +1555,9 @@ def k8sPodListVulnsGet(username_role, user_token, ns):
vulnerabilityreport_list = k8s_client.CustomObjectsApi().list_namespaced_custom_object("trivy-operator.devopstales.io", "v1", ns, "vulnerabilityreports")
HAS_REPORT = True
except Exception as error:
ErrorHandler(logger, "error", error)
vulnerabilityreport_list = False
vulnerabilityreport_list = None
if error.status != 404:
ErrorHandler(logger, "error", error)

for pod in pod_list.items:
POD_VULN_SUM = {
Expand Down Expand Up @@ -1518,8 +1604,9 @@ def k8sPodVulnsGet(username_role, user_token, ns, pod):
try:
vulnerabilityreport_list = k8s_client.CustomObjectsApi().list_namespaced_custom_object("trivy-operator.devopstales.io", "v1", ns, "vulnerabilityreports")
except Exception as error:
ErrorHandler(logger, "error", error)
vulnerabilityreport_list = None
if error.status != 404:
ErrorHandler(logger, "error", error)

for po in pod_list.items:
POD_VULNS = {}
Expand Down
46 changes: 32 additions & 14 deletions src/kubedash/functions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from functions.registry import *

from functions.components import tracer, socketio, csrf
from threading import Lock
from opentelemetry.trace.status import Status, StatusCode


Expand All @@ -25,9 +24,6 @@
routes = Blueprint("routes", __name__)
logger = get_logger(__name__)

thread = None
thread_lock = Lock()

def authenticated_only(f):
@functools.wraps(f)
def wrapped(*args, **kwargs):
Expand Down Expand Up @@ -368,6 +364,7 @@ def callback():

if (
request.url.startswith("http://") and
"HTTP_X_FORWARDED_PROTO" in request.environ and
request.environ["HTTP_X_FORWARDED_PROTO"] == "https"
):
request_url = request.url.replace("http", "https")
Expand Down Expand Up @@ -1094,6 +1091,31 @@ def deployments():
namespaces = namespace_list,
)

@routes.route('/deployments/data', methods=['GET', 'POST'])
@login_required
def deployments_data():
if request.method == 'POST':
deployment_name = request.form.get('deployment_name')
session['ns_select'] = request.form.get('ns_select')

if session['user_type'] == "OpenID":
user_token = session['oauth_token']
else:
user_token = None

deployments_list = k8sDeploymentsGet(session['user_role'], user_token, session['ns_select'])
deployment_data = None
for deployment in deployments_list:
if deployment["name"] == deployment_name:
deployment_data = deployment

return render_template(
'deployment-data.html.j2',
deployment_data = deployment_data,
)
else:
return redirect(url_for('routes.login'))

##############################################################
## ReplicaSets
##############################################################
Expand Down Expand Up @@ -1191,10 +1213,11 @@ def pods_data():
def pods_logs():
if request.method == 'POST':
session['ns_select'] = request.form.get('ns_select')
logger.info("async_mode: %s" % socketio.async_mode)
return render_template(
'pod-logs.html.j2',
po_name=session['po_name'],
async_mode=socketio.async_mode
po_name = request.form.get('po_name'),
async_mode = socketio.async_mode
)
else:
return redirect(url_for('routes.login'))
Expand All @@ -1212,10 +1235,7 @@ def message(data):
else:
user_token = None

global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(k8sPodLogsStream, session['user_role'], user_token, session['ns_select'], data)
socketio.start_background_task(k8sPodLogsStream, session['user_role'], user_token, session['ns_select'], data)

##############################################################
## Pod Exec
Expand All @@ -1226,6 +1246,7 @@ def message(data):
def pods_exec():
if request.method == 'POST':
session['ns_select'] = request.form.get('ns_select')
logger.info("async_mode: %s" % socketio.async_mode)
return render_template(
'pod-exec.html.j2',
po_name = request.form.get('po_name'),
Expand All @@ -1250,10 +1271,7 @@ def message(data):
global wsclient
wsclient = k8sPodExecSocket(session['user_role'], user_token, session['ns_select'], data)

global thread
with thread_lock:
if thread is None:
socketio.start_background_task(k8sPodExecStream, wsclient)
socketio.start_background_task(k8sPodExecStream, wsclient)

@socketio.on("exec-input", namespace="/exec")
@authenticated_only
Expand Down
Loading

0 comments on commit ebefedc

Please sign in to comment.