英文:
helm chart create helper function using another helper function
问题
我正在尝试创建另一个使用现有辅助函数的辅助函数,但似乎不起作用。
我在_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 -}} # 这一行很重要。我们可以像这样使用上面的函数吗?
{{- 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对我来说都是新的,所以无法找出正确的语法,也不确定是否可行。(我们是这样在同一个辅助文件中编写两个函数的吗?)有人可以帮忙吗?
英文:
I am trying to make another helper function which uses an existing helper function but that seems to be not working.
I have following function in _helpers.tpl file:
{{- define "redis.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 30 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 30 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
Now I am trying to add another function to build connection string for redis using above function:
{{ - 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 -}}
And this is final content of my _helpers.tpl file:
{{- 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 and Helm both are new to me so not able to figure out correct synatx and even if its possible. (Is this how we write 2 functions in same helper file?) Can anyone please help here.
答案1
得分: 1
应该是可以的,这是我在_helpers中定义的内容(确保使用Values而不是Value):
{{- 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 }}
然后我在Service清单中使用它:
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/
...
英文:
Should be possible, here is what I defined in _helpers
(make sure you use Values, not Value)
{{- 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 }}
and then I use it in Service manifest:
apiVersion: v1
kind: Service
metadata:
name: {{ include "tpl-example.fullname" . }}
labels:
{{- include "tpl-example.labels" . | nindent 4 }}
test: {{ include "tpl-example.connection_string" . }}
then if I render the template with helm template -s templates/service.yaml MYAPP ./tpl-example --set port=1111
, it produces the following output:
...
test: redis://MYAPP-tpl-example:1111/
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论