{{- if eq .Values.registryBackup.obc.action "restore" -}}
{{ range $index, $bucket := (lookup "objectbucket.io/v1alpha1" "ObjectBucketClaim" $.Values.configuration.registryName "").items }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "bucket-replication.fullname" $ }}-{{ $bucket.metadata.name}}
  namespace: {{ $.Release.Namespace }}
  labels:
    {{- include "bucket-replication.labels" $ | nindent 4 }}
    obc-bucket-name: {{ $bucket.metadata.name }}
spec:
  template:
    metadata:
      name: {{ include "bucket-replication.fullname" $ }}
      labels:
        {{- include "bucket-replication.labels" $ | nindent 8 }}
        obc-bucket-name: {{ $bucket.metadata.name }}
    spec:
      serviceAccountName: {{ include "bucket-replication.serviceAccountName" $ }}
      containers:
        - name: {{ include "bucket-replication.fullname" $ }}
          image: {{ include "bucket-replication.image" $ }}
          env:
            - name: BACKUP_BUCKET
              value: {{ include "bucket-replication.backupBucket" $ }}
            - name: REGISTRY_NAMESPACE
              value: {{ $.Values.configuration.registryName }}
            - name: OBJECT_BUCKET_CLAIM
              value: {{ $bucket.metadata.name }}
            - name: FORCE_SYNC
              value: {{ $.Values.configuration.forceSync | quote }}
            - name: MAX_AGE
              value: {{ $.Values.registryBackup.obc.maxAge | quote }}
            - name: S3_ENDPOINT
              value: {{ include "bucket-replication.minioEndpoint"  $ }}
          command: [ "/bin/sh" ]
          args:
            - '-c'
            - /opt/scripts/bucket-replication.sh
          volumeMounts:
            - name: {{ include "bucket-replication.fullname" $ }}
              mountPath: /opt/scripts
          envFrom:
            - secretRef:
                name: {{ include "bucket-replication.secretName" $ }}
      restartPolicy: OnFailure
      tolerations:
        - key: node/{{ $.Values.configuration.registryName }}
          operator: Exists
      volumes:
        - name: {{ include "bucket-replication.fullname" $ }}
          configMap:
            name: {{ include "bucket-replication.fullname" $ }}
            defaultMode: 0755
{{- end }}
{{- end }}