Skip to content

Commit 11d31f9

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

File tree

5 files changed

+292
-0
lines changed

5 files changed

+292
-0
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
replicaRevision:
11+
luaScript: >
12+
function ReviseReplica(obj, replica)
13+
obj.spec.minAvailable = replica
14+
return obj
15+
end
16+
healthInterpretation:
17+
luaScript: >
18+
function InterpretHealth(observedObj)
19+
if observedObj.status == nil then
20+
return false
21+
end
22+
if observedObj.status.state.phase == nil or observedObj.status.state.phase == '' or observedObj.status.state.phase == 'Failed' or observedObj.status.state.phase == 'Terminated' or observedObj.status.state.phase == 'Terminating' then
23+
return false
24+
end
25+
return true
26+
end
27+
componentResource:
28+
luaScript: |
29+
local kube = require("kube")
30+
local function isempty(s)
31+
return s == nil or s == ''
32+
end
33+
34+
local function get(obj, path)
35+
local cur = obj
36+
for i = 1, #path do
37+
if cur == nil then return nil end
38+
cur = cur[path[i]]
39+
end
40+
return cur
41+
end
42+
43+
local function clone_plain(val, seen)
44+
local tv = type(val)
45+
if tv ~= "table" then return val end
46+
seen = seen or {}
47+
if seen[val] then return nil end
48+
seen[val] = true
49+
local out = {}
50+
for k, v in pairs(val) do
51+
local tk = type(k)
52+
if tk == "string" or tk == "number" then
53+
local cv = clone_plain(v, seen)
54+
if cv ~= nil then out[k] = cv end
55+
end
56+
end
57+
seen[val] = nil
58+
return out
59+
end
60+
61+
local function apply_pod_template(pt_spec, requires)
62+
if pt_spec == nil then return end
63+
local nodeSelector = clone_plain(pt_spec.nodeSelector)
64+
local tolerations = clone_plain(pt_spec.tolerations)
65+
local priority = pt_spec.priorityClassName
66+
67+
if nodeSelector ~= nil or tolerations ~= nil then
68+
requires.nodeClaim = requires.nodeClaim or {}
69+
requires.nodeClaim.nodeSelector = nodeSelector
70+
requires.nodeClaim.tolerations = tolerations
71+
end
72+
73+
if not isempty(priority) then
74+
requires.priorityClassName = priority
75+
end
76+
end
77+
78+
local function to_num(v, default)
79+
if v == nil or v == '' then
80+
return default
81+
end
82+
local n = tonumber(v)
83+
if n ~= nil then return n end
84+
return default
85+
end
86+
87+
function GetComponents(observedObj)
88+
local components = {}
89+
90+
local tasks = get(observedObj, {"spec","tasks"})
91+
if tasks == nil then
92+
return components
93+
end
94+
95+
for _, task in ipairs(tasks) do
96+
local pt_spec = get(task, {"template","spec"}) or {}
97+
98+
local replicas = to_num(task.minAvailable, 1)
99+
100+
local requires = { resourceRequest = {} }
101+
local containers = get(task, {"template", "spec", "containers"})
102+
if containers ~= nil then
103+
for _, container in ipairs(containers) do
104+
if container.resources and container.resources.requests then
105+
for name, value in pairs(container.resources.requests) do
106+
if requires.resourceRequest[name] then
107+
requires.resourceRequest[name] = kube.resourceAdd(requires.resourceRequest[name], value)
108+
else
109+
requires.resourceRequest[name] = value
110+
end
111+
end
112+
end
113+
end
114+
end
115+
apply_pod_template(pt_spec, requires)
116+
117+
table.insert(components, {
118+
name = task.name,
119+
replicas = replicas,
120+
replicaRequirements = requires
121+
})
122+
end
123+
124+
return components
125+
end
126+
statusAggregation:
127+
luaScript: >
128+
local function durationVal(d)
129+
if type(d) == "string" then
130+
return tonumber(string.match(d, "([%d%.]+)")) or 0
131+
elseif type(d) == "number" then
132+
return d
133+
else
134+
return 0
135+
end
136+
end
137+
function AggregateStatus(desiredObj, statusItems)
138+
if statusItems == nil then return desiredObj end
139+
if desiredObj.status == nil then desiredObj.status = {} end
140+
141+
local status = {
142+
state = {},
143+
minAvailable = 0,
144+
taskStatusCount = {},
145+
pending = 0,
146+
running = 0,
147+
succeeded = 0,
148+
failed = 0,
149+
terminating = 0,
150+
unknown = 0,
151+
version = 0,
152+
retryCount = 0,
153+
controlledResources = {},
154+
conditions = {},
155+
runningDuration = "0s"
156+
}
157+
158+
for i = 1, #statusItems do
159+
local s = statusItems[i].status
160+
if s ~= nil then
161+
status.minAvailable = math.max(status.minAvailable, s.minAvailable or 0)
162+
status.pending = status.pending + (s.pending or 0)
163+
status.running = status.running + (s.running or 0)
164+
status.succeeded = status.succeeded + (s.succeeded or 0)
165+
status.failed = status.failed + (s.failed or 0)
166+
status.terminating = status.terminating + (s.terminating or 0)
167+
status.unknown = status.unknown + (s.unknown or 0)
168+
status.version = math.max(status.version, s.version or 0)
169+
status.retryCount = status.retryCount + (s.retryCount or 0)
170+
if durationVal(s.runningDuration) > durationVal(status.runningDuration) then
171+
status.runningDuration = s.runningDuration
172+
end
173+
174+
if s.taskStatusCount ~= nil then
175+
for taskName, taskStatus in pairs(s.taskStatusCount) do
176+
if status.taskStatusCount[taskName] == nil then
177+
status.taskStatusCount[taskName] = { phase = {} }
178+
end
179+
if taskStatus.phase ~= nil then
180+
for phaseName, count in pairs(taskStatus.phase) do
181+
status.taskStatusCount[taskName].phase[phaseName] = (status.taskStatusCount[taskName].phase[phaseName] or 0) + count
182+
end
183+
end
184+
end
185+
end
186+
if s.controlledResources then
187+
for k, v in pairs(s.controlledResources) do
188+
status.controlledResources[k] = v
189+
end
190+
end
191+
192+
if s.conditions ~= nil then
193+
for _, c in ipairs(s.conditions) do
194+
table.insert(status.conditions, c)
195+
end
196+
end
197+
end
198+
end
199+
200+
desiredObj.status = status
201+
return desiredObj
202+
end
203+
statusReflection:
204+
luaScript: >
205+
function ReflectStatus(observedObj)
206+
local status = {}
207+
208+
if observedObj == nil or observedObj.status == nil then
209+
return status
210+
end
211+
212+
local s = observedObj.status
213+
status.state = s.state
214+
status.minAvailable = s.minAvailable
215+
status.pending = s.pending
216+
status.running = s.running
217+
status.succeeded = s.succeeded
218+
status.failed = s.failed
219+
status.terminating = s.terminating
220+
status.unknown = s.unknown
221+
status.version = s.version
222+
status.retryCount = s.retryCount
223+
status.runningDuration = s.runningDuration
224+
225+
status.taskStatusCount = {}
226+
if s.taskStatusCount ~= nil then
227+
for k, v in pairs(s.taskStatusCount) do
228+
status.taskStatusCount[k] = v
229+
end
230+
end
231+
232+
status.controlledResources = {}
233+
if s.controlledResources ~= nil then
234+
for k, v in pairs(s.controlledResources) do
235+
status.controlledResources[k] = v
236+
end
237+
end
238+
239+
if s.state ~= nil then
240+
status.state = s.state
241+
end
242+
243+
status.conditions = {}
244+
if type(s.conditions) == "table" then
245+
for _, cond in ipairs(s.conditions) do
246+
table.insert(status.conditions, cond)
247+
end
248+
end
249+
250+
return status
251+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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: InterpretReplica
7+
- observedInputPath: testdata/observed-job.yaml
8+
operation: InterpretHealth
9+
- observedInputPath: testdata/observed-job.yaml
10+
operation: InterpretStatus

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/batch.volcano.sh/v1alpha1/Job/testdata/desired-job.yaml

Whitespace-only changes.

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/batch.volcano.sh/v1alpha1/Job/testdata/observed-job.yaml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
applied: true
2+
clusterName: member1
3+
health: Healthy
4+
status:
5+
conditions:
6+
- lastTransitionTime: "2025-09-24T12:19:51Z"
7+
status: Pending
8+
state:
9+
phase: Pending
10+
lastTransitionTime: "2025-09-24T12:19:51Z"
11+
controlledResources:
12+
plugin-env: env
13+
plugin-ssh: ssh
14+
plugin-svc: svc
15+
failed: 0
16+
minAvailable: 2
17+
pending: 1
18+
retryCount: 0
19+
running: 1
20+
succeeded: 0
21+
taskStatusCount:
22+
worker:
23+
phase:
24+
Pending: 1
25+
task2:
26+
phase:
27+
Running: 1
28+
terminating: 0
29+
unknown: 0
30+
version: 0
31+
runningDuration: 94h37m37.157435715s

0 commit comments

Comments
 (0)