|
| 1 | +apiVersion: config.karmada.io/v1alpha1 |
| 2 | +kind: ResourceInterpreterCustomization |
| 3 | +metadata: |
| 4 | + name: declarative-configuration-raycluster |
| 5 | +spec: |
| 6 | + target: |
| 7 | + apiVersion: ray.io/v1 |
| 8 | + kind: RayCluster |
| 9 | + customizations: |
| 10 | + componentResource: |
| 11 | + luaScript: | |
| 12 | + local kube = require("kube") |
| 13 | +
|
| 14 | + local function isempty(s) |
| 15 | + return s == nil or s == '' |
| 16 | + end |
| 17 | +
|
| 18 | + function GetComponents(desiredObj) |
| 19 | + local components = {} |
| 20 | +
|
| 21 | + -- Head component (always 1 replica) |
| 22 | + if desiredObj.spec ~= nil and desiredObj.spec.headGroupSpec ~= nil and desiredObj.spec.headGroupSpec.template ~= nil then |
| 23 | + local headRequires = kube.accuratePodRequirements(desiredObj.spec.headGroupSpec.template) |
| 24 | + local headComponent = { |
| 25 | + name = "ray-head", |
| 26 | + replicas = 1, |
| 27 | + replicaRequirements = headRequires |
| 28 | + } |
| 29 | + table.insert(components, headComponent) |
| 30 | + end |
| 31 | +
|
| 32 | + -- Worker group components |
| 33 | + if desiredObj.spec ~= nil and desiredObj.spec.workerGroupSpecs ~= nil then |
| 34 | + for i, workerGroup in ipairs(desiredObj.spec.workerGroupSpecs) do |
| 35 | + local name = workerGroup.groupName |
| 36 | + if isempty(name) then |
| 37 | + name = "worker-" .. tostring(i) |
| 38 | + end |
| 39 | + local replicas = workerGroup.replicas or 0 |
| 40 | + local requires = nil |
| 41 | + if workerGroup.template ~= nil then |
| 42 | + requires = kube.accuratePodRequirements(workerGroup.template) |
| 43 | + end |
| 44 | + local wgComponent = { |
| 45 | + name = name, |
| 46 | + replicas = replicas, |
| 47 | + replicaRequirements = requires |
| 48 | + } |
| 49 | + table.insert(components, wgComponent) |
| 50 | + end |
| 51 | + end |
| 52 | +
|
| 53 | + return components |
| 54 | + end |
| 55 | + healthInterpretation: |
| 56 | + luaScript: > |
| 57 | + function InterpretHealth(observedObj) |
| 58 | + if observedObj.status == nil or observedObj.status.conditions == nil then |
| 59 | + return false |
| 60 | + end |
| 61 | + |
| 62 | + local headPodReady = false |
| 63 | + local clusterProvisioned = false |
| 64 | + local replicaFailure = false |
| 65 | + |
| 66 | + for _, condition in ipairs(observedObj.status.conditions) do |
| 67 | + if condition.type == 'HeadPodReady' and condition.status == 'True' then |
| 68 | + headPodReady = true |
| 69 | + elseif condition.type == 'RayClusterProvisioned' and condition.status == 'True' then |
| 70 | + clusterProvisioned = true |
| 71 | + elseif condition.type == 'RayClusterReplicaFailure' and condition.status == 'True' then |
| 72 | + replicaFailure = true |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + return headPodReady and clusterProvisioned and not replicaFailure |
| 77 | + end |
| 78 | + statusAggregation: |
| 79 | + luaScript: > |
| 80 | + function AggregateStatus(desiredObj, statusItems) |
| 81 | + if statusItems == nil then |
| 82 | + return desiredObj |
| 83 | + end |
| 84 | + if desiredObj.status == nil then |
| 85 | + desiredObj.status = {} |
| 86 | + end |
| 87 | +
|
| 88 | + -- If only one item, use it directly |
| 89 | + if #statusItems == 1 then |
| 90 | + desiredObj.status = statusItems[1].status |
| 91 | + return desiredObj |
| 92 | + end |
| 93 | +
|
| 94 | + -- Initialize aggregated values |
| 95 | + local conditions = {} |
| 96 | + local readyWorkerReplicas = 0 |
| 97 | + local availableWorkerReplicas = 0 |
| 98 | + local maxWorkerReplicas = 0 |
| 99 | + local minWorkerReplicas = 0 |
| 100 | + local desiredWorkerReplicas = 0 |
| 101 | + local desiredCPU = 0 |
| 102 | + local desiredGPU = 0 |
| 103 | + local desiredMemory = 0 |
| 104 | + local desiredTPU = 0 |
| 105 | + local lastUpdateTime = nil |
| 106 | + local endpoints = nil |
| 107 | + local head = nil |
| 108 | + local state = nil |
| 109 | + local stateTransitionTimes = nil |
| 110 | +
|
| 111 | + -- Aggregate status from all member clusters |
| 112 | + for i = 1, #statusItems do |
| 113 | + local currentStatus = statusItems[i].status |
| 114 | + if currentStatus ~= nil then |
| 115 | + -- Merge conditions from all clusters |
| 116 | + if currentStatus.conditions ~= nil then |
| 117 | + for _, condition in ipairs(currentStatus.conditions) do |
| 118 | + table.insert(conditions, condition) |
| 119 | + end |
| 120 | + end |
| 121 | +
|
| 122 | + -- Sum numeric replica counts |
| 123 | + if currentStatus.readyWorkerReplicas ~= nil then |
| 124 | + readyWorkerReplicas = readyWorkerReplicas + currentStatus.readyWorkerReplicas |
| 125 | + end |
| 126 | + if currentStatus.availableWorkerReplicas ~= nil then |
| 127 | + availableWorkerReplicas = availableWorkerReplicas + currentStatus.availableWorkerReplicas |
| 128 | + end |
| 129 | + if currentStatus.maxWorkerReplicas ~= nil then |
| 130 | + maxWorkerReplicas = maxWorkerReplicas + currentStatus.maxWorkerReplicas |
| 131 | + end |
| 132 | + if currentStatus.minWorkerReplicas ~= nil then |
| 133 | + minWorkerReplicas = minWorkerReplicas + currentStatus.minWorkerReplicas |
| 134 | + end |
| 135 | + if currentStatus.desiredWorkerReplicas ~= nil then |
| 136 | + desiredWorkerReplicas = desiredWorkerReplicas + currentStatus.desiredWorkerReplicas |
| 137 | + end |
| 138 | +
|
| 139 | + -- Sum resource quantities (stored as numeric strings like "2", "4", etc.) |
| 140 | + if currentStatus.desiredCPU ~= nil and currentStatus.desiredCPU ~= "" then |
| 141 | + desiredCPU = desiredCPU + tonumber(currentStatus.desiredCPU) |
| 142 | + end |
| 143 | + if currentStatus.desiredGPU ~= nil and currentStatus.desiredGPU ~= "" then |
| 144 | + desiredGPU = desiredGPU + tonumber(currentStatus.desiredGPU) |
| 145 | + end |
| 146 | + if currentStatus.desiredTPU ~= nil and currentStatus.desiredTPU ~= "" then |
| 147 | + desiredTPU = desiredTPU + tonumber(currentStatus.desiredTPU) |
| 148 | + end |
| 149 | + -- For memory, parse numeric part (e.g., "3G" -> 3) |
| 150 | + if currentStatus.desiredMemory ~= nil and currentStatus.desiredMemory ~= "" then |
| 151 | + local memNum = tonumber(string.match(currentStatus.desiredMemory, "%d+")) |
| 152 | + if memNum ~= nil then |
| 153 | + desiredMemory = desiredMemory + memNum |
| 154 | + end |
| 155 | + end |
| 156 | +
|
| 157 | + -- Take the most recent lastUpdateTime |
| 158 | + if currentStatus.lastUpdateTime ~= nil then |
| 159 | + if lastUpdateTime == nil or currentStatus.lastUpdateTime > lastUpdateTime then |
| 160 | + lastUpdateTime = currentStatus.lastUpdateTime |
| 161 | + end |
| 162 | + end |
| 163 | +
|
| 164 | + -- Keep endpoints and head from first non-nil cluster (likely the one with head pod) |
| 165 | + if endpoints == nil and currentStatus.endpoints ~= nil then |
| 166 | + endpoints = currentStatus.endpoints |
| 167 | + end |
| 168 | + if head == nil and currentStatus.head ~= nil then |
| 169 | + head = currentStatus.head |
| 170 | + end |
| 171 | +
|
| 172 | + -- Keep state and stateTransitionTimes for backward compatibility (deprecated fields) |
| 173 | + if state == nil and currentStatus.state ~= nil then |
| 174 | + state = currentStatus.state |
| 175 | + stateTransitionTimes = currentStatus.stateTransitionTimes |
| 176 | + end |
| 177 | + end |
| 178 | + end |
| 179 | +
|
| 180 | + -- Set aggregated status |
| 181 | + desiredObj.status.conditions = conditions |
| 182 | + desiredObj.status.readyWorkerReplicas = readyWorkerReplicas |
| 183 | + desiredObj.status.availableWorkerReplicas = availableWorkerReplicas |
| 184 | + desiredObj.status.maxWorkerReplicas = maxWorkerReplicas |
| 185 | + desiredObj.status.minWorkerReplicas = minWorkerReplicas |
| 186 | + desiredObj.status.desiredWorkerReplicas = desiredWorkerReplicas |
| 187 | + desiredObj.status.desiredCPU = tostring(desiredCPU) |
| 188 | + desiredObj.status.desiredGPU = tostring(desiredGPU) |
| 189 | + desiredObj.status.desiredTPU = tostring(desiredTPU) |
| 190 | + -- Reconstruct memory with unit suffix |
| 191 | + if desiredMemory > 0 then |
| 192 | + desiredObj.status.desiredMemory = tostring(desiredMemory) .. "G" |
| 193 | + end |
| 194 | + desiredObj.status.lastUpdateTime = lastUpdateTime |
| 195 | + desiredObj.status.endpoints = endpoints |
| 196 | + desiredObj.status.head = head |
| 197 | + desiredObj.status.state = state |
| 198 | + desiredObj.status.stateTransitionTimes = stateTransitionTimes |
| 199 | +
|
| 200 | + return desiredObj |
| 201 | + end |
0 commit comments