<p>I have a question regarding Kubernetes YAML string operations.</p>

<p>I need to set an env variable based on the hostname of the container that is deployed and append a port number to this variable.</p>

<pre><code> env:
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
</code></pre>

<blockquote>
  <p>How do I create another env variable that uses MY_POD_NAME and makes
  it look like this uri://$MY_POD_NAME:9099/</p>
</blockquote>

<p>This has to be defined as an env variable.  Are there string operations allowed in Kubernetes YAML files?</p>

<p>You can do something like </p>

<pre><code>- name: MY_POD_NAME
  valueFrom:
    fieldRef:
      fieldPath: metadata.name
- name: MY_POD_URI
  value: "uri://$(MY_POD_NAME):9099/"
</code></pre>

<p>We are using that since K8s 1.4</p>

<p><code>$()</code> is processed by k8s itself, does not work everywhere, but works for env variables. </p>

<p>If your container contains bash, you can also leverage bash variable expansion</p>

<pre><code>"command": ["/bin/bash"],
"args": [ "-c",
         "MY_POD_URI_BASH=uri://${MY_POD_NAME}:9099/ originalEntryPoint.sh
       ],
</code></pre>

<p><code>${}</code> is not touched by k8s, but evaluated later in container by bash. If you have a chance, prefer the first option with <code>$()</code></p>

<p><strong>note</strong>: order matters in declaration. In example above, if <code>MY_POD_NAME</code> is defined later in env array, the expansion will not work.</p>
