Using Kubernetes Downward Api while using configmap and envFrom configMapRef

2/3/2022

I'm using both env and envFrom in my k8s deployment manifest as below.

envFrom:
  - configMapRef:
      name: env
env:
  - name: POD_NAMESPACE
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace

I know I can inject env using K8s downward api using env as below.

env:
  - name: POD_NAMESPACE
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace
  - name: SOME_ENDPOINT
    value: mysvc.$(POD_NAMESPACE).svc.cluster.local

However, when I try to create env using ConfigMap, as below, I am not getting intended variable.

apiVersion: v1
kind: ConfigMap
metadata:
  name: env
data:
  SOME_ENDPOINT: mysvc.$(POD_NAMESPACE).svc.cluster.local

The expected result when I run printenv inside the container is SOME_ENDPOINT having mysvc.myns.svc.cluster.local value, but the result is SOME_ENDPOINT having value of mysvc.$(POD_NAMESPACE).svc.cluster.local itself.

Any solutions on injecting env using configmap with Downward Api?

Thanks!

-- Piljae Chae
configmap
kubernetes

0 Answers