kubernetes, parametrize env var after |- operand

4/3/2020

I have one yaml file that contains the following:

...
volumeMounts:
      - mountPath: /var/lib/grafana
        name: grafana-storage
      - mountPath: /etc/grafana/provisioning/datasources
        name: grafana-datasources
        readOnly: false
...

and another file in witch the grafana-datasource volume is specified:

apiVersion: v1
kind: ConfigMap
metadata:
    name: grafana-datasources
    namespace: ccx-data-pipeline
    labels:
        grafana_datasource: '1'
data:
    prometheus.yaml: |-
        {
            "apiVersion": 1,
            "datasources": [
                {
                    "access":"proxy",
                    "editable": true,
                    "name": "prometheus-ccx-service",
                    "orgId": 1,
                    "type": "prometheus",
                    "url": "http://prometheus-service:9090",
                    "version": 1
                }
            ]
        }

the point is that

"url": "http://prometheus-service:9090" 

can't be hard coded but must be parametrized (ideally with an environment variable). It would be great to do like so:

"url": "${ENDPOINT}"

but it doesn't work, probably due to the |- syntax. Is there an elegant way to achieve this?

-- JoulinRouge
config
configuration
environment-variables
kubernetes
yaml

1 Answer

4/3/2020

It's not possible directly in kubernetes. ConfigMap treats underlying file as generic data type and can't template it, since it doesn't recognize its type (.yaml, .conf etc)

What you can do:

  1. apply some sort of templating to your .yaml file in advance (e.g. via Jinja2 template)
  2. generate different ConfigMaps from different files depending on your environments (staging, prod).
  3. generate ConfigMaps dynamically with kustomize
-- Anton Matsiuk
Source: StackOverflow