---
apiVersion: batch/v1
kind: CronJob
metadata:
  labels:
    app.kubernetes.io/component: reposync-almalinux9-baseos
    app.kubernetes.io/name: reposync
  name: reposync-almalinux9-baseos
  namespace: reposync
spec:
  schedule: "0 2 * * *"
  timeZone: "Australia/Sydney"
  concurrencyPolicy: Forbid
  suspend: false
  successfulJobsHistoryLimit: 10
  failedJobsHistoryLimit: 5
  jobTemplate:
    spec:
      completions: 1
      parallelism: 1
      backoffLimit: 3
      ttlSecondsAfterFinished: 3600
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: reposync
              image: git.unkin.net/unkin/almalinux9-base:latest
              imagePullPolicy: Always
              command: ["/bin/bash", "-c"]
              args:
                - |
                  set -euo pipefail
                  echo "Starting AlmaLinux $REPO_TYPE repository sync..."

                  # Install reposync
                  dnf install -y dnf-plugins-core

                  # Create repo directory
                  mkdir -p /data/almalinux/$REPO_TYPE

                  # Sync repository
                  dnf reposync \
                    --repoid=$REPO_TYPE \
                    --destdir=/data/almalinux/$REPO_TYPE \
                    --download-metadata \
                    --newest-only \
                    --delete

                  echo "AlmaLinux $REPO_TYPE repository sync completed successfully"
              env:
                - name: REPO_TYPE
                  value: "baseos"
              resources:
                requests:
                  cpu: 500m
                  memory: 1Gi
                limits:
                  cpu: 2000m
                  memory: 4Gi
              volumeMounts:
                - name: repodata
                  mountPath: /data
                  readOnly: false
                - name: dnf-conf
                  mountPath: /etc/dnf/dnf.conf
                  subPath: dnf.conf
                  readOnly: true
          volumes:
            - name: repodata
              persistentVolumeClaim:
                claimName: reposync-almalinux9-baseos-repodata
            - name: dnf-conf
              configMap:
                name: dnf-conf
