0

I've read the Helm documentation on flow control but I'm struggling to understand the best way to reference a top-level value within my values.yaml when I've restricted the scope in a template using a range or with loop.

For example, I'm creating Istio resources and creating a Helm chart for each namespace's policeis. My values.yaml is similar to below:

# values file defining Istio config for a specific namespace
namespacePolicy:
  globalNamespace: <my-namespace-name>
  peerAuth:
    enabled: True
    mode: STRICT

# I then define some policies like a ServiceEntry for example:
serviceEntries:
  policies:
    - name: policy-1
      hosts:
       - host1.com
       - host2.com
       - host3.com
      ports:
        - number: 443
          name: https
          protocol: HTTPS
    - name: policy-2
      hosts:
       - host1.com
       - host2.com
       - host3.com
      ports:
        - number: 80
          name: http
          protocol: HTTP

In order to create my policies, I create a template named ServiceEntry.yaml using a range loop to iterate and create each serviceEntry:

{{- $namespace := .Values.namespacePolicy.globalNamespace }}
{{- range .Values.serviceEntries.policies }}
apiVersion: istio.io/version
kind: serviceEntry
metadata:
  name: {{ .name }}
  namespace: {{ $namespace }}
spec:
  hosts:
    {{- include "hosts" . }}
  ports:
    {{- include "ports" . }}
---
{{- end }}
{{- end }}

Where I'm running into an error is with the {{- include "hosts" . }} statement. I want to be able to simply define YAML blocks in my values.yaml file and drop them into my template during a with/range loop but this seems to throw the error:

executing ../templates/ServiceEntry.yaml at <include "hosts" .>: error calling include : no template "hosts" associated with template "gotpl"

It seems to imply that I need to define a template but I'm just trying to reference the current list of hosts for the current object within the policies I'm iterating through within the defined scope of the range statement.

I'm not the greatest with Helm or Go so I was hoping for some insight on what I'm doing wrong here or if there's just a better practice for what I'm trying to do with Helm. My reasoning for trying to use the {{- include "" . }} logic is that there are multiple cases in my Istio policies where I'm trying to reduce complexity where YAML lists need to be defined in my values and created within my templates.

1 Answers1

0

I want to be able to simply define YAML blocks in my values.yaml file and drop them into my template

Helm includes a toYaml function that can do this.

{{- range .Values.serviceEntries.policies }}
spec:
  hosts:
{{ toYaml .hosts | indent 4 }}
{{- end }}

Note that the line including toYaml begins at the first column, but then the output of toYaml is indented to the correct level.

include calls a named template that is defined in code; its first argument is the name of the template, not a field in ..

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thanks, this ended up being the approach I went with. I don't know why I got so stuck on the idea of using the include statement originally. – user3372090 Jul 27 '23 at 14:42