forked from NotHarshhaa/kubernetes-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
149 lines (131 loc) · 5.43 KB
/
app.js
File metadata and controls
149 lines (131 loc) · 5.43 KB
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
document.addEventListener('DOMContentLoaded', () => {
const scanForm = document.getElementById('scanForm');
const imageInput = document.getElementById('imageInput');
const scanResults = document.getElementById('scanResults');
const healthCheckButton = document.getElementById('healthCheckButton');
const healthCheckResult = document.getElementById('healthCheckResult');
const namespaceDropdown = document.getElementById('namespace-dropdown');
const autoRefreshToggle = document.getElementById('autoRefreshToggle');
let defaultNamespace = 'default';
let autoRefreshInterval;
// ========== Initial Setup ==========
init();
function init() {
if (scanForm) scanForm.addEventListener('submit', handleImageScan);
if (healthCheckButton) healthCheckButton.addEventListener('click', handleHealthCheck);
if (namespaceDropdown) namespaceDropdown.addEventListener('change', handleNamespaceChange);
if (autoRefreshToggle) autoRefreshToggle.addEventListener('change', toggleAutoRefresh);
updateDashboard();
}
// ========== Handlers ==========
function handleImageScan(event) {
event.preventDefault();
const imageName = imageInput.value.trim();
if (!imageName) return alert("Please enter a Docker image name.");
scanResults.textContent = "Scanning...";
scanImage(imageName);
}
function handleHealthCheck() {
healthCheckResult.textContent = 'Checking...';
fetchKubernetesInfo(defaultNamespace, (data) => {
const healthy = data.num_pods > 0;
healthCheckResult.textContent = healthy ? 'Healthy' : 'Unhealthy';
healthCheckResult.style.color = healthy ? 'green' : 'red';
}, () => {
healthCheckResult.textContent = 'Error';
healthCheckResult.style.color = 'red';
});
}
function handleNamespaceChange() {
const selectedNamespace = namespaceDropdown.value;
defaultNamespace = selectedNamespace;
fetchKubernetesInfo(selectedNamespace);
}
function toggleAutoRefresh() {
if (autoRefreshToggle.checked) {
autoRefreshInterval = setInterval(updateDashboard, 5000);
} else {
clearInterval(autoRefreshInterval);
}
}
// ========== Dashboard ==========
function updateDashboard() {
fetchSystemInfo();
fetchNamespaces();
}
function fetchSystemInfo() {
fetch('http://127.0.0.1:5000/system_info')
.then(res => res.json())
.then(data => {
document.querySelector('.memory-utilization .percentage').textContent = `${data.memory_usage.percent}%`;
document.querySelector('.cpu-utilization .percentage').textContent = `${data.cpu_percent}%`;
document.querySelector('.storage-used .percentage').textContent = `${data.disk_usage.percent}%`;
})
.catch(err => console.error('❌ System info fetch failed:', err));
}
function fetchNamespaces() {
fetch('http://127.0.0.1:5000/kubernetes_namespaces')
.then(res => res.json())
.then(namespaces => {
namespaceDropdown.innerHTML = '';
namespaces.forEach(ns => {
const option = document.createElement('option');
option.value = ns;
option.textContent = ns;
namespaceDropdown.appendChild(option);
});
// Set default or retain selected
if (!namespaces.includes(defaultNamespace)) {
defaultNamespace = namespaces[0] || 'default';
}
namespaceDropdown.value = defaultNamespace;
fetchKubernetesInfo(defaultNamespace);
})
.catch(err => console.error('❌ Failed to fetch namespaces:', err));
}
function fetchKubernetesInfo(namespace, onSuccess, onError) {
fetch(`http://127.0.0.1:5000/kubernetes_info?namespace=${namespace}`)
.then(res => res.json())
.then(data => {
document.querySelector('.deployments .count').textContent = data.num_deployments;
document.querySelector('.pods-running .count').textContent = data.num_pods;
document.querySelector('.services-running .count').textContent = data.num_services;
if (onSuccess) onSuccess(data);
})
.catch(err => {
console.error(`❌ Failed to fetch Kubernetes info for ${namespace}:`, err);
if (onError) onError(err);
});
}
function scanImage(imageName) {
fetch('http://127.0.0.1:5000/scan_image', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ container_id: imageName }),
})
.then(res => res.json())
.then(data => {
if (data.error) {
scanResults.textContent = `Error: ${data.error}`;
scanResults.style.color = 'red';
} else {
scanResults.style.color = 'black';
scanResults.textContent = '';
renderScanResult(data.scan_results);
}
})
.catch(err => {
console.error('❌ Scan failed:', err);
scanResults.textContent = 'Scan failed.';
scanResults.style.color = 'red';
});
}
function renderScanResult(result) {
try {
const formatted = typeof result === 'string' ? JSON.parse(result) : result;
scanResults.textContent = JSON.stringify(formatted, null, 2);
} catch (err) {
scanResults.textContent = result;
}
}
});