How to fix environment variables not being picked up in a .json file

7/21/2019

I am setting environment variables in k8s through a configMap. Also creating a generate.json file and mounting it via a configMap also. All this works fine. The problem is that the env vars are not being picked up by generate.json file.

I am trying get the environment variables passed by the configMap to the generate.json that is mounted via a configMap. My guess is that, the file generate.json will read container env vars passed via configMap and interpolate those where neccesary.

Here are the 2 config maps

ConfigMap to create the env variables

apiVersion: v1
kind: ConfigMap
metadata: 
  name: config-cm
  namespace: default
data:
  ADMIN_PW: "P@ssw0rd"
  EMAIL: "support@gluu.com"
  ORG_NAME: "Gluu"
  COUNTRY_CODE: US
  STATE: TE
  CITY: SLC
  LDAP_TYPE: opendj
  GLUU_CONFIG_ADAPTER: kubernetes
  GLUU_SECRET_ADAPTER: kubernetes

---

`---configmap to interpolate the created environment vars---`

apiVersion: v1
kind: ConfigMap
metadata:
  name: gen-json-file
  namespace: default
data:
  generate.json: |-
    {
      "hostname": "$DOMAIN",
      "country_code": "$COUNTRY_CODE",
      "state": "$STATE",
      "city": "$CITY",
      "admin_pw": "$ADMIN_PW",
      "email": "$EMAIL",
      "org_name": "$ORG_NAME"
    }

---
apiVersion: batch/v1
kind: Job
metadata:
  name: config-init
spec:
  parallelism: 1
  template:
    metadata:
      name: job
      labels:
        app: load
    spec:
      volumes:
        - name: config
          persistentVolumeClaim:
            claimName: config-volume-claim
        - name: mount-gen-file
          configMap:
            name: gen-json-file
      containers:
      - name: load
        image: gluufederation/config-init:4.0.0_dev
        lifecycle:
          preStop:
            exec:
              command: [ "/bin/sh", -c , "printenv" ]
        volumeMounts:
          - mountPath: /opt/config-init/db/
            name: config
          - mountPath: /opt/config-init/db/generate.json
            name: mount-gen-file
            subPath: generate.json
        envFrom:
        - configMapRef:
            name: config-cm
        args: [ "load" ]
      restartPolicy: Never

The expected result is that the file generate.json should be populated/interpolated from env vars to the variables.

-- Shammir
configmap
containers
docker
json
kubernetes

0 Answers