简体   繁体   English

如何将创建时间戳传递给 Kubernetes cronjob

[英]How to pass creation timestamp to kubernetes cronjob

Inside my container I have in my spec.jobTemplate.spec.template.spec :在我的容器中,我的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] CronJob.batch "schedule-3eb71b12d3" 无效:spec.jobTemplate.spec.template.spec.containers[0].env[19]

When changing to:当更改为:

- name: POD_CREATION_TIMESTAMP
  value: ""

I get no errors.我没有错误。 Any idea?任何想法?

The reason is that fieldRef doesn't support the use of metadata.creationTimestamp .原因是fieldRef不支持使用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.
...

Kubernetes lacks that capability. Kubernetes 缺乏这种能力。 When I try to add that field using fieldPath , I see:当我尝试使用fieldPath添加该字段时,我看到:

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.获得该值的唯一有保证的方法(我能想到的)是让您的 cronjob RBAC 访问以在运行时请求该信息。 Then, you could run this from inside the pod to get that value:然后,您可以从 pod 内部运行它以获取该值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM