Reference environment variables

When creating pods, you can reference the value of a previously defined environment variable by using the $() syntax. By using this syntax, you can reference variables that you define within a pod configuration elsewhere in that configuration.

If the environment variable reference cannot be resolved, the value will be left as the provided string.

Procedure
  1. Create a pod that references an existing environment variable:

    1. Create a pod.yaml file similar to the following:

      apiVersion: v1
      kind: Pod
      metadata:
        name: dapi-env-test-pod
      spec:
        securityContext:
          runAsNonRoot: true
          seccompProfile:
            type: RuntimeDefault
        containers:
          - name: env-test-container
            image: gcr.io/google_containers/busybox
            command: [ "/bin/sh", "-c", "env" ]
            env:
              - name: MY_EXISTING_ENV
                value: my_value
              - name: MY_ENV_VAR_REF_ENV
                value: $(MY_EXISTING_ENV)
            securityContext:
              allowPrivilegeEscalation: false
              capabilities:
                drop: [ALL]
        restartPolicy: Never
      # ...
    2. Create the pod from the pod.yaml file by using the following command:

      $ oc create -f pod.yaml
Verification
  • Check the container’s logs for the MY_ENV_VAR_REF_ENV value by using the following command:

    $ oc logs -p dapi-env-test-pod