<p>I have a <code>values.yaml</code> where I need to mention multiple ports like the following:</p>

<pre><code>kafkaClientPort:
  - 32000
  - 32001
  - 32002
</code></pre>

<p>In yaml for statefulset, I need to get value using ordinal number.
So for <code>kf-0</code>, I need to put first element of <code>kafkaClientPort</code>; and for <code>kf-1</code>, second element and so on.
I am trying like the following:</p>

<pre><code>args:
- "KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://$(MY_NODE_NAME):{{ index .Values.kafkaClientPort ${HOSTNAME##*-} }}"
</code></pre>

<p>But it is showing an error.</p>

<p>Please advise what is the best way to access dynamically <code>values.yaml</code> value.</p>

<p>The trick here is that Helm template doesn't know anything about ordinal in your stateful set.  If you look at the Kafka <a href="https://github.com/helm/charts/blob/e0874c51fa95c1f9fafc1f8fdd714891642caa51/incubator/kafka/values.yaml#L176" rel="nofollow noreferrer">Helm Chart</a>, you see that they are using a base port <code>31090</code> and then they add the ordinal number but that substitution is in place 'after' the template is created. Something like this in your values: </p>

<pre><code>"advertised.listener": |-
   PLAINTEXT://kafka.cluster.local:$((31090 + ${KAFKA_BROKER_ID}))
</code></pre>

<p>and then in the template file, the <a href="https://github.com/helm/charts/blob/3cacbb20dfc067bb601393aca87c17df80cf4ee6/incubator/kafka/templates/statefulset.yaml#L203" rel="nofollow noreferrer">use a bash export under <code>command</code></a> with a <code>printf</code> which is an alias for <code>fmt.Sprintf</code>. Something like this in your case:</p>

<pre><code>    command:
    - sh
    - -exc
    - |
      unset KAFKA_PORT &amp;&amp; \
      export KAFKA_BROKER_ID=${HOSTNAME##*-} &amp;&amp; \
      export "KAFKA_ADVERTISED_LISTENERS={{ printf "%s" $advertised.listener }} \\
      ...
</code></pre>
