<p>Wondering whether it's possible to use a port if it exists in the values, else use http.. something like this;</p>
<pre><code>svc:
  app:
    ports:
      - port: 8080
        name: http
      - port: 8090
        name: metrics
  app2:
    ports:
      - port: 8080
        name: http
</code></pre>
<p>Some service expose their metrics over http and some have metrics ports.. So I'd like to template it something like;</p>
<pre><code>{{ define &quot;app.service.ports&quot; }}
{{ range (index .Values.svc (include &quot;app.refName&quot; .) &quot;ports&quot;) }}
- name: {{ .name }}
{{ end }}
{{ end }}
</code></pre>
<p>This will pull each port name right, but I want to pull metrics if it exists, else pull http.. can someone point me in the right direction?</p>

jonnybinthemix
<p>Neither the Go <code>text/template</code> language nor the Helm extensions have any way to do a generic lookup in a complex structure like this.  Instead you need to manually iterate through the list and use a variable to remember what you've seen.</p>
<pre class="lang-none prettyprint-override"><code>{{- $ports := index .Values &quot;svc&quot; (include &quot;app.refName&quot; .) &quot;ports&quot; -}}

{{-/* Set up variables to remember what we've seen.  Initialize them
      to an empty dictionary, which is false in a conditional. -*/}}
{{- $httpPort := dict -}}
{{- $metricsPort := dict -}}

{{-/* Scan the list of ports. */-}}
{{- range $port := range $ports -}}
  {{- if eq $port.name &quot;metrics&quot; -}}
    {{- $metricsPort = $port -}}
  {{- else if eq $port.name &quot;http&quot; -}}
    {{- $httpPort = $port -}}
  {{- end -}}
{{- end -}}

{{-/* Emit the metrics port if it exists, else the HTTP port. */-}}
{{- coalesce $metricsPort $httpPort | toYaml -}}
</code></pre>
<p>In a higher-level language you could imagine searching a list, and for each dictionary element, accepting it if it has a <code>name</code> key with value <code>metrics</code>.  This usually involves passing a lambda or anonymous function to a <code>find()</code> function, and the <code>text/template</code> language has no support for that.</p>
