Skip to content

Commit 31efe77

Browse files
committed
types updated and syntax corrected
Signed-off-by: Sayan Banerjee <[email protected]>
1 parent 52b94b4 commit 31efe77

File tree

6 files changed

+107
-86
lines changed

6 files changed

+107
-86
lines changed

backend/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "dist/index.js",
77
"scripts": {
88
"dev": "nodemon",
9-
"build": "babel src --extensions \".ts,.js\" -d dist",
9+
"build": "tsc --noEmit && babel src --extensions \".ts,.js\" -d dist",
1010
"prepare": "npm run build",
1111
"test": "mocha --require @babel/register --recursive",
1212
"generate:types": "bash ./src/types/generate.sh"

backend/src/controllers/job.ts

+48-44
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,35 @@ import yaml from "js-yaml";
55
import { IJob } from "../types/index";
66

77
interface IResponse {
8-
response: http.IncomingMessage;
9-
body: { items: IJob[] };
8+
items: IJob[];
109
}
1110

1211
// Auxiliary function: determine the status based on the job status
1312
function getJobState(job: IJob) {
14-
if (job.status?.state) return job.status.state;
15-
if (job.status?.Running) return "Running";
16-
if (job.status?.Completed) return "Completed";
17-
if (job.status?.Failed) return "Failed";
18-
if (job.status?.Pending) return "Pending";
13+
if (job.status === "Running") return "Running";
14+
if (job.status === "Completed") return "Completed";
15+
if (job.status === "Failed") return "Failed";
16+
if (job.status === "Pending") return "Running";
1917
return job.status || "Unknown";
2018
}
2119

22-
// Get all Jobs (no pagination)
20+
// @desc Get all Jobs (no pagination)
21+
// @route GET /all-jobs
2322
export const getAllJobs = async (req: Request, res: Response) => {
2423
try {
25-
const response = (await k8sApi.listClusterCustomObject(
26-
"batch.volcano.sh",
27-
"v1alpha1",
28-
"jobs", // 修改这里:从 "jobs" 改为 "vcjobs"
29-
"true",
30-
)) as IResponse;
31-
32-
const jobs = response.body.items.map((job) => ({
24+
const response: IResponse = await k8sApi.listClusterCustomObject({
25+
group: "batch.volcano.sh",
26+
version: "v1alpha1",
27+
plural: "jobs", // 修改这里:从 "jobs" 改为 "vcjobs"
28+
pretty: "true",
29+
});
30+
31+
const jobs = response.items.map((job) => ({
3332
...job,
3433
status: {
3534
state: job.status?.state || getJobState(job),
3635
phase:
37-
job.status?.phase || job.spec?.minAvailable
36+
job.status?.state?.phase || job.spec?.minAvailable
3837
? "Running"
3938
: "Unknown",
4039
},
@@ -56,6 +55,9 @@ interface JobQueryParams {
5655
queue?: string;
5756
status?: string;
5857
}
58+
59+
// @desc Get Jobs with pagination
60+
// @route GET /jobs
5961
export const getJobs = async (
6062
req: Request<{}, {}, {}, JobQueryParams>,
6163
res: Response,
@@ -75,23 +77,23 @@ export const getJobs = async (
7577

7678
let response: IResponse;
7779
if (namespace === "" || namespace === "All") {
78-
response = (await k8sApi.listClusterCustomObject(
79-
"batch.volcano.sh",
80-
"v1alpha1",
81-
"jobs",
82-
"true",
83-
)) as IResponse;
80+
response = await k8sApi.listClusterCustomObject({
81+
group: "batch.volcano.sh",
82+
version: "v1alpha1",
83+
plural: "jobs",
84+
pretty: "true",
85+
});
8486
} else {
85-
response = (await k8sApi.listNamespacedCustomObject(
86-
"batch.volcano.sh",
87-
"v1alpha1",
87+
response = await k8sApi.listNamespacedCustomObject({
88+
group: "batch.volcano.sh",
89+
version: "v1alpha1",
8890
namespace,
89-
"jobs",
90-
"true",
91-
)) as IResponse;
91+
plural: "jobs",
92+
pretty: "true",
93+
});
9294
}
9395

94-
let filteredJobs = response.body.items || [];
96+
let filteredJobs = response.items || [];
9597

9698
if (searchTerm) {
9799
filteredJobs = filteredJobs.filter((job) =>
@@ -127,18 +129,19 @@ export const getJobs = async (
127129
}
128130
};
129131

130-
// Add an interface to obtain a single job
132+
// @desc Add an interface to obtain a single job
133+
// @route GET /jobs/:namespace/:name
131134
export const getJobByName = async (req: Request, res: Response) => {
132135
try {
133136
const { namespace, name } = req.params;
134-
const response = await k8sApi.getNamespacedCustomObject(
135-
"batch.volcano.sh",
136-
"v1alpha1",
137+
const response: IResponse = await k8sApi.getNamespacedCustomObject({
138+
group: "batch.volcano.sh",
139+
version: "v1alpha1",
137140
namespace,
138-
"jobs",
141+
plural: "jobs",
139142
name,
140-
);
141-
res.json(response.body);
143+
});
144+
res.json(response);
142145
} catch (err) {
143146
console.error("Error fetching job:", err);
144147
res.status(500).json({
@@ -152,22 +155,23 @@ interface JobParams {
152155
namespace: string;
153156
name: string;
154157
}
155-
// Add a route to obtain YAML in server.js
158+
//@desc Add a route to obtain YAML for a single job
159+
//@route GET /jobs/:namespace/:name/yaml
156160
export const getJobYamlByName = async (
157161
req: Request<JobParams>,
158162
res: Response,
159163
) => {
160164
try {
161165
const { namespace, name } = req.params;
162-
const response = (await k8sApi.getNamespacedCustomObject(
163-
"batch.volcano.sh",
164-
"v1alpha1",
166+
const response: IResponse = await k8sApi.getNamespacedCustomObject({
167+
group: "batch.volcano.sh",
168+
version: "v1alpha1",
165169
namespace,
166-
"jobs",
170+
plural: "jobs",
167171
name,
168-
)) as IResponse;
172+
});
169173

170-
const formattedYaml = yaml.dump(response.body, {
174+
const formattedYaml = yaml.dump(response, {
171175
indent: 2,
172176
lineWidth: -1,
173177
noRefs: true,

backend/src/controllers/namespace.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Request, Response } from "express";
22
import { k8sCoreApi } from "../config/kubernetes.js";
33

4-
// get all ns
4+
// @desc Get all namespaces
5+
// @route GET /namespaces
56
export const getNamespaces = async (req: Request, res: Response) => {
67
try {
78
const response = await k8sCoreApi.listNamespace();
89

910
res.json({
10-
items: response.body.items,
11+
items: response.items,
1112
});
1213
} catch (error) {
1314
console.error("Error fetching namespaces:", error);

backend/src/controllers/pod.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { Request, Response } from "express";
22
import { k8sCoreApi } from "../config/kubernetes.js";
33
import yaml from "js-yaml";
44

5-
// Get all Pods (no pagination)
5+
// @desc Get all pods (no pagination)
6+
// @route GET /all-pods
67
export const getAllPods = async (req: Request, res: Response) => {
78
try {
89
const response = await k8sCoreApi.listPodForAllNamespaces();
910
res.json({
10-
items: response.body.items,
11-
totalCount: response.body.items.length,
11+
items: response.items,
12+
totalCount: response.items.length,
1213
});
1314
} catch (error) {
1415
console.error("Error fetching all pods:", error);
@@ -21,6 +22,9 @@ interface PodQueryParams {
2122
search?: string;
2223
status?: string;
2324
}
25+
26+
// @desc Get all pods with optional filters
27+
// @route GET /pods
2428
export const getPods = async (
2529
req: Request<{}, {}, {}, PodQueryParams>,
2630
res: Response,
@@ -40,10 +44,10 @@ export const getPods = async (
4044
if (namespace === "" || namespace === "All") {
4145
response = await k8sCoreApi.listPodForAllNamespaces();
4246
} else {
43-
response = await k8sCoreApi.listNamespacedPod(namespace);
47+
response = await k8sCoreApi.listNamespacedPod({ namespace });
4448
}
4549

46-
let filteredPods = response.body.items || [];
50+
let filteredPods = response.items || [];
4751

4852
// Apply search filter
4953
if (searchTerm) {
@@ -73,14 +77,18 @@ export const getPods = async (
7377
}
7478
};
7579

76-
// Get details of a specific Pod
80+
// @desc Get pod details by name
81+
// @route GET /pods/:namespace/:name
7782
export const getPodYamlByName = async (req: Request, res: Response) => {
7883
try {
7984
const { namespace, name } = req.params;
80-
const response = await k8sCoreApi.readNamespacedPod(name, namespace);
85+
const response = await k8sCoreApi.readNamespacedPod({
86+
name,
87+
namespace,
88+
});
8189

8290
// Convert JSON to formatted YAML
83-
const formattedYaml = yaml.dump(response.body, {
91+
const formattedYaml = yaml.dump(response, {
8492
indent: 2,
8593
lineWidth: -1,
8694
noRefs: true,

backend/src/controllers/queue.ts

+38-31
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import http from "http";
55
import { IQueue } from "../types/index";
66

77
interface IResponse {
8-
response: http.IncomingMessage;
9-
body: { items: IQueue[] };
8+
items: IQueue[];
109
}
11-
// Get all Queues (no pagination)
10+
// @desc Get all Queues (no pagination)
11+
// @route GET /all-queues
1212
export const getAllQueues = async (req: Request, res: Response) => {
1313
try {
14-
const response = (await k8sApi.listClusterCustomObject(
15-
"scheduling.volcano.sh",
16-
"v1beta1",
17-
"queues",
18-
)) as IResponse;
14+
const response: IResponse = await k8sApi.listClusterCustomObject({
15+
group: "scheduling.volcano.sh",
16+
version: "v1beta1",
17+
plural: "queues",
18+
});
1919
res.json({
20-
items: response.body.items,
21-
totalCount: response.body.items.length,
20+
items: response.items,
21+
totalCount: response.items.length,
2222
});
2323
} catch (error) {
2424
console.error("Error fetching all queues:", error);
@@ -31,7 +31,9 @@ interface QueueQueryParams {
3131
search?: string;
3232
state?: string;
3333
}
34-
// Get all Volcano Queues
34+
35+
// @desc Get all queues with optional filters
36+
// @route GET /queues
3537
export const getQueues = async (
3638
req: Request<{}, {}, {}, QueueQueryParams>,
3739
res: Response,
@@ -49,13 +51,13 @@ export const getQueues = async (
4951
stateFilter,
5052
});
5153

52-
const response = (await k8sApi.listClusterCustomObject(
53-
"scheduling.volcano.sh",
54-
"v1beta1",
55-
"queues",
56-
)) as IResponse;
54+
const response: IResponse = await k8sApi.listClusterCustomObject({
55+
group: "scheduling.volcano.sh",
56+
version: "v1beta1",
57+
plural: "queues",
58+
});
5759

58-
let filteredQueues = response.body.items || [];
60+
let filteredQueues = response.items || [];
5961

6062
if (searchTerm) {
6163
filteredQueues = filteredQueues.filter((queue) =>
@@ -92,30 +94,35 @@ export const getQueues = async (
9294
}
9395
};
9496

95-
// Get details of a specific Queue
97+
// @desc Get queue details by name
98+
// @route GET /queues/:name
9699
export const getQueueByName = async (req: Request, res: Response) => {
100+
const { name } = req.params;
97101
try {
98-
const response = await k8sApi.getClusterCustomObject(
99-
"scheduling.volcano.sh",
100-
"v1beta1",
101-
"queues",
102-
req.params.name,
103-
);
104-
res.json(response.body);
102+
const response = await k8sApi.getClusterCustomObject({
103+
group: "scheduling.volcano.sh",
104+
version: "v1beta1",
105+
plural: "queues",
106+
name,
107+
});
108+
res.json(response);
105109
} catch (error) {
106110
console.error("Error fetching queue details:", error);
107111
res.status(500).json({ error: "Failed to fetch queue details" });
108112
}
109113
};
110114

115+
// @desc Get queue YAML by name
116+
// @route GET /queues/:name/yaml
111117
export const getQueueYamlByName = async (req: Request, res: Response) => {
118+
const { name } = req.params;
112119
try {
113-
const response = await k8sApi.getClusterCustomObject(
114-
"scheduling.volcano.sh",
115-
"v1beta1",
116-
"queues",
117-
req.params.name,
118-
);
120+
const response = await k8sApi.getClusterCustomObject({
121+
group: "scheduling.volcano.sh",
122+
version: "v1beta1",
123+
plural: "queues",
124+
name,
125+
});
119126

120127
const formattedYaml = yaml.dump(response.body, {
121128
indent: 2,

backend/src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { components as components_queue } from "./queue";
33

44
export type IJob = Omit<components_job["schemas"]["Job"], "metadata"> & {
55
metadata?: Record<string, string>;
6+
status: string;
67
};
78
export type IQueue = Omit<components_queue["schemas"]["Queue"], "metadata"> & {
89
metadata?: Record<string, string>;

0 commit comments

Comments
 (0)