MCPcopy
hub / github.com/apecloud/kubeblocks / evaluateObjectVarsExpression

Function evaluateObjectVarsExpression

pkg/controller/component/vars.go:239–306  ·  view source on GitHub ↗
(definedVars []appsv1.EnvVar, credentialVars []corev1.EnvVar, vars *[]corev1.EnvVar)

Source from the content-addressed store, hash-verified

237}
238
239func evaluateObjectVarsExpression(definedVars []appsv1.EnvVar, credentialVars []corev1.EnvVar, vars *[]corev1.EnvVar) error {
240 var (
241 isValues = make(map[string]bool)
242 values = make(map[string]any)
243 )
244 normalize := func(name string) string {
245 return strings.ReplaceAll(name, "-", "_")
246 }
247 for _, v := range [][]corev1.EnvVar{*vars, credentialVars} {
248 for _, vv := range v {
249 if vv.ValueFrom == nil {
250 isValues[vv.Name] = true
251 values[normalize(vv.Name)] = vv.Value
252 } else {
253 isValues[vv.Name] = false
254 }
255 }
256 }
257
258 evaluable := func(v appsv1.EnvVar) bool {
259 if v.Expression == nil || len(*v.Expression) == 0 {
260 return false
261 }
262 isValue, ok := isValues[v.Name]
263 // !ok is for vars that defined and resolved successfully, but have nil value.
264 return !ok || isValue
265 }
266
267 update := func(name, value string) {
268 if val, exist := values[normalize(name)]; exist {
269 if val != value {
270 for i := range *vars {
271 if (*vars)[i].Name == name {
272 (*vars)[i].Value = value
273 break
274 }
275 }
276 }
277 } else {
278 // TODO: insert the var to keep orders?
279 *vars = append(*vars, corev1.EnvVar{Name: name, Value: value})
280 }
281 values[normalize(name)] = value
282 }
283
284 eval := func(v appsv1.EnvVar) error {
285 if !evaluable(v) {
286 return nil
287 }
288 tpl, err := varTemplate.Parse(*v.Expression)
289 if err != nil {
290 return err
291 }
292 var buf strings.Builder
293 if err = tpl.Execute(&buf, values); err != nil {
294 return err
295 }
296 update(v.Name, buf.String())

Callers 1

resolveObjectRefVarsFunction · 0.85

Calls 4

normalizeFunction · 0.85
updateFunction · 0.85
ExecuteMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…