|
| 1 | +apiVersion: config.karmada.io/v1alpha1 |
| 2 | +kind: ResourceInterpreterCustomization |
| 3 | +metadata: |
| 4 | + name: declarative-configuration-notebook |
| 5 | +spec: |
| 6 | + target: |
| 7 | + apiVersion: kubeflow.org/v1 |
| 8 | + kind: Notebook |
| 9 | + customizations: |
| 10 | + replicaResource: |
| 11 | + luaScript: > |
| 12 | + local kube = require("kube") |
| 13 | + function GetReplicas(obj) |
| 14 | + local replica = 1 |
| 15 | + local requirement = kube.accuratePodRequirements(obj.spec.template) |
| 16 | + return replica, requirement |
| 17 | + end |
| 18 | + healthInterpretation: |
| 19 | + luaScript: > |
| 20 | + function InterpretHealth(observedObj) |
| 21 | + if observedObj.status == nil or observedObj.status.containerState == nil then |
| 22 | + return false |
| 23 | + end |
| 24 | +
|
| 25 | + local state = observedObj.status.containerState |
| 26 | + if state.running ~= nil then |
| 27 | + return true |
| 28 | + end |
| 29 | +
|
| 30 | + if state.waiting ~= nil then |
| 31 | + local reason = state.waiting.reason or "" |
| 32 | + if reason == "ContainerCreating" then |
| 33 | + return true |
| 34 | + end |
| 35 | + return false |
| 36 | + end |
| 37 | +
|
| 38 | + if state.terminated ~= nil then |
| 39 | + local reason = state.terminated.reason or "" |
| 40 | + local exitCode = state.terminated.exitCode or 0 |
| 41 | +
|
| 42 | + if reason == "Error" or reason == "OOMKilled" or reason == "ContainerCannotRun" or exitCode ~= 0 then |
| 43 | + return false |
| 44 | + end |
| 45 | +
|
| 46 | + return true |
| 47 | + end |
| 48 | +
|
| 49 | + return false |
| 50 | + end |
| 51 | + statusAggregation: |
| 52 | + luaScript: > |
| 53 | + function AggregateStatus(desiredObj, statusItems) |
| 54 | + if statusItems == nil then |
| 55 | + return desiredObj |
| 56 | + end |
| 57 | + if desiredObj.status == nil then desiredObj.status = {} end |
| 58 | + |
| 59 | + if #statusItems == 1 then |
| 60 | + desiredObj.status = statusItems[1].status |
| 61 | + return desiredObj |
| 62 | + end |
| 63 | +
|
| 64 | + local status = { |
| 65 | + readyReplicas = 0, |
| 66 | + containerState = {}, |
| 67 | + conditions = {} |
| 68 | + } |
| 69 | + local aggregatedConditions = {} |
| 70 | + local successfulClustersNum = 0 |
| 71 | + local failedClusters = {} |
| 72 | + local aggregatedContainerState = nil |
| 73 | + local latestStartTime = nil |
| 74 | +
|
| 75 | + for i = 1, #statusItems do |
| 76 | + local s = statusItems[i] |
| 77 | + if s ~= nil and s.status ~= nil then |
| 78 | + local st = s.status |
| 79 | + status.readyReplicas = status.readyReplicas + (st.readyReplicas or 0) |
| 80 | + local cs = st.containerState |
| 81 | +
|
| 82 | + local isFailed = cs == nil or (type(cs) == "table" and next(cs) == nil) or cs.terminated ~= nil or cs.waiting ~= nil |
| 83 | + if isFailed then |
| 84 | + table.insert(failedClusters, s.clusterName) |
| 85 | + if cs == nil or next(cs) == nil then |
| 86 | + aggregatedContainerState ={ waiting = { reason = "Unschedulable" } } |
| 87 | + else |
| 88 | + aggregatedContainerState = cs |
| 89 | + end |
| 90 | + else |
| 91 | + if cs.running ~= nil and #failedClusters == 0 then |
| 92 | + successfulClustersNum = successfulClustersNum + 1 |
| 93 | + local startedAt = cs.running.startedAt |
| 94 | + if aggregatedContainerState == nil then |
| 95 | + aggregatedContainerState = { running = { startedAt = startedAt } } |
| 96 | + latestStartTime = startedAt |
| 97 | + else |
| 98 | + if startedAt ~= nil and (latestStartTime == nil or startedAt > latestStartTime) then |
| 99 | + latestStartTime = startedAt |
| 100 | + aggregatedContainerState.running = { startedAt = startedAt } |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +
|
| 108 | + if #failedClusters > 0 then |
| 109 | + table.insert(aggregatedConditions, { |
| 110 | + type = "Failed", |
| 111 | + status = "True", |
| 112 | + lastProbeTime = os.date("!%Y-%m-%dT%H:%M:%SZ"), |
| 113 | + lastTransitionTime = os.date("!%Y-%m-%dT%H:%M:%SZ"), |
| 114 | + reason = "NotebookFailed", |
| 115 | + message = "Notebook executed failed in member clusters: " .. table.concat(failedClusters, ", ") |
| 116 | + }) |
| 117 | + end |
| 118 | +
|
| 119 | + if successfulClustersNum == #statusItems and successfulClustersNum > 0 then |
| 120 | + table.insert(aggregatedConditions, { |
| 121 | + type = "Ready", |
| 122 | + status = "True", |
| 123 | + lastProbeTime = os.date("!%Y-%m-%dT%H:%M:%SZ"), |
| 124 | + lastTransitionTime = os.date("!%Y-%m-%dT%H:%M:%SZ"), |
| 125 | + reason = "Ready", |
| 126 | + message = "All notebooks are ready" |
| 127 | + }) |
| 128 | + end |
| 129 | +
|
| 130 | + status.conditions = aggregatedConditions |
| 131 | + status.containerState = aggregatedContainerState |
| 132 | + desiredObj.status = status |
| 133 | + return desiredObj |
| 134 | + end |
| 135 | + statusReflection: |
| 136 | + luaScript: > |
| 137 | + function ReflectStatus(observedObj) |
| 138 | + if observedObj.status == nil then |
| 139 | + return {} |
| 140 | + end |
| 141 | + local status = { |
| 142 | + readyReplicas = 0, |
| 143 | + containerState = {}, |
| 144 | + conditions = {} |
| 145 | + } |
| 146 | +
|
| 147 | + local s = observedObj.status |
| 148 | + status.readyReplicas = s.readyReplicas or 0 |
| 149 | + status.containerState = s.containerState or {} |
| 150 | + if type(s.conditions) == "table" then |
| 151 | + status.conditions = s.conditions |
| 152 | + end |
| 153 | +
|
| 154 | + return status |
| 155 | + end |
0 commit comments