{{- if .Values.configuration.provider -}}
{{- $provider := .Values.configuration.provider -}}
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "ark.fullname" . }}
  labels:
    release: {{ .Release.Name }}
    app: {{ template "ark.name" . }}
    chart: {{ template "ark.chart" . }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      release: {{ .Release.Name }}
      app: {{ template "ark.name" . }}
  template:
    metadata:
      labels:
        release: {{ .Release.Name }}
        app: {{ template "ark.name" . }}
      {{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
{{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
{{- if .Values.metrics.enabled }}
{{ toYaml .Values.metrics.podAnnotations | indent 8 }}
{{- end }}
      {{- end }}
    spec:
      restartPolicy: Always
      serviceAccountName: {{ template "ark.serverServiceAccount" . }}
      containers:
        - name: ark
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.metrics.enabled }}
          ports:
            - name: monitoring
              containerPort: 8085
          {{- end }}
          command:
            - /ark
          args:
            - server
          {{- with .Values.configuration }}
            {{- with .backupSyncPeriod }}
            - --backup-sync-period={{ . }}
            {{- end }}
            {{- with .resticTimeout }}
            - --restic-timeout={{ . }}
            {{- end }}
            {{- if .restoreOnlyMode }}
            - --restore-only
            {{- end }}
            {{- with .restoreResourcePriorities }}
            - --restore-resource-priorities={{ . }}
            {{- end }}
          {{- end }}
          {{- if eq $provider "azure" }}
          envFrom:
            - secretRef:
                name: {{ template "ark.secretName" . }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
{{ toYaml . | indent 12 }}
          {{- end }}
          volumeMounts:
            - name: plugins
              mountPath: /plugins
        {{- if and .Values.credentials.useSecret (or (eq $provider "aws") (eq $provider "gcp")) }}
            - name: cloud-credentials
              mountPath: /credentials
          env:
          {{- if eq $provider "aws" }}
            - name: AWS_SHARED_CREDENTIALS_FILE
          {{- else }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
          {{- end }}
              value: /credentials/cloud
          {{ if .Values.configuration.extraEnvVars }}
          {{- range $key, $value := .Values.configuration.extraEnvVars }}
            - name: {{ default "none" $key }}
              value: {{ default "none" $value }}
          {{- end }}
          {{- end }}
        {{- end }}
      volumes:
        {{- if and .Values.credentials.useSecret (or (eq $provider "aws") (eq $provider "gcp")) }}
        - name: cloud-credentials
          secret:
            secretName: {{ template "ark.secretName" . }}
        {{- end }}
        - name: plugins
          emptyDir: {}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- end -}}
