我的Helm部署yaml文件中有以下代码段:
{{if or .Values.ha.enabled .Values.checkpointing.enable_checkpointing .Values.enable_upgrade_hook}}
{{if eq .Values.pvc.file_prefix "file://"}}
- mountPath: {{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}
name: shared-pvc
{{end}}
{{end}}我想将所有这些if签入自定义函数中,只需在这里调用该函数。使用该函数的新代码片段应该如下所示:
{{if eq enable_mount_volume "true"}}
- mountPath: {{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}
name: shared-pvc
{{end}}我怎样才能做到这一点?我可能有多个部署的yaml文件,每个文件都执行这个条件检查,只需要调用一个函数就可以了,而不是在每个yaml文件中签入逻辑很重的如果签入(只是为了减少错误的发生)。
另外,我也不想在每个模板文件中定义这个函数,因为这样做会使目的落空。
发布于 2018-10-04 13:55:16
可以在以下划线开头的文件中创建名为条件挂载的部分模板,例如,templates/_conditional-mount.tpl。
{{define "conditional-mount"}}
{{if or .Values.ha.enabled .Values.checkpointing.enable_checkpointing .Values.enable_upgrade_hook}}
{{if eq .thisPvc.file_prefix "file://"}}
- mountPath: {{ .thisPvc.shared_storage_path }}/{{ template "fullname" . }}
name: shared-pvc
{{end}}
{{end}}
{{end}}然后在你需要的任何地方使用它:
{{include "conditional-mount" (dict "Values" .Values "thisPvc" .Values.pvc)}}这里的诀窍是通过指向.Values.pvc的作用域对象thisPvc指定要挂载的pvc。使用小枝函数。然后,您可以将其命名为不同的PVC,例如,.Values.pvcXYZ。
{{include "conditional-mount" (dict "Values" .Values "thisPvc" .Values.pvcXYZ)}}发布于 2018-07-10 06:04:06
你可能会发现{{block "foo"}}会做你想做的事情,这取决于大量的“如果什么”。
在这个问题上,舵机文档有一个更多的单词,这是很好的,因为他们显然已经考虑过了,而悲伤的是因为有那么多的词。
https://stackoverflow.com/questions/51240307
复制相似问题