英文:
Helm template need to get and set value in dict
问题
我在我的函数$newdict中有以下数据:
   modules:
     module1: 
      enabled: true
     module2:
      enabled: false
     module3:
      enabled: true, 等等
我需要做的是:检查module1是否启用,然后将module2的启用状态也设置为true。我在_helpers.tpl文件中尝试的方法是:
  {{- range $key, $value := $newdict -}}
  {{ if and (eq $key "module1") (eq "enabled" "true") }}
  {{ $_ := set $newdict $key "module2" (eq "enabled" "true") }} 
  {{ end }}  
  {{ end }}
  {{ toYaml $newdict }}
helm lint没有显示任何错误,但$newdict中的更改没有反映出来。
这个功能是为了在deployment.yaml中部署init容器:
     initContainers:
  {{- $mycustom := (include "myfunction" . ) | fromYaml}}
  {{- range $key, $value := $mycustom }}
    {{- if $value.enabled }}
    - name: init-{{ $key }}-myinit
因此,最终,只有在部署了"module1"时,我才需要部署"module2"的init容器。
英文:
I have the following data in my function $newdict
   modules:
     module1: 
      enabled: true
     module2:
      enabled: false
     module3:
      enabled: true, etc
What is need to do is : check if module1 enabled, then set enabled for module2 as well. What I tried in _helpers.tpl file:
  {{- range $key, $value := $newdict -}}
  {{ if and (eq $key "module1") (eq "enabled" "true") }}
  {{ $_ := set $newdict $key "module2" (eq "enabled" "true") }} 
  {{ end }}  
  {{ end }}
  {{ toYaml $newdict }}
helm lint doesn't show any errors, but the changes are not reflected in $newdict
This thing is needed for deployment.yaml to deploy init container:
     initContainers:
  {{- $mycustom := (include "myfunction" . ) | fromYaml}}
  {{- range $key, $value := $mycustom }}
    {{- if $value.enabled }}
    - name: init-{{ $key }}-myinit
So, in the end, I need to deploy init container "module2" only if "module1" is also deployed
答案1
得分: 1
Helm的一般风格是值是不可变的。我建议在这里避免使用set函数,而是坚持使用更函数式的风格。
你可以直接在字典(映射)结构中索引元素,或者使用标准的Go模板index函数,这可能有助于你的设置。你不需要遍历整个字典来查找一个键。所以,如果你确定变量具有module1和module2这两个键,那么你可以简化最终的逻辑为:
initContainers:
{{- if or .Values.module1.enabled .Values.module2.enabled }}
  - name: init-module2-myinit
  ...
{{- end }}
你的设置暗示了有更多的初始化容器;module1、module2和module3各自都有自己的初始化容器,但是你还希望在打开module1时无论module2的设置如何都会输出module2的初始化容器。解决这个问题的一种方法是编写一个帮助函数来判断给定的模块是否启用:
{{/* 判断某个模块是否启用。使用一个包含两个元素的列表调用,第一个元素是要检查的值结构,第二个元素是特定的模块键。返回字符串"true"或空字符串。 */}}
{{- define "is-module-enabled" -}}
{{- $modules := index . 0 -}}
{{- $key := index . 1 -}}
{{- if index $modules $key "enabled" -}}
true
{{- else if and (eq $key "module2") $modules.module1.enabled -}}
true
{{- end -}}
{{- end -}}
initContainers:
{{- range $key := .Values.modules -}}
{{- if include "is-module-enabled" (list .Values.modules $key) }}
  - name: init-{{ $key }}-myinit
  ...
{{- end }}
{{- end }}
英文:
Helm's general style is that values are immutable.  I'd avoid the set function here and stick to a more functional style.
It may help your setup that you can directly index things in dict (map) structures, or use the standard Go template index function.  You don't need to iterate through the entire dict looking for a key.  So if you're sure the variable has keys module1 and module2 then you can simplify the final logic to:
initContainers:
{{- if or .Values.module1.enabled .Values.module2.enabled }}
  - name: init-module2-myinit
  ...
{{- end }}
Your setup hints at having more init containers; module1, module2, and module3 would each have their own init containers, but you also want to emit module2's whenever module1 is turned on regardless of the module2 setting.  One way to approach this might be to write a helper function that decided whether a given module was enabled:
{{/* Decide whether some module is enabled.  Call with a list of
     two items, the values structure to examine and the specific
     module key.  Returns either the string "true" or an empty
     string. */}}
{{- define "is-module-enabled" -}}
{{- $modules := index . 0 -}}
{{- $key := index . 1 -}}
{{- if index $modules $key "enabled" -}}
true
{{- else if and (eq $key "module2") $modules.module1.enabled -}}
true
{{- end -}}
{{- end -}}
initContainers:
{{- range $key := .Values.modules -}}
{{- if include "is-module-enabled" (list .Values.modules $key) }}
  - name: init-{{ $key }}-myinit
  ...
{{- end }}
{{- end }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论