apiVersion: batch/v1
kind: Job
metadata:
  name: ${FQDN}-sync-buckets
  namespace: ${DOMAIN}
spec:
  template:
    spec:
      containers:
      - command: [/sync/sync.sh]
        env:
        - name: RO_OBJECTSTORE_S3_KEY
          valueFrom:
            secretKeyRef:
              key: AWS_ACCESS_KEY_ID 
              name: s3-ro
        - name: RO_OBJECTSTORE_S3_SECRET 
          valueFrom:
            secretKeyRef:
              key: AWS_SECRET_ACCESS_KEY
              name: s3-ro
        - name: NEW_OBJECTSTORE_S3_KEY
          valueFrom:
            secretKeyRef:
              key: AWS_ACCESS_KEY_ID 
              name: ${FQDN}-s3-new
        - name: NEW_OBJECTSTORE_S3_SECRET 
          valueFrom:
            secretKeyRef:
              key: AWS_SECRET_ACCESS_KEY
              name: ${FQDN}-s3-new
        image: libresh/mc
        imagePullPolicy: IfNotPresent
        name: sync-buckets
        volumeMounts:
        - name: sync-script
          mountPath: /sync/
      volumes:
      - name: sync-script
        configMap:
          name: sync-script
          defaultMode: 0700
      restartPolicy: Never
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: sync-script
data:
  sync.sh: |
    #!/bin/bash -eux
    export MC_HOST_ro=https://${RO_OBJECTSTORE_S3_KEY}:${RO_OBJECTSTORE_S3_SECRET}@s3.standard.indie.host
    export MC_HOST_new=https://${NEW_OBJECTSTORE_S3_KEY}:${NEW_OBJECTSTORE_S3_SECRET}@hot-objects.liiib.re
    mc mirror --overwrite ro/${FQDN} new/${FQDN}
    trickled -d 200000 -u 200000
    trickle -d 200000 -u 200000 mc mirror --overwrite ro/${FQDN} new/${FQDN}
