Skip to content

Commit 973029f

Browse files
Deconstructed the backend code
Signed-off-by: Himanshityagii24 <[email protected]>
1 parent a20477b commit 973029f

9 files changed

+456
-0
lines changed

backend/controllers/jobController.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';
2+
3+
4+
// Get all jobs with filters
5+
export const getAllJobs = async (req, res) => {
6+
try {
7+
const { namespace = '', search = '', queue = '', status = '' } = req.query;
8+
9+
let response;
10+
if (namespace === '' || namespace === 'All') {
11+
response = await k8sApi.listClusterCustomObject('batch.volcano.sh', 'v1alpha1', 'jobs', true);
12+
} else {
13+
response = await k8sApi.listNamespacedCustomObject('batch.volcano.sh', 'v1alpha1', namespace, 'jobs', true);
14+
}
15+
16+
let filteredJobs = response.body.items || [];
17+
18+
if (search) {
19+
filteredJobs = filteredJobs.filter(job =>
20+
job.metadata.name.toLowerCase().includes(search.toLowerCase())
21+
);
22+
}
23+
24+
if (queue && queue !== 'All') {
25+
filteredJobs = filteredJobs.filter(job => job.spec.queue === queue);
26+
}
27+
28+
if (status && status !== 'All') {
29+
filteredJobs = filteredJobs.filter(job => job.status.state.phase === status);
30+
}
31+
32+
res.json({
33+
items: filteredJobs,
34+
totalCount: filteredJobs.length,
35+
});
36+
} catch (err) {
37+
console.error('Error fetching jobs:', err);
38+
res.status(500).json({
39+
error: 'Failed to fetch jobs',
40+
details: err.message,
41+
});
42+
}
43+
};
44+
45+
// Get details of a specific job
46+
export const getJobDetails = async (req, res) => {
47+
try {
48+
const { namespace, name } = req.params;
49+
const response = await k8sApi.getNamespacedCustomObject(
50+
'batch.volcano.sh',
51+
'v1alpha1',
52+
namespace,
53+
'jobs',
54+
name
55+
);
56+
res.json(response.body);
57+
} catch (err) {
58+
console.error('Error fetching job:', err);
59+
res.status(500).json({
60+
error: 'Failed to fetch job',
61+
details: err.message,
62+
});
63+
}
64+
};
65+
66+
// Get YAML of a specific job
67+
export const getJobYaml = async (req, res) => {
68+
try {
69+
const { namespace, name } = req.params;
70+
const response = await k8sApi.getNamespacedCustomObject(
71+
'batch.volcano.sh',
72+
'v1alpha1',
73+
namespace,
74+
'jobs',
75+
name
76+
);
77+
const formattedYaml = yaml.dump(response.body, {
78+
indent: 2,
79+
lineWidth: -1,
80+
noRefs: true,
81+
sortKeys: false,
82+
});
83+
res.setHeader('Content-Type', 'text/yaml');
84+
res.send(formattedYaml);
85+
} catch (error) {
86+
console.error('Error fetching job YAML:', error);
87+
res.status(500).json({
88+
error: 'Failed to fetch job YAML',
89+
details: error.message,
90+
});
91+
}
92+
};
93+
94+
// Get all jobs without pagination
95+
export const getAllJobsWithoutPagination = async (req, res) => {
96+
try {
97+
const response = await k8sApi.listClusterCustomObject(
98+
'batch.volcano.sh',
99+
'v1alpha1',
100+
'jobs',
101+
{
102+
pretty: true,
103+
}
104+
);
105+
res.json({
106+
items: response.body.items,
107+
totalCount: response.body.items.length,
108+
});
109+
} catch (err) {
110+
console.error('Error fetching all jobs:', err);
111+
res.status(500).json({ error: 'Failed to fetch all jobs' });
112+
}
113+
};

backend/controllers/podController.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';
2+
3+
// Get all Pods based on query params
4+
export const getAllPods = async (req, res) => {
5+
try {
6+
const namespace = req.query.namespace || "";
7+
const searchTerm = req.query.search || "";
8+
const statusFilter = req.query.status || "";
9+
10+
let response;
11+
if (namespace === "" || namespace === "All") {
12+
response = await k8sCoreApi.listPodForAllNamespaces();
13+
} else {
14+
response = await k8sCoreApi.listNamespacedPod(namespace);
15+
}
16+
17+
let filteredPods = response.body.items || [];
18+
19+
// Apply search filter
20+
if (searchTerm) {
21+
filteredPods = filteredPods.filter((pod) =>
22+
pod.metadata.name.toLowerCase().includes(searchTerm.toLowerCase())
23+
);
24+
}
25+
if (statusFilter && statusFilter !== "All") {
26+
filteredPods = filteredPods.filter((pod) =>
27+
pod.status.phase === statusFilter
28+
);
29+
}
30+
31+
res.json({
32+
items: filteredPods,
33+
totalCount: filteredPods.length,
34+
});
35+
} catch (err) {
36+
console.error("Error fetching pods:", err);
37+
res.status(500).json({
38+
error: "Failed to fetch pods",
39+
details: err.message,
40+
});
41+
}
42+
};
43+
44+
// Get YAML details of a specific Pod
45+
export const getPodYaml = async (req, res) => {
46+
try {
47+
const { namespace, name } = req.params;
48+
const response = await k8sCoreApi.readNamespacedPod(name, namespace);
49+
50+
// Convert JSON to formatted YAML
51+
const formattedYaml = yaml.dump(response.body, {
52+
indent: 2,
53+
lineWidth: -1,
54+
noRefs: true,
55+
sortKeys: false,
56+
});
57+
58+
res.setHeader("Content-Type", "text/yaml");
59+
res.send(formattedYaml);
60+
} catch (error) {
61+
console.error("Error fetching pod YAML:", error);
62+
res.status(500).json({
63+
error: "Failed to fetch pod YAML",
64+
details: error.message,
65+
});
66+
}
67+
};
68+
69+
// Get all Kubernetes namespaces
70+
export const getNamespaces = async (req, res) => {
71+
try {
72+
const response = await k8sCoreApi.listNamespace();
73+
res.json({
74+
items: response.body.items,
75+
});
76+
} catch (error) {
77+
console.error("Error fetching namespaces:", error);
78+
res.status(500).json({
79+
error: "Failed to fetch namespaces",
80+
details: error.message,
81+
});
82+
}
83+
};
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';
2+
3+
// Fetch all Queues
4+
export const getAllQueues = async (req, res) => {
5+
try {
6+
const page = parseInt(req.query.page) || 1;
7+
const limit = parseInt(req.query.limit) || 10;
8+
const searchTerm = req.query.search || "";
9+
const stateFilter = req.query.state || "";
10+
console.log('Fetching queues with params:', { page, limit, searchTerm, stateFilter });
11+
12+
const response = await k8sApi.listClusterCustomObject(
13+
"scheduling.volcano.sh",
14+
"v1beta1",
15+
"queues"
16+
);
17+
18+
let filteredQueues = response.body.items || [];
19+
20+
// Apply search filter
21+
if (searchTerm) {
22+
filteredQueues = filteredQueues.filter((queue) =>
23+
queue.metadata.name.toLowerCase().includes(searchTerm.toLowerCase())
24+
);
25+
}
26+
27+
// Apply state filter
28+
if (stateFilter && stateFilter !== "All") {
29+
filteredQueues = filteredQueues.filter((queue) =>
30+
queue.status.state === stateFilter
31+
);
32+
}
33+
34+
const totalCount = filteredQueues.length;
35+
const startIndex = (page - 1) * limit;
36+
const endIndex = Math.min(startIndex + limit, totalCount);
37+
const paginatedQueues = filteredQueues.slice(startIndex, endIndex);
38+
39+
res.json({
40+
items: paginatedQueues,
41+
totalCount: totalCount,
42+
page: page,
43+
limit: limit,
44+
totalPages: Math.ceil(totalCount / limit)
45+
});
46+
} catch (error) {
47+
console.error("Error fetching queues:", error);
48+
res.status(500).json({
49+
error: "Failed to fetch queues",
50+
details: error.message,
51+
});
52+
}
53+
};
54+
55+
56+
// Get details of a specific Queue
57+
export const getQueueDetails = async (req, res) => {
58+
const queueName = req.params.name;
59+
try {
60+
console.log("Fetching details for queue:", queueName);
61+
62+
const response = await k8sApi.getClusterCustomObject(
63+
"scheduling.volcano.sh",
64+
"v1beta1",
65+
"queues",
66+
queueName
67+
);
68+
69+
console.log("Queue details response:", response.body);
70+
71+
res.json(response.body);
72+
} catch (error) {
73+
console.error("Error fetching queue details:", error);
74+
res.status(500).json({
75+
error: "Failed to fetch queue details",
76+
details: error.message,
77+
});
78+
}
79+
};
80+
81+
82+
// Get YAML of a specific Queue
83+
export const getQueueYaml = async (req, res) => {
84+
try {
85+
const response = await k8sApi.getClusterCustomObject(
86+
"scheduling.volcano.sh",
87+
"v1beta1",
88+
"queues",
89+
req.params.name
90+
);
91+
const formattedYaml = yaml.dump(response.body, {
92+
indent: 2,
93+
lineWidth: -1,
94+
noRefs: true,
95+
sortKeys: false
96+
});
97+
98+
// Set the content type to text/yaml and send the response
99+
res.setHeader('Content-Type', 'text/yaml');
100+
res.send(formattedYaml);
101+
} catch (error) {
102+
console.error("Error fetching queue YAML:", error);
103+
res.status(500).json({
104+
error: "Failed to fetch queue YAML",
105+
details: error.message
106+
});
107+
}
108+
};
109+
110+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';
3+
4+
// Controller function to fetch all Kubernetes jobs
5+
export const getAllJobs = async (req, res) => {
6+
try {
7+
8+
const response = await k8sApi.listClusterCustomObject(
9+
"batch.volcano.sh",
10+
"v1alpha1",
11+
"jobs",
12+
{ pretty: true }
13+
);
14+
15+
16+
const jobs = response.body.items.map(job => ({
17+
...job,
18+
status: {
19+
state: job.status?.state || 'Unknown',
20+
phase: job.status?.phase || 'Running'
21+
}
22+
}));
23+
24+
25+
res.json({
26+
items: jobs,
27+
totalCount: jobs.length
28+
});
29+
} catch (err) {
30+
31+
console.error("Error fetching all jobs:", err);
32+
res.status(500).json({ error: "Failed to fetch all jobs" });
33+
}
34+
};
35+
// Controller function to fetch all Kubernetes queues
36+
37+
export const getAllQueues = async (req, res) => {
38+
try {
39+
const response = await k8sApi.listClusterCustomObject(
40+
"scheduling.volcano.sh",
41+
"v1beta1",
42+
"queues"
43+
);
44+
45+
res.json({
46+
items: response.body.items,
47+
totalCount: response.body.items.length
48+
});
49+
} catch (error) {
50+
51+
console.error("Error fetching all queues:", error);
52+
res.status(500).json({ error: "Failed to fetch all queues" });
53+
}
54+
};
55+
56+
// Controller function to fetch all Kubernetes pods
57+
export const getAllPods = async (req, res) => {
58+
try {
59+
const response = await k8sCoreApi.listPodForAllNamespaces();
60+
61+
res.json({
62+
items: response.body.items,
63+
totalCount: response.body.items.length
64+
});
65+
} catch (error) {
66+
67+
console.error('Error fetching all pods:', error);
68+
res.status(500).json({ error: 'Failed to fetch all pods' });
69+
}
70+
};

0 commit comments

Comments
 (0)