3

I am trying to get either my deployment manifest or a configmap approach to pass in such a set of variables:

DIRECTORY=/home/test  
TOKEN=$(cat $(DIRECTORY)/token)

In both my deployment manifest, and my configmap attempts, the TOKEN=$(cat $(DIRECTORY)/token): gets set literally, and not resolved within the pod (basically it should be: TOKEN=/home/test/token)

Is it not possible to set vars this way with Kubenernetes? Is this due to the fact that the pod expects { } around the variables and not ( ) like kubenetes requires?

ddisec
  • 71
  • 1
  • 6
  • 1
    The expression `$(some command)` is a shell expression. Kubernetes is not your shell. You can do something like that at runtime in the `ENTRYPOINT` of your image, perhaps. – larsks Feb 16 '21 at 19:10

2 Answers2

0

If I good understood, you want to pass some environment variables to pod and then use them with another environment variable?

It can be achieved in Deployment using Dependent Environment Variables.

When you create a Pod, you can set dependent environment variables for the containers that run in the Pod. To set dependent environment variables, you can use $(VAR_NAME) in the value of env in the configuration file.

Test YAML

kind: Pod
metadata:
  name: dependent-envars-demo
spec:
  containers:
    - name: dependent-envars-demo
      args:
        - while true; do echo -en '\n'; printf DIRECTORY=$DIRECTORY'\n'; printf TOKEN=$TOKEN'\n'; sleep 300; done;
      command:
        - sh
        - -c
      image: busybox
      env:
        - name: SERVICE_PORT
          value: "80"
        - name: DIRECTORY
          value: "/home/test"
        - name: TOKEN
          value: "$(DIRECTORY)/token"

To verify if this is working, you should check logs.

DIRECTORY=/home/test
TOKEN=/home/test/token

Regarding ConfigMap, it should be consider as normal text, not bash commands. It help separate your code from configuration and it's mostly key: value type. However, there are some specific scenario, where ConfigMap contains script body and later it can be used in pod. Example can be found in another Stackoverflow thread or USING KUBERNETES CONFIGMAPS AS CODE article.

Last thing I want to mention that Kubernetes have ConfigMap for configuration changes and Secrets for more sensitive data.

PjoterS
  • 12,841
  • 1
  • 22
  • 54
  • In your Test YAML code, would I be able to set a value such as ```value: "$(cat $(DIRECTORY)/token)"``` ? – ddisec Feb 17 '21 at 19:02
  • If you want to run bash commands inside pod you would need to follow [Run a command in a shell](https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#run-a-command-in-a-shell) This way you could use bash inside the pod. But alternative solution could be to use [Init Container](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) to prepare some configuration before deploying another container. – PjoterS Feb 19 '21 at 12:36
0

I was looking for the same feature.. however, there is no straightforward way as per my research.

Alternatively, this pattern can work & you can generalize it:

  image:...
  command: ["/bin/sh", "-c"]
  args:
    - >-
      export TOKEN=$(cat /path/to/token)
      exec /bin/original/entrypoint
      
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254