Search code examples
kuberneteskubernetes-helmgo-templates

Helm template not able to read ip address - can't evaluate field ipAddress in type string


I am trying to create a helm template for Istio's ServiceEntry which has a list of addresses for static ip addresses. In values.yaml, I have

- name: test-se
  namespace: test-se-ns
  egressUrls: 
  - mydbhost.com
  port: 32306
  protocol: TCP
  ipAddress: 10.2.2.2

In the .tpl file I am trying to add the value of ipAddress to a list

  {{- with .ipAddress }}
  addresses: 
  - {{ .ipAddress | quote }}
  {{- end }}

Always fails with exception

templates/_service_entry.tpl:18:13: executing "common.serviceentry.tpl" at <.ipAddress>: can't evaluate field ipAddress in type string

Any idea what I am doing wrong?


Solution

  • If you use with you make the thing that you have used as with the context inside that block.

    So, use the dot to refer to it.

    {{- with .ipAddress }}
    addresses: 
      - {{ . | quote }}
    {{- end }}
    

    From the docs:

    {{with pipeline}} T1 {{end}}
        If the value of the pipeline is empty, no output is generated;
        otherwise, dot is set to the value of the pipeline and T1 is
        executed.
    

    In this case, an if seems also fitting, since you do not much with the new context.

    {{- if .ipAddress }}
    addresses: 
      - {{ .ipAddress | quote }}
    {{- end }}