-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjobController.js
113 lines (102 loc) · 3.28 KB
/
jobController.js
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
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';
// Get all jobs with filters
export const getAllJobs = async (req, res) => {
try {
const { namespace = '', search = '', queue = '', status = '' } = req.query;
let response;
if (namespace === '' || namespace === 'All') {
response = await k8sApi.listClusterCustomObject('batch.volcano.sh', 'v1alpha1', 'jobs', true);
} else {
response = await k8sApi.listNamespacedCustomObject('batch.volcano.sh', 'v1alpha1', namespace, 'jobs', true);
}
let filteredJobs = response.body.items || [];
if (search) {
filteredJobs = filteredJobs.filter(job =>
job.metadata.name.toLowerCase().includes(search.toLowerCase())
);
}
if (queue && queue !== 'All') {
filteredJobs = filteredJobs.filter(job => job.spec.queue === queue);
}
if (status && status !== 'All') {
filteredJobs = filteredJobs.filter(job => job.status.state.phase === status);
}
res.json({
items: filteredJobs,
totalCount: filteredJobs.length,
});
} catch (err) {
console.error('Error fetching jobs:', err);
res.status(500).json({
error: 'Failed to fetch jobs',
details: err.message,
});
}
};
// Get details of a specific job
export const getJobDetails = async (req, res) => {
try {
const { namespace, name } = req.params;
const response = await k8sApi.getNamespacedCustomObject(
'batch.volcano.sh',
'v1alpha1',
namespace,
'jobs',
name
);
res.json(response.body);
} catch (err) {
console.error('Error fetching job:', err);
res.status(500).json({
error: 'Failed to fetch job',
details: err.message,
});
}
};
// Get YAML of a specific job
export const getJobYaml = async (req, res) => {
try {
const { namespace, name } = req.params;
const response = await k8sApi.getNamespacedCustomObject(
'batch.volcano.sh',
'v1alpha1',
namespace,
'jobs',
name
);
const formattedYaml = yaml.dump(response.body, {
indent: 2,
lineWidth: -1,
noRefs: true,
sortKeys: false,
});
res.setHeader('Content-Type', 'text/yaml');
res.send(formattedYaml);
} catch (error) {
console.error('Error fetching job YAML:', error);
res.status(500).json({
error: 'Failed to fetch job YAML',
details: error.message,
});
}
};
// Get all jobs without pagination
export const getAllJobsWithoutPagination = async (req, res) => {
try {
const response = await k8sApi.listClusterCustomObject(
'batch.volcano.sh',
'v1alpha1',
'jobs',
{
pretty: true,
}
);
res.json({
items: response.body.items,
totalCount: response.body.items.length,
});
} catch (err) {
console.error('Error fetching all jobs:', err);
res.status(500).json({ error: 'Failed to fetch all jobs' });
}
};