Unable to add configmap data as environment variables into pod. it says invalid variable name cannot be added as environmental variable

7/22/2020

I am trying to add config data as environment variables, but Kubernetes warns about invalid variable names. The configmap data contains JSON and property files.

<!-- language: lang-yaml -->
spec:
  containers:
    - name: env-var-configmap
      image: nginx:1.7.9 
      envFrom:
        - configMapRef:
            name: example-configmap

After deploying I do not see them added in the process environment. Instead I see a warning message like below

Config map example-configmap contains keys that are not valid environment variable names. Only config map keys with valid names will be added as environment variables.

But I see it works if I add it directly as a key-value pair

<!-- language: lang-yaml -->
env:
  # Define the environment variable
  - name: SPECIAL_LEVEL_KEY
    valueFrom:
      configMapKeyRef:
        # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
        name: special-config
        # Specify the key associated with the value
        key: special.how

I have thousand of key values in the configmap data and I could not add them all as separate key-value pairs.

Is there any short syntax to add all values from a configmap as environment variables?

-- ramesh reddy
kubernetes
openshift

3 Answers

7/22/2020

sample reference is given below

create configmap as shown below

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
load configmap data as environment variables in the pod

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config
  restartPolicy: Never
output

master $ kubectl logs dapi-test-pod | grep SPECIAL
SPECIAL_LEVEL=very
SPECIAL_TYPE=charm
-- P Ekambaram
Source: StackOverflow

2/22/2022

You should rename your variable.

In my case they were like this:

VV-CUSTOMER-CODE
VV-CUSTOMER-URL

I just rename to:

VV_CUSTOMER_CODE
VV_CUSTOMER_URL

Works fine. Openshift/kubernets works with underline _, but not with hyphen - .

I hope help you.

-- Marcelo Rebou&#231;as
Source: StackOverflow

1/27/2021

My answer, while @P-Ekambaram already helped you out, I was getting the same error message, it turned out that my issue was that I named the configMap ms-provisioning-broadsoft-adapter and was trying to use ms-provisioning-broadsoft-adapter as the key. As soon as I changed they key to ms_provisioning_broadsoft_adapter, e.g. I added the underscores instead of hyphens and it happily let me add it to an application.

Hope this might help someone else that also runs into the error invalid variable name cannot be added as environmental variable

-- JGlass
Source: StackOverflow