@@ -5,36 +5,35 @@ import yaml from "js-yaml";
5
5
import { IJob } from "../types/index" ;
6
6
7
7
interface IResponse {
8
- response : http . IncomingMessage ;
9
- body : { items : IJob [ ] } ;
8
+ items : IJob [ ] ;
10
9
}
11
10
12
11
// Auxiliary function: determine the status based on the job status
13
12
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" ;
19
17
return job . status || "Unknown" ;
20
18
}
21
19
22
- // Get all Jobs (no pagination)
20
+ // @desc Get all Jobs (no pagination)
21
+ // @route GET /all-jobs
23
22
export const getAllJobs = async ( req : Request , res : Response ) => {
24
23
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 ) => ( {
33
32
...job ,
34
33
status : {
35
34
state : job . status ?. state || getJobState ( job ) ,
36
35
phase :
37
- job . status ?. phase || job . spec ?. minAvailable
36
+ job . status ?. state ?. phase || job . spec ?. minAvailable
38
37
? "Running"
39
38
: "Unknown" ,
40
39
} ,
@@ -56,6 +55,9 @@ interface JobQueryParams {
56
55
queue ?: string ;
57
56
status ?: string ;
58
57
}
58
+
59
+ // @desc Get Jobs with pagination
60
+ // @route GET /jobs
59
61
export const getJobs = async (
60
62
req : Request < { } , { } , { } , JobQueryParams > ,
61
63
res : Response ,
@@ -75,23 +77,23 @@ export const getJobs = async (
75
77
76
78
let response : IResponse ;
77
79
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
+ } ) ;
84
86
} 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" ,
88
90
namespace,
89
- "jobs" ,
90
- "true" ,
91
- ) ) as IResponse ;
91
+ plural : "jobs" ,
92
+ pretty : "true" ,
93
+ } ) ;
92
94
}
93
95
94
- let filteredJobs = response . body . items || [ ] ;
96
+ let filteredJobs = response . items || [ ] ;
95
97
96
98
if ( searchTerm ) {
97
99
filteredJobs = filteredJobs . filter ( ( job ) =>
@@ -127,18 +129,19 @@ export const getJobs = async (
127
129
}
128
130
} ;
129
131
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
131
134
export const getJobByName = async ( req : Request , res : Response ) => {
132
135
try {
133
136
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" ,
137
140
namespace,
138
- "jobs" ,
141
+ plural : "jobs" ,
139
142
name,
140
- ) ;
141
- res . json ( response . body ) ;
143
+ } ) ;
144
+ res . json ( response ) ;
142
145
} catch ( err ) {
143
146
console . error ( "Error fetching job:" , err ) ;
144
147
res . status ( 500 ) . json ( {
@@ -152,22 +155,23 @@ interface JobParams {
152
155
namespace : string ;
153
156
name : string ;
154
157
}
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
156
160
export const getJobYamlByName = async (
157
161
req : Request < JobParams > ,
158
162
res : Response ,
159
163
) => {
160
164
try {
161
165
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" ,
165
169
namespace,
166
- "jobs" ,
170
+ plural : "jobs" ,
167
171
name,
168
- ) ) as IResponse ;
172
+ } ) ;
169
173
170
- const formattedYaml = yaml . dump ( response . body , {
174
+ const formattedYaml = yaml . dump ( response , {
171
175
indent : 2 ,
172
176
lineWidth : - 1 ,
173
177
noRefs : true ,
0 commit comments