|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import { k8sApi } from "../config/kubernetes"; |
| 3 | +import http from "http"; |
| 4 | +import yaml from "js-yaml"; |
| 5 | +import { IJob } from "../types/index"; |
| 6 | + |
| 7 | +interface IResponse { |
| 8 | + items: IJob[]; |
| 9 | +} |
| 10 | + |
| 11 | +// Auxiliary function: determine the status based on the job status |
| 12 | +function getJobState(job: IJob) { |
| 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"; |
| 17 | + return job.status || "Unknown"; |
| 18 | +} |
| 19 | + |
| 20 | +// @desc Get all Jobs (no pagination) |
| 21 | +// @route GET /all-jobs |
| 22 | +export const getAllJobs = async (req: Request, res: Response) => { |
| 23 | + try { |
| 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) => ({ |
| 32 | + ...job, |
| 33 | + status: { |
| 34 | + state: job.status?.state || getJobState(job), |
| 35 | + phase: |
| 36 | + job.status?.state?.phase || job.spec?.minAvailable |
| 37 | + ? "Running" |
| 38 | + : "Unknown", |
| 39 | + }, |
| 40 | + })); |
| 41 | + |
| 42 | + res.json({ |
| 43 | + items: jobs, |
| 44 | + totalCount: jobs.length, |
| 45 | + }); |
| 46 | + } catch (err) { |
| 47 | + console.error("Error fetching all jobs:", err); |
| 48 | + res.status(500).json({ error: "Failed to fetch all jobs" }); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +interface JobQueryParams { |
| 53 | + namespace?: string; |
| 54 | + search?: string; |
| 55 | + queue?: string; |
| 56 | + status?: string; |
| 57 | +} |
| 58 | + |
| 59 | +// @desc Get Jobs with pagination |
| 60 | +// @route GET /jobs |
| 61 | +export const getJobs = async ( |
| 62 | + req: Request<{}, {}, {}, JobQueryParams>, |
| 63 | + res: Response, |
| 64 | +) => { |
| 65 | + try { |
| 66 | + const namespace = req.query.namespace || ""; |
| 67 | + const searchTerm = req.query.search || ""; |
| 68 | + const queueFilter = req.query.queue || ""; |
| 69 | + const statusFilter = req.query.status || ""; |
| 70 | + |
| 71 | + console.log("Fetching jobs with params:", { |
| 72 | + namespace, |
| 73 | + searchTerm, |
| 74 | + queueFilter, |
| 75 | + statusFilter, |
| 76 | + }); |
| 77 | + |
| 78 | + let response: IResponse; |
| 79 | + if (namespace === "" || namespace === "All") { |
| 80 | + response = await k8sApi.listClusterCustomObject({ |
| 81 | + group: "batch.volcano.sh", |
| 82 | + version: "v1alpha1", |
| 83 | + plural: "jobs", |
| 84 | + pretty: "true", |
| 85 | + }); |
| 86 | + } else { |
| 87 | + response = await k8sApi.listNamespacedCustomObject({ |
| 88 | + group: "batch.volcano.sh", |
| 89 | + version: "v1alpha1", |
| 90 | + namespace, |
| 91 | + plural: "jobs", |
| 92 | + pretty: "true", |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + let filteredJobs = response.items || []; |
| 97 | + |
| 98 | + if (searchTerm) { |
| 99 | + filteredJobs = filteredJobs.filter((job) => |
| 100 | + job.metadata?.name |
| 101 | + .toLowerCase() |
| 102 | + .includes(searchTerm.toLowerCase()), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + // Apply queueFilter filtering |
| 107 | + if (queueFilter && queueFilter !== "All") { |
| 108 | + filteredJobs = filteredJobs.filter( |
| 109 | + (job) => job.spec?.queue === queueFilter, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + if (statusFilter && statusFilter !== "All") { |
| 114 | + filteredJobs = filteredJobs.filter( |
| 115 | + (job) => job.status?.state?.phase === statusFilter, |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + res.json({ |
| 120 | + items: filteredJobs, |
| 121 | + totalCount: filteredJobs.length, |
| 122 | + }); |
| 123 | + } catch (err) { |
| 124 | + console.error("Error fetching jobs:", err); |
| 125 | + res.status(500).json({ |
| 126 | + error: "Failed to fetch jobs", |
| 127 | + details: (err as Error).message, |
| 128 | + }); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +// @desc Add an interface to obtain a single job |
| 133 | +// @route GET /jobs/:namespace/:name |
| 134 | +export const getJobByName = async (req: Request, res: Response) => { |
| 135 | + try { |
| 136 | + const { namespace, name } = req.params; |
| 137 | + const response: IResponse = await k8sApi.getNamespacedCustomObject({ |
| 138 | + group: "batch.volcano.sh", |
| 139 | + version: "v1alpha1", |
| 140 | + namespace, |
| 141 | + plural: "jobs", |
| 142 | + name, |
| 143 | + }); |
| 144 | + res.json(response); |
| 145 | + } catch (err) { |
| 146 | + console.error("Error fetching job:", err); |
| 147 | + res.status(500).json({ |
| 148 | + error: "Failed to fetch job", |
| 149 | + details: (err as Error).message, |
| 150 | + }); |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +interface JobParams { |
| 155 | + namespace: string; |
| 156 | + name: string; |
| 157 | +} |
| 158 | +//@desc Add a route to obtain YAML for a single job |
| 159 | +//@route GET /jobs/:namespace/:name/yaml |
| 160 | +export const getJobYamlByName = async ( |
| 161 | + req: Request<JobParams>, |
| 162 | + res: Response, |
| 163 | +) => { |
| 164 | + try { |
| 165 | + const { namespace, name } = req.params; |
| 166 | + const response: IResponse = await k8sApi.getNamespacedCustomObject({ |
| 167 | + group: "batch.volcano.sh", |
| 168 | + version: "v1alpha1", |
| 169 | + namespace, |
| 170 | + plural: "jobs", |
| 171 | + name, |
| 172 | + }); |
| 173 | + |
| 174 | + const formattedYaml = yaml.dump(response, { |
| 175 | + indent: 2, |
| 176 | + lineWidth: -1, |
| 177 | + noRefs: true, |
| 178 | + sortKeys: false, |
| 179 | + }); |
| 180 | + |
| 181 | + res.setHeader("Content-Type", "text/yaml"); |
| 182 | + res.send(formattedYaml); |
| 183 | + } catch (err) { |
| 184 | + console.error("Error fetching job YAML:", err); |
| 185 | + res.status(500).json({ |
| 186 | + error: "Failed to fetch job YAML", |
| 187 | + details: (err as Error).message, |
| 188 | + }); |
| 189 | + } |
| 190 | +}; |
0 commit comments