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
-
Create a pod that references an existing environment variable:
-
Create a
pod.yamlfile 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 # ... -
Create the pod from the
pod.yamlfile by using the following command:$ oc create -f pod.yaml
-
Verification
-
Check the container’s logs for the
MY_ENV_VAR_REF_ENVvalue by using the following command:$ oc logs -p dapi-env-test-pod