apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: data-sync-fs-to-eos-
spec:

  arguments: 
    parameters:
    - name: source_fs_name
      value: ""
    - name: source_path
      value: ""
    - name: destination_bucket_name
      value: ""
    - name: destination_path
      value: ""
    - name: bucket_endpoint_url
      value: https://objectstore.e2enetworks.net
    - name: bucket_access_key
      value: ""
    - name: bucket_secret_key
      value: ""

  entrypoint: data-sync-job

  volumes:
  - name: source-fs
    persistentVolumeClaim:
      claimName: "{{workflow.parameters.source_fs_name}}"

  templates:
    - name: data-sync-job
      metadata: {}
      container:
        name: mc-mirror
        image: minio/mc:RELEASE.2025-08-13T08-35-41Z
        command: ["/bin/sh", "-c"]
        args:
          - |
            echo "===== STARTING DATA-SYNC JOB ====="

            echo ">>> adding destination bucket..."
            mc alias set destination-bkt $MINIO_ENDPOINT_URL $MINIO_ACCESS_KEY $MINIO_SECRET_KEY

            DATA_SOURCE="/source-fs/$SOURCE_PATH"
            DATA_DESTINATION="destination-bkt/$TARGET_BUCKET_NAME/$DESTINATION_PATH"

            # collapse multiple slashes into a single slash (in case concatenation causes //)
            DATA_SOURCE=$(echo "$DATA_SOURCE" | tr -s '/')
            DATA_DESTINATION=$(echo "$DATA_DESTINATION" | tr -s '/')

            echo ">>> Details:"
            echo ">>> datasync source:  $DATA_SOURCE"
            echo ">>> datasync destination:  $DATA_DESTINATION"

            echo ">>> datasync started ..."
            mc mirror $DATA_SOURCE  $DATA_DESTINATION  --overwrite --preserve --retry --summary
            echo ">>> datasync completed."

            echo "===== DATA-SYNC JOB COMPLETE! ====="
        env:
        - name: MINIO_ENDPOINT_URL
          value: "{{workflow.parameters.bucket_endpoint_url}}"
        - name: TARGET_BUCKET_NAME
          value: "{{workflow.parameters.destination_bucket_name}}"
        - name: MINIO_ACCESS_KEY
          value: "{{workflow.parameters.bucket_access_key}}"
        - name: MINIO_SECRET_KEY
          value: "{{workflow.parameters.bucket_secret_key}}"
        - name: SOURCE_PATH
          value: "{{workflow.parameters.source_path}}"
        - name: DESTINATION_PATH
          value: "{{workflow.parameters.destination_path}}"
        volumeMounts:
        - name: source-fs
          mountPath: /source-fs
      inputs: {}
      outputs: {}
