Use an environment variable as integer in yaml file

10/31/2019

I have an application in a container which reads a YAML file which contains data like

              initializationCount=0
              port=980

Now that I want to remove those hard coded values inside the application and get them out of the container. Hence I created a configMap with all configuration values. I used the config map keys as environmental variables while deploying the pod.

My issue is that, If I want to use these environment variables in my yaml file like

            initializationCount=${iCount}
            port=${port}

The API which reads this YAML file throws number format Exception since the env variables are always strings. I do not have control over the API which reads my yaml file.

I have tried

            initializationCount=!!int ${iCount} 

but it does not work.

-- Bhargav Behara
configmap
kubernetes

1 Answer

10/31/2019

Rather than pulling in the configmap values as environment variables, try mounting the configmap as a volume at runtime.

The configmap should have one key which is the name of your YAML file. the value for that key should be the contents of the file.

This data will be mounted to the container's filesystem when the pod initializes. That way your app will read the config YAML the same way it has been, but the values will be externalized in the configmap.

Something like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-app
      image: my-app:latest
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: app-config
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  config.yaml: |
    initializationCount=0
    port=980

Kubernetes docs here

-- switchboard.op
Source: StackOverflow