0
apiVersion: v1
kind: ConfigMap
metadata:
  name: time-config
data:
  TIME_FREQ: "6"
---
apiVersion: v1
kind: Pod
metadata:
  name: time-check
spec:
  containers:
  - name: time-check
    image: busybox:latest
    command: ["/bin/sh"]
    args: ["-c","while true; do date; sleep $TIME_FREQ;done >> /opt/data/time/time-check.log"]
    env:
      - name: TIME_FREQ
        valueFrom:
          configMapKeyRef:
            name: time-config
            key: TIME_FREQ
    volumeMounts:
      - name: log-volume
        mountPath: /opt/data/time
  volumes:
  - name: log-volume
    emptyDir: {}

According to documentation, to use variable you should put it in parentheses, however, that gives me:

TIME_FREQ: command not found
sleep: missing operand

If I use only $TIME_FREQ, the variable does not expand at all and sleep only complains about missing operand. I've tried curly braces, but didn't work, too.

If I set the variable locally, the config will accept it and the command runs ok, with variable defined as $TIME_FREQ.

Why does it not work?

Output from get:

apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"time-check","namespace":"default"},"spec":{"containers":[{"args":["-c","while true; do date; sleep ;done \u003e\u003e /opt/data/time/time-check.log"],"command":["/bin/sh"],"env":[{"name":"TIME_FREQ","valueFrom":{"configMapKeyRef":{"key":"TIME_FREQ","name":"time-config"}}}],"image":"busybox:latest","name":"time-check","volumeMounts":[{"mountPath":"/opt/data/time","name":"log-volume"}]}],"volumes":[{"emptyDir":{},"name":"log-volume"}]}}
  creationTimestamp: "2024-09-01T12:07:41Z"
  name: time-check
  namespace: default
  resourceVersion: "28228"
  uid: 281e2819-df90-41c0-a5c4-b7203ed2ebec
spec:
  containers:
  - args:
    - -c
    - while true; do date; sleep ;done >> /opt/data/time/time-check.log
    command:
    - /bin/sh
    env:
    - name: TIME_FREQ
      valueFrom:
        configMapKeyRef:
          key: TIME_FREQ
          name: time-config
    image: busybox:latest
    imagePullPolicy: Always
    name: time-check
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /opt/data/time
      name: log-volume
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-bm6wc
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: minikube
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - emptyDir: {}
    name: log-volume
  - name: kube-api-access-bm6wc
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
            path: namespace
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2024-09-01T12:07:49Z"
    status: "True"
    type: PodReadyToStartContainers
  - lastProbeTime: null
    lastTransitionTime: "2024-09-01T12:07:41Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2024-09-01T12:07:49Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2024-09-01T12:07:49Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2024-09-01T12:07:41Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://abd378ce0519222c43fc194ba6952475176fba2bb37ee71a5ac9f6838239d2cb
    image: busybox:latest
    imageID: docker-pullable://busybox@sha256:82742949a3709938cbeb9cec79f5eaf3e48b255389f2dcedf2de29ef96fd841c
    lastState: {}
    name: time-check
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2024-09-01T12:07:49Z"
  hostIP: 192.168.49.2
  hostIPs:
  - ip: 192.168.49.2
  phase: Running
  podIP: 10.244.0.6
  podIPs:
  - ip: 10.244.0.6
  qosClass: BestEffort
  startTime: "2024-09-01T12:07:41Z"

And describe:

Name:             time-check
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Sun, 01 Sep 2024 14:07:41 +0200
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               10.244.0.6
IPs:
  IP:  10.244.0.6
Containers:
  time-check:
    Container ID:  docker://abd378ce0519222c43fc194ba6952475176fba2bb37ee71a5ac9f6838239d2cb
    Image:         busybox:latest
    Image ID:      docker-pullable://busybox@sha256:82742949a3709938cbeb9cec79f5eaf3e48b255389f2dcedf2de29ef96fd841c
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
    Args:
      -c
      while true; do date; sleep ;done >> /opt/data/time/time-check.log
    State:          Running
      Started:      Sun, 01 Sep 2024 14:07:49 +0200
    Ready:          True
    Restart Count:  0
    Environment:
      TIME_FREQ:  <set to the key 'TIME_FREQ' of config map 'time-config'>  Optional: false
    Mounts:
      /opt/data/time from log-volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bm6wc (ro)
Conditions:
  Type                        Status
  PodReadyToStartContainers   True 
  Initialized                 True 
  Ready                       True 
  ContainersReady             True 
  PodScheduled                True 
Volumes:
  log-volume:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:     
    SizeLimit:  <unset>
  kube-api-access-bm6wc:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  22m   default-scheduler  Successfully assigned default/time-check to minikube
  Normal  Pulling    22m   kubelet            Pulling image "busybox:latest"
  Normal  Pulled     22m   kubelet            Successfully pulled image "busybox:latest" in 7.077s (7.077s including waiting). Image size: 4261574 bytes.
  Normal  Created    22m   kubelet            Created container time-check
  Normal  Started    22m   kubelet            Started container time-check

2 Answers 2

0

As the docs says, you can only use UTF-8 strings. Try changing your ConfigMap to this

apiVersion: v1
kind: ConfigMap
metadata:
  name: time-config
data:
  TIME_FREQ: "6"

And leave your pod template as is. I just tested it, and it works. :)

send a message if you need more help

3
  • Thanks for the reply. Actually, I forgot to put the number between quotes in the question, fixed. It wouldn't even let me create the CM without it. Thought, I tried again and the env var still wouldn't pass through to the sleep command. I am using minikube for testing and the exercise is from kodekloud, both environments the same result. sleep is without argument and sends into log sh: sleep: missing operand Commented Sep 1 at 12:18
  • @user27155638 Please provide full yaml output of the pod manifest so i could get more context. also logs output of the pod. it seems that it is a problem with the shell kodekloud provides maybe.
    – Eladik
    Commented Sep 1 at 12:22
  • Please, see the edits in question with get and describe outputs. In kubectl logs, there is only endless stream of sh: sleep: missing operand Commented Sep 1 at 12:37
0

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 https://stackoverflow.com/questions/56752494/variable-isnt-initialized-in-script-passed-through-heredoc

1
  • Can you improve your self-answer with some explanation of how the SO post helped and some code?
    – Peter Turner
    Commented Sep 2 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.