'Use environment variable as default for another env variable in Kubernetes

Is there a way to use an environment variable as the default for another? For example:

apiVersion: v1
kind: Pod
metadata:
  name: Work
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: ALWAYS_SET
      value: "default"
    - name: SOMETIMES_SET
      value: "custom"

    - name: RESULT
      value: "$(SOMETIMES_SET) ? $(SOMETIMES_SET) : $(ALWAYS_SET)"


Solution 1:[1]

I don't think there is a way to do that but anyway you can try something like this

apiVersion: v1
kind: Pod
metadata:
  name: Work
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    args:
    - RESULT=${SOMETIMES_SET:-${ALWAYS_SET}}; command_to_run_app
    command:
    - sh
    - -c
    env:
    - name: ALWAYS_SET
      value: "default"
    - name: SOMETIMES_SET
      value: "custom"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shankar