Pass current date to kubernetes cronjob

Viewed 896

I have a docker image that receive an env var name SINCE_DATE.
I have created a cronjob to run that container and I want to pass it the current date.

How can I do it?

Trying this, I get the literal string date -d "yesterday 23:59"

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cron
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          containers:
          - name: my-cron
            image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
            imagePullPolicy: {{ .Values.image.pullPolicy }}
            env:
            - name: SINCE_DATE
              value: $(date -d "yesterday 23:59")
1 Answers

You could achieve it by overwriting container Entrypoint command and set environment variable. In your case it would looks like:

          containers:
          - name: my-cron
            image: nginx
            #imagePullPolicy: {{ .Values.image.pullPolicy }}
            command:
            - bash
            - -c
            - |
                  export SINCE_DATE=`date -d "yesterday 23:59"`
                  exec /docker-entrypoint.sh           

Note:

Nginx docker-entrypoint.sh in located in / If your image have different path, you should use it, for example exec /usr/local/bin/docker-entrypoint.sh

Very similar use-case can be found in this Stack question

What does this solution?

It will overwrite default script set in the container ENTRYPOINT with the same script but beforehand set dynamically environment variable.

Related