7

I am trying to loop for a count in a kubernetes helm chart like this:

reaction.mongo_url_big: mongodb://{{ for $mongocount := 0; $mongocount < {{ .Values.mongodbReplicantCount }}; $mongocount++ }}{{ .Values.mongodbReleaseName }}-mongodb-replicaset-{{ $mongocount }}:{{ .Values.mongodbPort }}{{ if $mongocount < {{ .Values.mongodbReplicantCount }} - 1 }},{{ end }}{{ end }}/{{ .Values.mongodbName }}?replicaSet={{ .Values.mongodbReplicaSet }}

However, go templates seem to be lacking a means of rendering a 'for' loop, by design

I want it to output something like:

 reaction.mongo_url: mongodb://{{ .Values.mongodbReleaseName }}-mongodb-replicaset-0:{{ .Values.mongodbPort }},{{ .Values.mongodbReleaseName }}-mongodb-replicaset-1:{{ .Values.mongodbPort }},{{ .Values.mongodbReleaseName }}-mongodb-replicaset-2:{{ .Values.mongodbPort }}/{{ .Values.mongodbName }}?replicaSet={{ .Values.mongodbReplicaSet }}

The line in my helm chart is here: https://github.com/joshuacox/reactionetes/blob/gymongonasium/reactioncommerce/templates/configmap.yaml#L11

Ed Randall
  • 6,887
  • 2
  • 50
  • 45
thoth
  • 1,112
  • 2
  • 9
  • 15
  • Your question is about why the developers of a package didn't implement what you want, you'll be better served if you ask the people who made those decisions. If I were you I would try the go mailing list [#go-nuts](https://groups.google.com/forum/#!forum/golang-nuts). – mkopriva Dec 11 '17 at 23:12
  • Related / possible duplicate of [Golang code to repeat an html code n times](https://stackoverflow.com/questions/46112679/golang-code-to-repeat-an-html-code-n-times/46112802#46112802). – icza Dec 11 '17 at 23:32
  • @icza no clue how to apply your stuff inside a helm template, so I don't think it's duplicate. However, I think I've found my solution here --> https://github.com/kubernetes/charts/blob/master/stable/aerospike/templates/_helpers.tpl#L23-L25 – thoth Dec 12 '17 at 03:19
  • 1
    This is a valid question, not really opinion-based at all, and it doesn't take much imagination to edit the single sentence that was deemed unacceptable by the mods into a factual statement... done and voted to re-open. – Ed Randall Jun 04 '20 at 13:07

2 Answers2

11

Use range:

{{ range .Values }}
   {{ .MongodbReleaseName }}
{{ end }}

This will output the .MongodbReleaseName (assuming that's a field) of every item in .Values. The value is assigned to . while within the range so you can simply refer to fields/functions of the individual Values. This is very like a for loop in other templating languages. You can also use it by assigning an index and value.

Kenny Grant
  • 9,360
  • 2
  • 33
  • 47
8

Notice on the helm tips and tricks page they mention that sprig functions have been added, one of which is until, which can be seen in action here or in my case:

{{- define "mongodb_replicaset_url" -}}
  {{- printf "mongodb://" -}}
  {{- range $mongocount, $e := until (.Values.mongodbReplicaCount|int) -}}
    {{- printf "%s-mongodb-replicaset-%d." $.Values.mongodbReleaseName $mongocount -}}
    {{- printf "%s-mongodb-replicaset:%d" $.Values.mongodbReleaseName ($.Values.mongodbPort|int) -}}
    {{- if lt $mongocount  ( sub ($.Values.mongodbReplicaCount|int) 1 ) -}}
      {{- printf "," -}}
    {{- end -}}
  {{- end -}}
  {{- printf "/%s?replicaSet=%s" $.Values.mongodbName  $.Values.mongodbReplicaSet -}}
{{- end -}}
thoth
  • 1,112
  • 2
  • 9
  • 15