3

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.

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

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?

David Maze
  • 130,717
  • 29
  • 175
  • 215
ramesh reddy
  • 429
  • 2
  • 5
  • 12
  • 1
    could you share the file from which you are trying to make the configmap. the keys are invalid in your file so you need to check those. – Tarun Khosla Jul 22 '20 at 08:39
  • this is how data in my json file. { "logging.level.com.ing": "INFO", "logging.level.org.springframework": "INFO", "server.port": "\"8443\"", "spring.profiles.active": "okd", "server.ssl.enabled": "\"true\"" } – ramesh reddy Jul 23 '20 at 06:35
  • https://stackoverflow.com/questions/49478036/populate-configmap-by-importing-data-from-file-in-k8s Check if this helps – Tarun Khosla Jul 23 '20 at 06:50
  • is it possible to add like below valueFrom: configMapKeyRef: key: {{ key }} name: sample-configmoap – ramesh reddy Jul 23 '20 at 09:00
  • Please take a look at the docs: [ConfigMap Restrictions](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#restrictions). Please provide reproducible example for this configMap as an yaml file as mentioned by [@tarun khosla](https://stackoverflow.com/users/4518472/tarun-khosla) – Mark Jul 23 '20 at 13:30
  • here is the files 1. application_env_variable.json: { "logging.level.com.ing" : "DEBUG", "logging.level.org.springframework" : "DEBUG", } application.properties: logging.level.com.ing=${logging.level.com.ing} logging.level.org.springframework=${logging.level.org.springframework} you can create configmap by including these two file and try to add to pod as environment variables. and let me know thanks in advance – ramesh reddy Jul 24 '20 at 07:23
  • It's not reproducible but very interesting (i can't trigger this error) However as stated in the docs and in the comments using env variables you should consider best practice for env variables like: `using uppercase letters, digits and underscores`. In order to deal further with this issue please update your post with some reproducible example using yaml files for this scenario and commands did you use. For the second example used in this post, you are remapping existing key from configMap (containing dot) into SPECIAL_LEVEL_KEY variable name. – Mark Jul 24 '20 at 11:15

3 Answers3

1

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
  • 1,427
  • 2
  • 12
  • 26
0

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
Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
  • i am creating configmap out files (.json and .properties files which have different data format) when i am adding as envFrom: - configMapRef: name: special-config it warns invalid variable names – ramesh reddy Jul 23 '20 at 06:20
  • dont use json file. convert it into properties file with key-value pairs. – P Ekambaram Jul 23 '20 at 06:26
0

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.