Kubernetes environment variable expansion fails with `VAR: command not found`

I am working on a kodekloud excercise (Example 2) where I need to expand a variable, but it doesn’t work any way I tried. The “kubernetes” expansion results with VAR: command not found, and normal expansions $VAR or ${VAR} result with empty string, although the variable is loaded inside the container. Apart from kodekloud environment, I am testing this also in latest stable minikube.
Why does it not work?

Example 1

apiVersion: v1
kind: Pod
metadata:
  name: echo
spec:
  containers:
  - name: busy-echo
    image: busybox:latest
    env:
    - name: MESSAGE
      value: "hello world"
    command: ["/bin/echo"]
    args: ["$(MESSAGE)"]

Example 2

apiVersion: v1
kind: ConfigMap
metadata:
  name: time-config
data:
  TIME_FREQ: "4"
---
apiVersion: v1
kind: Pod
metadata:
  name: time-check
spec:
  containers:
  - name: time-check
    image: busybox:latest
    command: ["/bin/sh","-c"]
    args: ["while true; do date; sleep $TIME_FREQ; done"]
    env:
    - name: TIME_FREQ
      valueFrom:
        configMapKeyRef:
          name: time-config
          key: TIME_FREQ
1 Like

I figured out why it’s not working for me. I was using heredoc to create the file and didn’t escape the variable. For refrence, here’s some info bash - Variable isn't initialized in script passed through heredoc - Stack Overflow