---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: trash-updater
  namespace: downloads
  labels:
    app: trash-updater
spec:
  schedule: "0 3 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          automountServiceAccountToken: false

          initContainers:
            - name: render-config
              image: ghcr.io/k8s-at-home/trash-updater:v1.8.2
              env:
                - name: RADARR_API_KEY
                  valueFrom:
                    secretKeyRef:
                      name: trash-updater-secrets
                      key: radarrApiKey
                - name: SONARR_API_KEY
                  valueFrom:
                    secretKeyRef:
                      name: trash-updater-secrets
                      key: sonarrApiKey
              command:
                - "/bin/bash"
                - -c
              args:
                - "envsubst < /config/trash.yaml > /shared/trash.yaml"
              volumeMounts:
                - name: config
                  mountPath: /config
                - name: shared
                  mountPath: /shared

          containers:
            - name: trash-updater-sonarr
              image: ghcr.io/k8s-at-home/trash-updater:v1.8.2
              imagePullPolicy: IfNotPresent
              env:
                - name: TZ
                  value: "${TIMEZONE}"
              command:
                - /app/trash
              args:
                - sonarr
                - --config
                - /config/trash.yaml
              volumeMounts:
                - name: shared
                  mountPath: /config
                  readOnly: true

            - name: trash-updater-radarr
              image: ghcr.io/k8s-at-home/trash-updater:v1.8.2
              imagePullPolicy: IfNotPresent
              env:
                - name: TZ
                  value: "${TIMEZONE}"
              command:
                - /app/trash
              args:
                - radarr
                - --config
                - /config/trash.yaml
              volumeMounts:
                - name: shared
                  mountPath: /config
                  readOnly: true

          restartPolicy: OnFailure

          volumes:
            - name: config
              configMap:
                name: trash-updater-config
            - name: shared
              emptyDir: {}
