|
| 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 |
0 commit comments