apiVersion: batch/v1
kind: Job
metadata:
  name: gitlab-migrate-to-longhorn
  namespace: gitlab
spec:
  backoffLimit: 4
  template:
    metadata:
      name: gitlab-migrate-to-longhorn
    spec:
      restartPolicy: Never
      containers:
        - name: migrate
          image: alpine:3.18
          command:
            - sh
            - -c
            - |
              set -euo pipefail
              apk add --no-cache rsync
              echo "Starting migration: /var/opt/gitlab (data)"
              rsync -aHAX --numeric-ids --delete /var/opt_gitlab_old/ /var/opt_gitlab_new/
              echo "Finished data copy. Starting config copy: /etc/gitlab"
              rsync -aHAX --numeric-ids --delete /etc_gitlab_old/ /etc_gitlab_new/
              echo "Migration complete"
          volumeMounts:
            - name: old-data
              mountPath: /var/opt_gitlab_old
            - name: new-data
              mountPath: /var/opt_gitlab_new
            - name: old-config
              mountPath: /etc_gitlab_old
            - name: new-config
              mountPath: /etc_gitlab_new
      volumes:
        - name: old-data
          persistentVolumeClaim:
            claimName: gitlab-data-pvc
        - name: new-data
          persistentVolumeClaim:
            claimName: gitlab-data-longhorn-pvc
        - name: old-config
          persistentVolumeClaim:
            claimName: gitlab-config-pvc
        - name: new-config
          persistentVolumeClaim:
            claimName: gitlab-config-longhorn-pvc