|
| 1 | +apiVersion: config.karmada.io/v1alpha1 |
| 2 | +kind: ResourceInterpreterCustomization |
| 3 | +metadata: |
| 4 | + name: declarative-configuration-sidecarset |
| 5 | +spec: |
| 6 | + target: |
| 7 | + apiVersion: apps.kruise.io/v1alpha1 |
| 8 | + kind: SidecarSet |
| 9 | + customizations: |
| 10 | + statusReflection: |
| 11 | + luaScript: | |
| 12 | + function ReflectStatus(observedObj) |
| 13 | + if observedObj.status == nil then |
| 14 | + return {} |
| 15 | + end |
| 16 | + return { |
| 17 | + matchedPods = observedObj.status.matchedPods or 0, |
| 18 | + updatedPods = observedObj.status.updatedPods or 0, |
| 19 | + readyPods = observedObj.status.readyPods or 0 |
| 20 | + } |
| 21 | + end |
| 22 | + replicaResource: |
| 23 | + luaScript: | |
| 24 | + function GetReplicas(obj) |
| 25 | + -- SidecarSet doesn't manage replicas directly, return 0 |
| 26 | + return 0 |
| 27 | + end |
| 28 | + statusAggregation: |
| 29 | + luaScript: | |
| 30 | + function AggregateStatus(desiredObj, statusItems) |
| 31 | + local matchedPods = 0 |
| 32 | + local updatedPods = 0 |
| 33 | + local readyPods = 0 |
| 34 | + |
| 35 | + for i = 1, #statusItems do |
| 36 | + local status = statusItems[i].status or {} |
| 37 | + matchedPods = matchedPods + (status.matchedPods or 0) |
| 38 | + updatedPods = updatedPods + (status.updatedPods or 0) |
| 39 | + readyPods = readyPods + (status.readyPods or 0) |
| 40 | + end |
| 41 | + |
| 42 | + return { |
| 43 | + apiVersion = "apps.kruise.io/v1alpha1", |
| 44 | + kind = "SidecarSet", |
| 45 | + metadata = desiredObj.metadata, |
| 46 | + status = { |
| 47 | + matchedPods = matchedPods, |
| 48 | + updatedPods = updatedPods, |
| 49 | + readyPods = readyPods |
| 50 | + } |
| 51 | + } |
| 52 | + end |
| 53 | + retention: |
| 54 | + luaScript: | |
| 55 | + function Retain(desiredObj, observedObj) |
| 56 | + -- No specific retention logic needed as Karmada handles status preservation |
| 57 | + return desiredObj |
| 58 | + end |
| 59 | + healthInterpretation: |
| 60 | + luaScript: | |
| 61 | + function InterpretHealth(observedObj) |
| 62 | + if observedObj.status == nil then |
| 63 | + return false |
| 64 | + end |
| 65 | + local matchedPods = observedObj.status.matchedPods or 0 |
| 66 | + local updatedPods = observedObj.status.updatedPods or 0 |
| 67 | + -- If no pods are matched, consider it healthy (nothing to update) |
| 68 | + if matchedPods == 0 then |
| 69 | + return true |
| 70 | + end |
| 71 | + -- A SidecarSet is healthy if all matched pods have been updated |
| 72 | + return updatedPods == matchedPods |
| 73 | + end |
| 74 | + dependencyInterpretation: |
| 75 | + luaScript: | |
| 76 | + function GetDependencies(desiredObj) |
| 77 | + local dependencies = {} |
| 78 | + if not desiredObj.spec then |
| 79 | + return dependencies |
| 80 | + end |
| 81 | +
|
| 82 | + -- Helper function to add a dependency |
| 83 | + local function addDependency(kind, name, namespace) |
| 84 | + table.insert(dependencies, { |
| 85 | + apiVersion = "v1", |
| 86 | + kind = kind, |
| 87 | + name = name, |
| 88 | + namespace = namespace or (desiredObj.metadata and desiredObj.metadata.namespace) |
| 89 | + }) |
| 90 | + end |
| 91 | +
|
| 92 | + -- Check for references in containers |
| 93 | + if desiredObj.spec.containers then |
| 94 | + for i = 1, #desiredObj.spec.containers do |
| 95 | + local container = desiredObj.spec.containers[i] |
| 96 | + |
| 97 | + -- Check environment variables |
| 98 | + if container.env then |
| 99 | + for j = 1, #container.env do |
| 100 | + local env = container.env[j] |
| 101 | + if env.valueFrom then |
| 102 | + if env.valueFrom.configMapKeyRef then |
| 103 | + addDependency("ConfigMap", env.valueFrom.configMapKeyRef.name) |
| 104 | + end |
| 105 | + if env.valueFrom.secretKeyRef then |
| 106 | + addDependency("Secret", env.valueFrom.secretKeyRef.name) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + -- Check envFrom |
| 113 | + if container.envFrom then |
| 114 | + for j = 1, #container.envFrom do |
| 115 | + local envFrom = container.envFrom[j] |
| 116 | + if envFrom.configMapRef then |
| 117 | + addDependency("ConfigMap", envFrom.configMapRef.name) |
| 118 | + end |
| 119 | + if envFrom.secretRef then |
| 120 | + addDependency("Secret", envFrom.secretRef.name) |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +
|
| 127 | + -- Check for volume references |
| 128 | + if desiredObj.spec.volumes then |
| 129 | + for i = 1, #desiredObj.spec.volumes do |
| 130 | + local volume = desiredObj.spec.volumes[i] |
| 131 | + |
| 132 | + -- Standard volume types |
| 133 | + if volume.configMap then |
| 134 | + addDependency("ConfigMap", volume.configMap.name) |
| 135 | + end |
| 136 | + if volume.secret then |
| 137 | + addDependency("Secret", volume.secret.secretName) |
| 138 | + end |
| 139 | + |
| 140 | + -- Projected volumes |
| 141 | + if volume.projected and volume.projected.sources then |
| 142 | + for j = 1, #volume.projected.sources do |
| 143 | + local source = volume.projected.sources[j] |
| 144 | + if source.configMap then |
| 145 | + addDependency("ConfigMap", source.configMap.name) |
| 146 | + end |
| 147 | + if source.secret then |
| 148 | + addDependency("Secret", source.secret.name) |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + return dependencies |
| 156 | + end |
0 commit comments