apiVersion: batch/v1
kind: CronJob
metadata:
  name: "{{ id }}"
  labels:
    role: "cron-background-job"
    job_type: {{ job_type }}
    btrix.org: {{ oid }}

spec:
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 2

  schedule: "{{ schedule }}"

  jobTemplate:
    metadata:
      labels:
        role: "background-job"
        job_type: {{ job_type }}
        job_id: {{ id }}
        btrix.org: {{ oid }}

    spec:
      template:
        spec:
          restartPolicy: Never
          priorityClassName: bg-job
          podFailurePolicy:
            rules:
            - action: FailJob
              onExitCodes:
                containerName: rclone
                operator: NotIn
                values: [0]

          containers:
            - name: rclone
              image: rclone/rclone:latest
              env:

              - name: RCLONE_CONFIG_REPLICA_TYPE
                value: "s3"

              - name: RCLONE_CONFIG_REPLICA_ACCESS_KEY_ID
                valueFrom:
                  secretKeyRef:
                    name: "{{ replica_secret_name }}"
                    key: STORE_ACCESS_KEY

              - name: RCLONE_CONFIG_REPLICA_SECRET_ACCESS_KEY
                valueFrom:
                  secretKeyRef:
                    name: "{{ replica_secret_name }}"
                    key: STORE_SECRET_KEY

              - name: RCLONE_CONFIG_REPLICA_REGION
                valueFrom:
                  secretKeyRef:
                    name: "{{ replica_secret_name }}"
                    key: STORE_REGION

              - name: RCLONE_CONFIG_REPLICA_PROVIDER
                valueFrom:
                  secretKeyRef:
                    name: "{{ replica_secret_name }}"
                    key: STORE_S3_PROVIDER

              - name: RCLONE_CONFIG_REPLICA_ENDPOINT
                value: "{{ replica_endpoint }}"

              command: ["rclone", "-vv", "delete", "replica:{{ replica_file_path }}"]

              resources:
                limits:
                  memory: "200Mi"

                requests:
                  memory: "200Mi"
                  cpu: "50m"
