Skip to content

Commit abdb0c4

Browse files
committed
add volcanojob
Signed-off-by: hudekai <2812840067@qq.com>
1 parent 4594ca0 commit abdb0c4

File tree

5 files changed

+414
-0
lines changed

5 files changed

+414
-0
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
apiVersion: config.karmada.io/v1alpha1
2+
kind: ResourceInterpreterCustomization
3+
metadata:
4+
name: declarative-configuration-job
5+
spec:
6+
target:
7+
apiVersion: batch.volcano.sh/v1alpha1
8+
kind: Job
9+
customizations:
10+
healthInterpretation:
11+
luaScript: >
12+
function InterpretHealth(observedObj)
13+
if observedObj.status == nil or observedObj.status.state == nil then
14+
return false
15+
end
16+
local phase = observedObj.status.state.phase
17+
if phase == nil or phase == '' then
18+
return false
19+
end
20+
if phase == 'Running' or phase == 'Completed' or phase == "Pending" or
21+
phase == "Aborting" or phase == "Aborted" or phase == "Restarting" or
22+
phase == "Completing" or phase == "Terminating" or phase == "Terminated" then
23+
return true
24+
end
25+
return false
26+
end
27+
componentResource:
28+
luaScript: |
29+
local kube = require("kube")
30+
31+
local function get(obj, path)
32+
local cur = obj
33+
for i = 1, #path do
34+
if cur == nil then return nil end
35+
cur = cur[path[i]]
36+
end
37+
return cur
38+
end
39+
40+
local function to_num(v, default)
41+
if v == nil or v == '' then
42+
return default
43+
end
44+
local n = tonumber(v)
45+
if n ~= nil then return n end
46+
return default
47+
end
48+
49+
function GetComponents(observedObj)
50+
local components = {}
51+
52+
local tasks = get(observedObj, {"spec","tasks"})
53+
if tasks == nil then
54+
return components
55+
end
56+
57+
for _, task in ipairs(tasks) do
58+
local replicas = to_num(task.minAvailable, 1)
59+
requires = kube.accuratePodRequirements(task.template)
60+
61+
table.insert(components, {
62+
name = task.name,
63+
replicas = replicas,
64+
replicaRequirements = requires
65+
})
66+
end
67+
68+
return components
69+
end
70+
statusAggregation:
71+
luaScript: >
72+
local function durationVal(d)
73+
if type(d) == "number" then
74+
return d
75+
end
76+
if type(d) ~= "string" then
77+
return 0
78+
end
79+
local totalSeconds = 0
80+
for num, unit in string.gmatch(d, "([%d%.]+)([hms])") do
81+
num = tonumber(num)
82+
if unit == "h" then
83+
totalSeconds = totalSeconds + num * 3600
84+
elseif unit == "m" then
85+
totalSeconds = totalSeconds + num * 60
86+
elseif unit == "s" then
87+
totalSeconds = totalSeconds + num
88+
end
89+
end
90+
if totalSeconds > 0 then
91+
return totalSeconds
92+
end
93+
return tonumber(d) or 0
94+
end
95+
local function omitEmpty(t)
96+
if t == nil then return nil end
97+
local out = {}
98+
for k, v in pairs(t) do
99+
if type(v) == "table" then
100+
local inner = omitEmpty(v)
101+
if inner ~= nil and next(inner) ~= nil then
102+
out[k] = inner
103+
end
104+
elseif v ~= nil and not (v == 0 or v == "" or v == "0s") then
105+
out[k] = v
106+
end
107+
end
108+
if next(out) ~= nil then
109+
return out
110+
else
111+
return nil
112+
end
113+
end
114+
function AggregateStatus(desiredObj, statusItems)
115+
if statusItems == nil then return desiredObj end
116+
if desiredObj.status == nil then desiredObj.status = {} end
117+
118+
local status = {
119+
state = {},
120+
minAvailable = 0,
121+
taskStatusCount = {},
122+
pending = 0,
123+
running = 0,
124+
succeeded = 0,
125+
failed = 0,
126+
terminating = 0,
127+
unknown = 0,
128+
version = 0,
129+
retryCount = 0,
130+
controlledResources = {},
131+
conditions = {},
132+
runningDuration = "0s",
133+
state = {}
134+
}
135+
136+
for i = 1, #statusItems do
137+
local s = statusItems[i].status
138+
if s ~= nil then
139+
status.minAvailable = status.minAvailable + (s.minAvailable or 0)
140+
status.pending = status.pending + (s.pending or 0)
141+
status.running = status.running + (s.running or 0)
142+
status.succeeded = status.succeeded + (s.succeeded or 0)
143+
status.failed = status.failed + (s.failed or 0)
144+
status.terminating = status.terminating + (s.terminating or 0)
145+
status.unknown = status.unknown + (s.unknown or 0)
146+
status.version = math.max(status.version, s.version or 0)
147+
status.retryCount = status.retryCount + (s.retryCount or 0)
148+
status.state = s.state
149+
if durationVal(s.runningDuration) > durationVal(status.runningDuration) then
150+
status.runningDuration = s.runningDuration
151+
end
152+
153+
if s.taskStatusCount ~= nil then
154+
for taskName, taskStatus in pairs(s.taskStatusCount) do
155+
if status.taskStatusCount[taskName] == nil then
156+
status.taskStatusCount[taskName] = { phase = {} }
157+
end
158+
if taskStatus.phase ~= nil then
159+
for phaseName, count in pairs(taskStatus.phase) do
160+
status.taskStatusCount[taskName].phase[phaseName] = (status.taskStatusCount[taskName].phase[phaseName] or 0) + count
161+
end
162+
end
163+
end
164+
end
165+
if s.controlledResources then
166+
for k, v in pairs(s.controlledResources) do
167+
status.controlledResources[k] = v
168+
end
169+
end
170+
171+
if s.conditions ~= nil then
172+
for _, c in ipairs(s.conditions) do
173+
table.insert(status.conditions, c)
174+
end
175+
end
176+
end
177+
end
178+
179+
desiredObj.status = omitEmpty(status) or {}
180+
return desiredObj
181+
end
182+
statusReflection:
183+
luaScript: >
184+
function ReflectStatus(observedObj)
185+
local status = {}
186+
187+
if observedObj == nil or observedObj.status == nil then
188+
return status
189+
end
190+
191+
local s = observedObj.status
192+
status.minAvailable = s.minAvailable
193+
status.pending = s.pending
194+
status.running = s.running
195+
status.succeeded = s.succeeded
196+
status.failed = s.failed
197+
status.terminating = s.terminating
198+
status.unknown = s.unknown
199+
status.version = s.version
200+
status.retryCount = s.retryCount
201+
status.runningDuration = s.runningDuration
202+
203+
status.taskStatusCount = {}
204+
if s.taskStatusCount ~= nil then
205+
for k, v in pairs(s.taskStatusCount) do
206+
status.taskStatusCount[k] = v
207+
end
208+
end
209+
210+
status.controlledResources = {}
211+
if s.controlledResources ~= nil then
212+
for k, v in pairs(s.controlledResources) do
213+
status.controlledResources[k] = v
214+
end
215+
end
216+
217+
if s.state ~= nil then
218+
status.state = s.state
219+
end
220+
221+
status.conditions = {}
222+
if type(s.conditions) == "table" then
223+
for _, cond in ipairs(s.conditions) do
224+
table.insert(status.conditions, cond)
225+
end
226+
end
227+
228+
return status
229+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests:
2+
- desiredInputPath: testdata/desired-job.yaml
3+
statusInputPath: testdata/status-file.yaml
4+
operation: AggregateStatus
5+
- observedInputPath: testdata/observed-job.yaml
6+
operation: InterpretHealth
7+
- observedInputPath: testdata/observed-job.yaml
8+
operation: InterpretStatus
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
apiVersion: batch.volcano.sh/v1alpha1
2+
kind: Job
3+
metadata:
4+
name: dk-job
5+
spec:
6+
maxRetry: 3
7+
minAvailable: 3
8+
plugins:
9+
env: []
10+
ssh: []
11+
svc:
12+
- --disable-network-policy=true
13+
queue: default
14+
schedulerName: volcano
15+
tasks:
16+
- minAvailable: 1
17+
name: job-nginx1
18+
replicas: 1
19+
template:
20+
metadata:
21+
name: nginx1
22+
spec:
23+
containers:
24+
- args:
25+
- sleep 10
26+
command:
27+
- bash
28+
- -c
29+
image: nginx:latest
30+
imagePullPolicy: IfNotPresent
31+
name: nginx
32+
resources:
33+
requests:
34+
cpu: 100m
35+
nodeSelector:
36+
kubernetes.io/os: linux
37+
restartPolicy: OnFailure
38+
- minAvailable: 2
39+
name: job-nginx2
40+
replicas: 3
41+
template:
42+
metadata:
43+
name: nginx2
44+
spec:
45+
containers:
46+
- args:
47+
- sleep 30
48+
command:
49+
- bash
50+
- -c
51+
image: nginx:latest
52+
imagePullPolicy: IfNotPresent
53+
name: nginx
54+
resources:
55+
requests:
56+
cpu: 100m
57+
nodeSelector:
58+
kubernetes.io/os: linux
59+
restartPolicy: OnFailure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
apiVersion: batch.volcano.sh/v1alpha1
2+
kind: Job
3+
metadata:
4+
name: dk-job
5+
spec:
6+
maxRetry: 3
7+
minAvailable: 3
8+
plugins:
9+
env: []
10+
ssh: []
11+
svc:
12+
- --disable-network-policy=true
13+
queue: default
14+
schedulerName: volcano
15+
tasks:
16+
- minAvailable: 1
17+
name: job-nginx1
18+
replicas: 1
19+
template:
20+
metadata:
21+
name: nginx1
22+
spec:
23+
containers:
24+
- args:
25+
- sleep 10
26+
command:
27+
- bash
28+
- -c
29+
image: nginx:latest
30+
imagePullPolicy: IfNotPresent
31+
name: nginx
32+
resources:
33+
requests:
34+
cpu: 100m
35+
nodeSelector:
36+
kubernetes.io/os: linux
37+
restartPolicy: OnFailure
38+
- minAvailable: 2
39+
name: job-nginx2
40+
replicas: 3
41+
template:
42+
metadata:
43+
name: nginx2
44+
spec:
45+
containers:
46+
- args:
47+
- sleep 30
48+
command:
49+
- bash
50+
- -c
51+
image: nginx:latest
52+
imagePullPolicy: IfNotPresent
53+
name: nginx
54+
resources:
55+
requests:
56+
cpu: 100m
57+
nodeSelector:
58+
kubernetes.io/os: linux
59+
restartPolicy: OnFailure
60+
status:
61+
conditions:
62+
- lastTransitionTime: "2025-09-29T10:57:03Z"
63+
status: Pending
64+
- lastTransitionTime: "2025-09-29T10:57:07Z"
65+
status: Running
66+
- lastTransitionTime: "2025-09-29T10:57:38Z"
67+
status: Completed
68+
failed: 0
69+
minAvailable: 3
70+
pending: 0
71+
retryCount: 0
72+
running: 0
73+
runningDuration: 35.688396584s
74+
state:
75+
lastTransitionTime: "2025-09-29T10:57:38Z"
76+
phase: Completed
77+
succeeded: 4
78+
taskStatusCount:
79+
job-nginx1:
80+
phase:
81+
Succeeded: 1
82+
job-nginx2:
83+
phase:
84+
Succeeded: 3
85+
terminating: 0
86+
unknown: 0
87+
version: 1

0 commit comments

Comments
 (0)