Kubernetes set env variable to value from configMap after encoding it

7/22/2020

I have a Pod configuration file very similar to this example in the docs where I set a few env variables from a configMap file.
Now I need to add another variable but I need to base64 encode it. I can easily do it when I take data from values by applying the b64enc function, but I don't know how to do it when getting the value from a configMap

This is what I can do

env:
  - name: PLAIN_VALUE
    valueFrom:
      configMapKeyRef:
        name: myconfig
        key: PLAIN_VALUE
  - name: ENCODED_VALUE_FROM_VALUES
    value: {{ .Values.myConfig.plainValue | b64enc | quote }}

I would like to do something like the following

env:
  - name: ENCODED_VALUE
    valueFrom:
      configMapKeyRef:
        name: myconfig
        key: PLAIN_VALUE
        transformation: b64enc

How can I b64enc the valueFrom: configMapKeyRef: myconfig/PLAIN_VALUE?
P.S. configMapRef would also work, I can make a separate config file for that value.

-- Naigel
configmap
kubernetes
yaml

1 Answer

7/23/2020

In this scenario you should use secrets. It encodes the values in base64.

You can easily create a secret using kubectl command, for example:

kubectl create secret generic test-secret --from-literal='your_value'

And it works similarly to configmap when it comes to passing encoded values to pods.

env:
  - name: ENCODED_VALUE
    valueFrom:
      secretKeyRef:
        name: myconfig
        key: value
    
-- kool
Source: StackOverflow