<p>In my helm template I want to have the following block:</p>
<pre><code>ports:
  - containerPort: {{ .Values.ports.containerPort | default 8080}}
    name: {{ .Values.ports.name | default &quot;8080tcp02&quot; | quote}}
    protocol: {{ .Values.ports.protocol | default &quot;TCP&quot; | quote}}
          {{- end }}
</code></pre>
<p>If in <code>values.yaml</code> file block <code>ports</code> exist, then take values for <code>containerPort</code>, <code>name</code> and<code>protocol</code> from there. Otherwise, if block <code>ports</code> is absent in <code>values.yaml</code>, then take default values. Now I'm getting <code>nil pointer evaluating interface {}.containerPort</code>, if block <code>ports</code> is absent in <code>values.yaml</code>.</p>
<p>I also tried to do so:</p>
<pre><code>{{- with .Values.ports }}
ports:
  - containerPort: {{ .containerPort | default 8080}}
    name: {{ .name | default &quot;8080tcp02&quot;}}
    protocol: {{ .protocol | default &quot;TCP&quot;}}
{{- end }}
</code></pre>
<p>But then If block <code>ports</code> is absent in <code>values.yaml</code> It will absent in result as well. How can I achieve this?</p>
<p>Thanks in advance!</p>
<p>EDIT:</p>
<pre><code>  ports:
    {{- if .Values.ports }}
    {{- with . }}
    - containerPort: {{ .containerPort }}
      name: {{ .name |  quote }}
      protocol: {{ .protocol | quote }}
    {{- else }}
    - containerPort: 8080
      name: 8080tcp02
      protocol: TCP
    {{- end }}
    {{- end }}
</code></pre>
<p>Now it works with if statement, but not with <code>with</code> statement.</p>

Georgii Lvov
<p>Try if/else block:</p>
<pre><code>ports:
 {{- if .Values.ports }}
  - containerPort: {{ .Values.ports.containerPort | default 8080 }}
    name: {{ .Values.ports.name | default &quot;8080tcp02&quot; }}
    protocol: {{ .Values.ports.protocol | default &quot;TCP&quot; }}
 {{- else }}
  - containerPort: 8080
    name: &quot;8080tcp02&quot;
    protocol: &quot;TCP&quot;
 {{- end }}
</code></pre>
<p>Loop thru the ports:</p>
<pre><code> ports:
 {{- if .Values.ports }}
 {{- range $content := .Values.ports }}
  - containerPort: {{ $content.containerPort | default 8080 }}
    name: {{ $content.name | default &quot;8080tcp02&quot; }}
    protocol: {{ $content.protocol | default &quot;TCP&quot; }}
 {{- end }}
 {{- else }}
  - containerPort: 8080
    name: &quot;8080tcp02&quot;
    protocol: &quot;TCP&quot;
 {{- end }}
</code></pre>
