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" 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+ function AggregateStatus(desiredObj, statusItems)
96+ if statusItems == nil then return desiredObj end
97+ if desiredObj.status == nil then desiredObj.status = {} end
98+
99+ local status = {
100+ state = {},
101+ minAvailable = 0,
102+ taskStatusCount = {},
103+ pending = 0,
104+ running = 0,
105+ succeeded = 0,
106+ failed = 0,
107+ terminating = 0,
108+ unknown = 0,
109+ version = 0,
110+ retryCount = 0,
111+ controlledResources = {},
112+ conditions = {},
113+ runningDuration = "0s",
114+ state = {}
115+ }
116+
117+ for i = 1, #statusItems do
118+ local s = statusItems[i].status
119+ if s ~= nil then
120+ status.minAvailable = math.max(status.minAvailable, s.minAvailable or 0)
121+ status.pending = status.pending + (s.pending or 0)
122+ status.running = status.running + (s.running or 0)
123+ status.succeeded = status.succeeded + (s.succeeded or 0)
124+ status.failed = status.failed + (s.failed or 0)
125+ status.terminating = status.terminating + (s.terminating or 0)
126+ status.unknown = status.unknown + (s.unknown or 0)
127+ status.version = math.max(status.version, s.version or 0)
128+ status.retryCount = status.retryCount + (s.retryCount or 0)
129+ status.state = s.state
130+ if durationVal(s.runningDuration) > durationVal(status.runningDuration) then
131+ status.runningDuration = s.runningDuration
132+ end
133+
134+ if s.taskStatusCount ~= nil then
135+ for taskName, taskStatus in pairs(s.taskStatusCount) do
136+ if status.taskStatusCount[taskName] == nil then
137+ status.taskStatusCount[taskName] = { phase = {} }
138+ end
139+ if taskStatus.phase ~= nil then
140+ for phaseName, count in pairs(taskStatus.phase) do
141+ status.taskStatusCount[taskName].phase[phaseName] = (status.taskStatusCount[taskName].phase[phaseName] or 0) + count
142+ end
143+ end
144+ end
145+ end
146+ if s.controlledResources then
147+ for k, v in pairs(s.controlledResources) do
148+ status.controlledResources[k] = v
149+ end
150+ end
151+
152+ if s.conditions ~= nil then
153+ for _, c in ipairs(s.conditions) do
154+ table.insert(status.conditions, c)
155+ end
156+ end
157+ end
158+ end
159+
160+ desiredObj.status = status
161+ return desiredObj
162+ end
163+ statusReflection :
164+ luaScript : >
165+ function ReflectStatus(observedObj)
166+ local status = {}
167+
168+ if observedObj == nil or observedObj.status == nil then
169+ return status
170+ end
171+
172+ local s = observedObj.status
173+ status.minAvailable = s.minAvailable
174+ status.pending = s.pending
175+ status.running = s.running
176+ status.succeeded = s.succeeded
177+ status.failed = s.failed
178+ status.terminating = s.terminating
179+ status.unknown = s.unknown
180+ status.version = s.version
181+ status.retryCount = s.retryCount
182+ status.runningDuration = s.runningDuration
183+
184+ status.taskStatusCount = {}
185+ if s.taskStatusCount ~= nil then
186+ for k, v in pairs(s.taskStatusCount) do
187+ status.taskStatusCount[k] = v
188+ end
189+ end
190+
191+ status.controlledResources = {}
192+ if s.controlledResources ~= nil then
193+ for k, v in pairs(s.controlledResources) do
194+ status.controlledResources[k] = v
195+ end
196+ end
197+
198+ if s.state ~= nil then
199+ status.state = s.state
200+ end
201+
202+ status.conditions = {}
203+ if type(s.conditions) == "table" then
204+ for _, cond in ipairs(s.conditions) do
205+ table.insert(status.conditions, cond)
206+ end
207+ end
208+
209+ return status
210+ end
0 commit comments