gpt4 book ai didi

kubernetes - 在 Helm 图表中编写自定义函数

转载 作者:行者123 更新时间:2023-12-04 21:31:10 27 4
gpt4 key购买 nike

我的 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 文件中放置逻辑繁重的 if 检查会很有用(只是为了减少出错的可能性)。

另外,我不想在每个模板文件中都定义这个函数,因为这会破坏目的。

最佳答案

您可以创建一个 partial template在以下划线开头的文件中命名条件挂载,例如,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)}}

这里的诀窍是您指定要通过范围对象 挂载的 pvc。此PVC 指向 .Values.pvc . Sprig dict function用来。
然后可以针对不同的PVC调用它,例如, .Values.pvcXYZ :
{{include "conditional-mount" (dict "Values" .Values "thisPvc" .Values.pvcXYZ)}}

关于kubernetes - 在 Helm 图表中编写自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51240307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com