Can we use the data defined in configmap as a key in deployment?

Viewed 66

I have a configmap created. I have added data as a key-value pair in configmap

data:
  EXTERNAL_CONFIG_FILE: /user/app/api/config

I need to use this variable to set a mount path in deployment

-  name: config-properties-volume-mnt
   mountPath: {{ $EXTERNAL_CONFIG_FILE }}

I am getting undefined variable "$EXTERNAL_CONFIG_FILE" while deploying. I do not want to define this variable in values.yaml. Is there a way where I can use this variable defined in configmap in deployment?

1 Answers

It's not possible to dynamically define any parameter on a manifest, you have to use the Helm or Kustomize

Or else you can use the sed to replace the Text in manifest simply

You can not use this way, configmap and secret are normally to inject the variables or file into the POD not at declare step.

-  name: config-properties-volume-mnt
   mountPath: {{ $EXTERNAL_CONFIG_FILE }}

if you have helm chart keeping details into the vaules.yaml is the only option.

Subpath method :

You can use the subpath to achieve, with subpath you can use the environment: https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: container1
    env:
    - name: EXTERNAL_CONFIG_FILE
      value: /user/app/api/config
    image: busybox:1.28
    command: [ "sh", "-c", "while [ true ]; do echo 'Hello'; sleep 10; done | tee -a /logs/hello.txt" ]
    volumeMounts:
    - name: workdir1
      mountPath: /logs
      # The variable expansion uses round brackets (not curly brackets).
      subPathExpr: $(EXTERNAL_CONFIG_FILE)
  restartPolicy: Never
  volumes:
  - name: workdir1
    hostPath:
      path: /var/log/pods

Instead of env used in the above example you can use the configmap

Related