For some reason I needed a running pod to be able to access the value of its own container image(s). I initially thought of the Downward API but while it can give you the pod’s name, IP and other stuff, and even access resource limits per container using their name, the image is one of the attributes that is not immediately available. But the Downward API allows for metadata annotations to be available. So one can define a pod like:
apiVersion: v1 kind: Pod metadata: annotations: nginx_container: bitnami/nginx:1.23.2 labels: app: nginx name: nginx spec: containers: - image: bitnami/nginx:1.23.2 name: nginx env: - name: IMAGE valueFrom: fieldRef: fieldPath: metadata.annotations['nginx_container']
But this has the drawback of defining the image value in two places in the same file. Unless you can template this out with helm, or you generate the YAML via some other automation, this is an invitation for a bug the time when you will forget to update both places. Is there a better way?
Of course there is, and I might have seen it sooner had I not been fixed on utilizing the Downward API. The answer is YAML anchors and aliases:
apiVersion: v1 kind: Pod metadata: labels: app: nginx name: nginx spec: containers: - image: &nginx_image bitnami/nginx:1.23.2 name: nginx env: - name: IMAGE value: *nginx_image