{{/* 'securityContext' got renamed to 'podSecurityContext', merge both dicts into one for backward compatibility */}}
{{- $podSecurityContext := merge (.Values.podSecurityContext | default dict) (.Values.securityContext | default dict) -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "velero.fullname" . }}
  namespace: {{ .Release.Namespace }}
  {{- with .Values.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ include "velero.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: {{ include "velero.chart" . }}
    component: velero
    {{- with .Values.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: 1
  {{- if hasKey .Values "revisionHistoryLimit" }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  {{- end }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/name: {{ include "velero.name" . }}
  template:
    metadata:
      labels:
        name: velero
        app.kubernetes.io/name: {{ include "velero.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/version: {{ .Chart.AppVersion }}
        helm.sh/chart: {{ include "velero.chart" . }}
        {{- if .Values.podLabels }}
          {{- toYaml .Values.podLabels | nindent 8 }}
        {{- end }}
    {{- if or .Values.podAnnotations .Values.metrics.enabled (and .Values.credentials.useSecret (not .Values.credentials.existingSecret)) }}
      annotations:
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if and (.Values.metrics.enabled) (not .Values.metrics.serviceMonitor.enabled) }}
        {{- with .Values.metrics.podAnnotations }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end }}
      {{- if and .Values.credentials.useSecret (not .Values.credentials.existingSecret) }}
        checksum/secret: {{ template "chart.config-checksum" . }}
      {{- end }}
    {{- end }}
    spec:
      {{- with .Values.hostAliases }}
      hostAliases:
      {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- if .Values.image.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
      restartPolicy: Always
      serviceAccountName: {{ include "velero.serverServiceAccount" . }}
      automountServiceAccountToken: {{ .Values.serviceAccount.server.automountServiceAccountToken }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ include "velero.priorityClassName" . }}
      {{- end }}
      {{- if .Values.runtimeClassName }}
      runtimeClassName: {{ include "velero.runtimeClassName" . }}
      {{- end }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      containers:
        - name: velero
      {{- if .Values.image.digest }}
          image: "{{ .Values.image.repository }}@{{ .Values.image.digest }}"
      {{- else }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
      {{- end }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.metrics.enabled }}
          ports:
            - name: http-monitoring
              containerPort: 8085
          {{- end }}
          command:
            - /velero
          args:
            - server
            ### Flags
          {{- with .Values.configuration }}
            - --uploader-type={{ default "kopia" .uploaderType }}
            {{- with .backupSyncPeriod }}
            - --backup-sync-period={{ . }}
            {{- end }}
            {{- with .fsBackupTimeout }}
            - --fs-backup-timeout={{ . }}
            {{- end }}
            {{- with .clientBurst }}
            - --client-burst={{ . }}
            {{- end }}
            {{- with .clientPageSize }}
            - --client-page-size={{ . }}
            {{- end }}
            {{- with .clientQPS }}
            - --client-qps={{ . }}
            {{- end }}
            {{- with .defaultBackupStorageLocation }}
            - --default-backup-storage-location={{ . }}
            {{- end }}
            {{- with .defaultBackupTTL }}
            - --default-backup-ttl={{ . }}
            {{- end }}
            {{- with .defaultItemOperationTimeout }}
            - --default-item-operation-timeout={{ . }}
            {{- end }}
            {{- with .defaultVolumeSnapshotLocations }}
            - --default-volume-snapshot-locations={{ . }}
            {{- end }}
            {{- if .defaultVolumesToFsBackup }}
            - --default-volumes-to-fs-backup
            {{- end }}
            {{- with .defaultRepoMaintainFrequency }}
            - --default-repo-maintain-frequency={{ . }}
            {{- end }}
            {{- with .disableControllers }}
            - --disable-controllers={{ . }}
            {{- end }}
            {{- with .disableInformerCache }}
            - --disable-informer-cache={{ . }}
            {{- end }}
            {{- with .garbageCollectionFrequency }}
            - --garbage-collection-frequency={{ . }}
            {{- end }}
            {{- with .itemBlockWorkerCount }}
            - --item-block-worker-count={{ . }}
            {{- end }}
            {{- with .logFormat }}
            - --log-format={{ . }}
            {{- end }}
            {{- with .logLevel }}
            - --log-level={{ . }}
            {{- end }}
            {{- with .metricsAddress }}
            - --metrics-address={{ . }}
            {{- end }}
            {{- with .pluginDir }}
            - --plugin-dir={{ . }}
            {{- end }}
            {{- with .profilerAddress }}
            - --profiler-address={{ . }}
            {{- end }}
            {{- if .restoreOnlyMode }}
            - --restore-only
            {{- end }}
            {{- with .restoreResourcePriorities }}
            - --restore-resource-priorities={{ . }}
            {{- end }}
            {{- with .storeValidationFrequency }}
            - --store-validation-frequency={{ . }}
            {{- end }}
            {{- with .terminatingResourceTimeout }}
            - --terminating-resource-timeout={{ . }}
            {{- end }}
            {{- with .defaultSnapshotMoveData }}
            - --default-snapshot-move-data={{ . }}
            {{- end }}
            ### Global Flags
            {{- with .features }}
            - --features={{ . }}
            {{- end }}
            {{- with .namespace }}
            - --namespace={{ . }}
            {{- end }}
            {{- with .repositoryMaintenanceJob }}
            {{- if and .repositoryConfigData (or .repositoryConfigData.global .repositoryConfigData.repositories) }}
            - --repo-maintenance-job-configmap={{ default "velero-repo-maintenance" .repositoryConfigData.name }}
            {{- else }}
            {{- with .requests }}
            {{- with .cpu }}
            - --maintenance-job-cpu-request={{ . }}
            {{- end }}
            {{- with .memory }}
            - --maintenance-job-mem-request={{ . }}
            {{- end }}
            {{- end }}
            {{- with .limits }}
            {{- with .cpu }}
            - --maintenance-job-cpu-limit={{ . }}
            {{- end }}
            {{- with .memory }}
            - --maintenance-job-mem-limit={{ . }}
            {{- end }}
            {{- end }}
            {{- with .latestJobsCount }}
            - --keep-latest-maintenance-jobs={{ . }}
            {{- end }}
            {{- end }}
            {{- end }}
            {{- with .extraArgs }}
            ### User-supplied overwrite flags
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.resizePolicy }}
          resizePolicy:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.metrics.enabled }}
          {{- with .Values.livenessProbe }}
          livenessProbe: {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.readinessProbe }}
          readinessProbe: {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- with .Values.containerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: plugins
              mountPath: /plugins
            {{- if .Values.credentials.useSecret }}
            - name: cloud-credentials
              mountPath: /credentials
            {{- end }}
            - name: scratch
              mountPath: /scratch
            {{- if .Values.containerSecurityContext.readOnlyRootFilesystem }}
            - name: tmpdir
              mountPath: /tmp
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
        {{- if .Values.credentials.extraSecretRef }}
          envFrom:
          - secretRef:
              name: {{ .Values.credentials.extraSecretRef }}
        {{- end }}
          env:
            - name: VELERO_SCRATCH_DIR
              value: /scratch
            - name: VELERO_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: LD_LIBRARY_PATH
              value: /plugins
          {{- if .Values.credentials.useSecret }}
            - name: AWS_SHARED_CREDENTIALS_FILE
              value: /credentials/cloud
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: /credentials/cloud
            - name: AZURE_CREDENTIALS_FILE
              value: /credentials/cloud
            - name: ALIBABA_CLOUD_CREDENTIALS_FILE
              value: /credentials/cloud
          {{- end }}
          {{- with .Values.configuration.extraEnvVars }}
          {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.credentials.extraEnvVars }}
          {{- range $key, $value := . }}
            - name: {{ default "none" $key }}
              valueFrom:
                secretKeyRef:
                  name: {{ include "velero.secretName" $ }}
                  key: {{ default "none" $value }}
          {{- end }}
          {{- end }}
          {{- if .Values.lifecycle }}
          lifecycle: {{ toYaml .Values.lifecycle | nindent 12 }}
          {{- end }}
      dnsPolicy: {{ .Values.dnsPolicy }}
{{- if .Values.initContainers }}
      initContainers:
        {{- if eq (typeOf .Values.initContainers) "string" }}
        {{- tpl .Values.initContainers . | nindent 8 }}
        {{- else }}
        {{- toYaml .Values.initContainers | nindent 8 }}
        {{- end }}
{{- end }}
      volumes:
        {{- if .Values.credentials.useSecret }}
        - name: cloud-credentials
          secret:
            secretName: {{ include "velero.secretName" . }}
        {{- end }}
        - name: plugins
          emptyDir: {}
        - name: scratch
          emptyDir: {}
        {{- if .Values.containerSecurityContext.readOnlyRootFilesystem }}
        - name: tmpdir
          emptyDir: {}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
        {{- end }}
    {{- with $podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
