Home > OS >  How to pass creation timestamp to kubernetes cronjob
How to pass creation timestamp to kubernetes cronjob

Time:07-06

Inside my container I have in my spec.jobTemplate.spec.template.spec:

          containers:
            - name: "run"
              env:
                {{ include "schedule.envVariables" . | nindent 16 }}
                - name: POD_NAME
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.name
                - name: POD_CREATION_TIMESTAMP
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.creationTimestamp

However I just get the error:

CronJob.batch "schedule-3eb71b12d3" is invalid: spec.jobTemplate.spec.template.spec.containers[0].env[19]

When changing to:

- name: POD_CREATION_TIMESTAMP
  value: ""

I get no errors. Any idea?

CodePudding user response:

The reason is that fieldRef doesn't support the use of metadata.creationTimestamp.

$ kubectl explain job.spec.template.spec.containers.env.valueFrom

...
   fieldRef <Object>
     Selects a field of the pod: supports metadata.name, metadata.namespace,
     `metadata.labels['<KEY>']`, `metadata.annotations['<KEY>']`, spec.nodeName,
     spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs.
...

CodePudding user response:

Kubernetes lacks that capability. When I try to add that field using fieldPath, I see:

field label not supported: metadata.creationTimestamp

The only guaranteed way of getting that value (that I can think of) would be to give your cronjob RBAC access to request that info at runtime. Then, you could run this from inside the pod to get that value:

kubectl get pod ${POD_NAME} -o=jsonpath='{.metadata.creationTimestamp}'
  • Related