Sumit Murari
Sumit Murari

Reputation: 1689

How to get Pod creation time in pod spec yaml file

Is it possible to get timestamp using downward API or any other way in pod spec

Checked here https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#capabilities-of-the-downward-api, but couldn't find timestamp.

Below is sample section from yaml where i want to generate output file with timestamp.

          args:
            - "audit"
            - "--config"
            - "/opt/app/app-config/app.yaml"
            - "--format"
            - "json"
            - "--output-file"
            - "/var/log/app/app-$(date +%s).log"

Similar questions:

Upvotes: 1

Views: 6120

Answers (1)

Jakub
Jakub

Reputation: 8830

As mentioned in comments by @David Maze

You can wrap this in a sh -c invocation to cause a shell to do the subprocess expansion.


Other way here would be to use kubectl set.

You can use

kubectl set env deployment/deployment-name TIMESTAMP=$(kubectl get pod pod-name -o jsonpath='{.metadata.creationTimestamp}')

This will set creationTimestamp value as the env variable with TIMESTAMP name.

You can check the results with kubectl describe pod pod-name and search for Environment.


Additionally you can use

kubectl -n namespace get pod pod_name -o jsonpath="{range .status.conditions[*]}{.type}{','}{.lastTransitionTime}{'\n'}{end}"

to get all the events of the pod with timestamps

Upvotes: 1

Related Questions