How to read the kubernetes pod's creation timestamp with Terraform

83 Views Asked by At

I build a Kubernetes cronjob scheduled every minute. I need to capture the pod creation timestamp (some pods are created quickly but take time to startup) for a specific functional reason. With Terraform it is possible to set environment variables (job_template / spec / template / container / env / value_from) but the documentation does not mention the pod creation_timestamp inner property. Is there any way to populate a environment variable with this information using Terraform ? Thanks for your help

1

There are 1 best solutions below

1
On

Kubernetes's mechanism for exposing details of the pod spec to the running process is the downward API, with a specific environment variable path. The Terraform kubernetes_pod documentation lists some specific fields that field_ref and resource_field_ref allow, which exactly match the available fields in the downward API.

None of these fields are the creation timestamp of the Pod, so this information isn't available via the downward API, and you can't get it in an environment variable. You can generally see the Pod's name, namespace, annotations, labels, IP addresses, and resource requests and limits, but not arbitrary fields from the Pod spec.

For many practical purposes, it may be enough to just capture a time stamp as the first thing the Job's main process does. As you note, there may be a substantial delay between the CronJob triggering, the Pod being created, and the process actually starting (imagine for example the cluster autoscaler needing to provision a new node for the Pod to run on), and I don't believe this delay is visible to the process at all.