【问题标题】:helm chart create helper function using another helper functionhelm chart 使用另一个辅助函数创建辅助函数
【发布时间】:2022-01-08 15:11:40
【问题描述】:

我正在尝试制作另一个使用现有辅助函数但似乎不起作用的辅助函数。

我在 _helpers.tpl 文件中有以下功能:

{{- define "redis.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 30 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 30 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}

现在我正在尝试添加另一个函数来使用上述函数为 redis 构建连接字符串:

{{ - define "redis.connection_string" -}}
{{ - printf "redis://%s:%s/" include "redis.fullname" .Value.port -}} # This line is important. Can we use above function like this?
{{ -end -}}

这是我的 _helpers.tpl 文件的最终内容:

{{- define "redis.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 30 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 30 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}


{{ - define "redis.connection_string" -}}
{{ - printf "redis://%s:%s/" include "redis.fullname" .Value.port -}}
{{ - end -}}

GO 和 Helm 对​​我来说都是新手,所以即使它可能也无法找出正确的语法。 (这就是我们在同一个帮助文件中编写 2 个函数的方式吗?)任何人都可以在这里提供帮助。

【问题讨论】:

    标签: go kubernetes-helm helper helm3


    【解决方案1】:

    应该可以,这是我在_helpers中定义的 (确保您使用的是值,而不是值)

    {{- define "tpl-example.fullname" -}}
    {{- if .Values.fullnameOverride -}}
    {{- .Values.fullnameOverride | trunc 30 | trimSuffix "-" -}}
    {{- else -}}
    {{- printf "%s-%s" .Release.Name .Chart.Name | trunc 30 | trimSuffix "-" -}}
    {{- end -}}
    {{- end -}}
    
    {{- define "tpl-example.connection_string" }}
    {{- printf "redis://%s:%d/" (include "tpl-example.fullname" .) .Values.port }}
    {{- end }}
    

    然后我在服务清单中使用它:

    apiVersion: v1
    kind: Service
    metadata:
      name: {{ include "tpl-example.fullname" . }}
      labels:
        {{- include "tpl-example.labels" . | nindent 4 }}
        test: {{ include "tpl-example.connection_string" . }}
    

    如果我使用helm template -s templates/service.yaml MYAPP ./tpl-example --set port=1111 渲染模板,它会产生以下输出:

    ...
        test: redis://MYAPP-tpl-example:1111/
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 2014-01-23
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多