{{- if and .Values.s3.enabled .Values.persistence.enabled }}
{{- fail "\n\nCannot configure both s3 and PV for storing backups" }}
{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "backupRestore.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "backupRestore.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "backupRestore.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "backupRestore.selectorLabels" . | nindent 8 }}
      annotations:
        checksum/s3: {{ include (print $.Template.BasePath "/s3-secret.yaml") . | sha256sum }}
        checksum/pvc: {{ include (print $.Template.BasePath "/pvc.yaml") . | sha256sum }}
        {{- if .Values.monitoring.metrics.enabled }}
        prometheus.io/port: "metrics"
        prometheus.io/scrape: "true"
        {{ end }}
    spec:
      serviceAccountName: {{ include "backupRestore.serviceAccountName" . }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{ toYaml .Values.imagePullSecrets | indent 6 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: {{ template "system_default_registry" . }}{{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ default "Always" .Values.imagePullPolicy }}
        ports:
        - containerPort: 8080
        args:
{{- if .Values.debug }}
        - "--debug"
{{- end }}
{{- if .Values.trace }}
        - "--trace"
{{- end }}
        env:
        - name: CHART_NAMESPACE
          value: {{ .Release.Namespace }}
          {{- if .Values.s3.enabled }}
        - name: DEFAULT_S3_BACKUP_STORAGE_LOCATION
          value: {{ include "backupRestore.s3SecretName" . }}
          {{- end }}
          {{- if .Values.proxy }}
        - name: HTTP_PROXY
          value: {{ .Values.proxy }}
        - name: HTTPS_PROXY
          value: {{ .Values.proxy }}
        - name: NO_PROXY
          value: {{ .Values.noProxy }}
          {{- end }}
          {{- if .Values.monitoring.metrics.enabled }}
        - name: METRICS_SERVER
          value: "true"
          {{ end }}
          {{- if .Values.persistence.enabled }}
        - name: DEFAULT_PERSISTENCE_ENABLED
          value: "persistence-enabled"
        volumeMounts:
        - mountPath: "/var/lib/backups"
          name: pv-storage
      volumes:
        - name: pv-storage
          persistentVolumeClaim:
            claimName: {{ include "backupRestore.pvcName" . }}
          {{- end }} 
      nodeSelector: {{ include "linux-node-selector" . | nindent 8 }}
{{- if .Values.nodeSelector }}
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      tolerations: {{ include "linux-node-tolerations" . | nindent 8 }}
{{- if .Values.tolerations }}
{{ toYaml .Values.tolerations | indent 8 }}
{{- end }}
