Expand the env value defined in kubernetes definition to container

3/27/2018

I am trying to set some environment variables in docker container, Below is the env definition part from the kubernetes pod definition.

              env:
                - name: NRIA_LICENSE_KEY
                  value: NRIA_LICENSE_KEY
                -name:  NRIA_DISPLAY_NAME
                  value: abc-$HOSTNAME

When I echo the value of NRIA_DISPLAY_NAME variable in the container, however, I am getting below output, which is not expected.

echo $NRIA_DISPLAY_NAME
NRIA_DISPLAY_NAME=abc-\$HOSTNAME

expected output:

    echo $NRIA_DISPLAY_NAME
    NRIA_DISPLAY_NAME=abc-myhostname ($HOSTNAME should be replaced with the actual hostname value)

I tried setting it in command tag as well, that didn't work.

 command: [ "bash", "-c", "export NRIA_DISPLAY_NAME=abc-$HOSTNAME" ]
-- MangeshBiradar
containers
docker
environment-variables
kubernetes
newrelic

1 Answer

3/28/2018
- name:  NRIA_DISPLAY_NAME
  value: abc-$HOSTNAME

should have two changes made to it:

  1. Ensure the thing you wish to have substituted is also present in the env: block -- I know you might think $HOSTNAME is universally populated, but that's not always true, and it is for sure not true in the kubernetes mental model

  2. Use the syntax $(INTERPOLATION_HERE) rather than the bash-centric $VAR or ${VAR}. That syntax is documented in the PodSpec reference

Thus:

env:
- name: HOSTNAME
  valueFrom:
    fieldRef:
      fieldPath: metadata.name
      # or "status.hostIP" or "spec.nodeName" etc
- name: NRIA_DISPLAY_NAME
  value: abc-$(HOSTNAME)
-- mdaniel
Source: StackOverflow