ConfigMap value as input for another variable inside container

5/12/2021

How to use ConfigMap for $LOCAL_IP_DB variable declared in below section as input for another variable declared? $LOCAL_IP_DB is a generic key defined inside db-secret configmap, but there is another environment variable which needs it? How to make it work?

spec:
      containers:
        - env:
            - name: LOCAL_IP_DB
              valueFrom:
                configMapKeyRef:
                    name: db-secret
                    key: LOCAL_IP_DB
            - name: LOG_Files
              value: \\${LOCAL_IP_DB}\redis\files\
-- infotechie1
configmap
containers
environment-variables
kubernetes
nested

1 Answer

5/12/2021

The key is using: $() instead of ${}

example-pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: example
spec:
  containers:
  - name: example
    image: bash
    args: [printenv]
    env:
    - name: LOCAL_IP_DB
      valueFrom:
        configMapKeyRef:
          name: db-secret
          key: LOCAL_IP_DB
    - name: LOG_FILES
      value: \$(LOCAL_IP_DB)\redis\files\

example-configmap.yaml:

apiVersion: v1
data:
  LOCAL_IP_DB: 192.168.0.1
kind: ConfigMap
metadata:
  name: db-secret

test:

controlplane $ kubectl apply -f example-pod.yaml -f example-configmap.yaml 
controlplane $ kubectl logs example | grep 192
LOCAL_IP_DB=192.168.0.1
LOG_FILES=\192.168.0.1\redis\files\

You can find more information about this function here: link

Note, if you want to manage secrets Secret is recommended way to do that.

-- csenga
Source: StackOverflow