0

我正在尝试制作另一个使用现有辅助功能的辅助功能,但这似乎不起作用。

我在 _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 个函数的方式吗?)任何人都可以在这里提供帮助。

4

1 回答 1

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/
...
于 2021-12-02T05:29:27.043 回答