Set Kubernetes env variable from container's image version

5/18/2020

I was wondering if it's possible to refer to image field in Kubernetes deployment yaml file, as

    env:
    - name: VERSION
      value:
        valueFrom:
          containerField: spec.image

Please let me know. Thank you.

-- Arsen
kubernetes

1 Answer

5/21/2020

image value in pod definition cannot be passed as environment variable using fieldRef.

The only supported values are metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs and resource fields (memory, cpu request/limits) and container ephemeral storage limit/request.

As a workaround it can be passed using labels and then passing this label as an environment variable, example:

    env:
    - name: VERSION
      valueFrom:
        fieldRef: 
          fieldPath: metadata.labels['version']
-- KFC_
Source: StackOverflow