0

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.

undefined
  • 3,464
  • 11
  • 48
  • 90

1 Answers1

1

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/
...
jabbson
  • 4,390
  • 1
  • 13
  • 23