{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.pluginBarmanCloud.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "cloudnative-pg.plugin-barman-cloud.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- /* Updating app.kubernetes.io/name because to avoid "duplicate deployment" error: https://github.com/cloudnative-pg/cloudnative-pg/blob/9c2769815ff78cbfc28e2a2818f3a04add33477c/pkg/certs/operator_deployment.go#L120 */}}
  {{- $appNameLabels := dict "app.kubernetes.io/name" "plugin-barman-cloud" }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.commonLabels) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cloudnative-pg
    app.kubernetes.io/component: plugin-barman-cloud
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.pluginBarmanCloud.autoscaling.hpa.enabled }}
  replicas: {{ .Values.pluginBarmanCloud.replicaCount }}
  {{- end }}
  {{- if .Values.pluginBarmanCloud.updateStrategy }}
  strategy: {{- toYaml .Values.pluginBarmanCloud.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $appNameLabels .Values.pluginBarmanCloud.podLabels .Values.commonLabels) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: cloudnative-pg
      app.kubernetes.io/component: plugin-barman-cloud
  template:
    metadata:
      {{- if .Values.pluginBarmanCloud.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: cloudnative-pg
        app.kubernetes.io/component: plugin-barman-cloud
    spec:
      serviceAccountName: {{ template "cloudnative-pg.plugin-barman-cloud.serviceAccountName" . }}
      {{- include "cloudnative-pg.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.pluginBarmanCloud.automountServiceAccountToken }}
      {{- if .Values.pluginBarmanCloud.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.pluginBarmanCloud.podAffinityPreset "component" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.pluginBarmanCloud.podAntiAffinityPreset "component" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.pluginBarmanCloud.nodeAffinityPreset.type "key" .Values.pluginBarmanCloud.nodeAffinityPreset.key "values" .Values.pluginBarmanCloud.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.priorityClassName }}
      priorityClassName: {{ .Values.pluginBarmanCloud.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.schedulerName }}
      schedulerName: {{ .Values.pluginBarmanCloud.schedulerName | quote }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.pluginBarmanCloud.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.pluginBarmanCloud.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.pluginBarmanCloud.initContainers }}
      initContainers:
        {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: plugin-barman-cloud
          image: {{ template "cloudnative-pg.plugin-barman-cloud.image" . }}
          imagePullPolicy: {{ .Values.pluginBarmanCloud.image.pullPolicy }}
          {{- if .Values.pluginBarmanCloud.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.pluginBarmanCloud.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.pluginBarmanCloud.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.pluginBarmanCloud.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - operator
            - --server-address=:{{ .Values.pluginBarmanCloud.containerPorts.grpc }}
            - --health-probe-bind-address=:{{ .Values.pluginBarmanCloud.containerPorts.health }}
            - --metrics-bind-address=:{{ .Values.pluginBarmanCloud.containerPorts.metrics }}
            - --leader-elect
            - --server-cert=/certs/server/tls.crt
            - --server-key=/certs/server/tls.key
            - --client-cert=/certs/client/tls.crt
            {{- if .Values.pluginBarmanCloud.image.debug }}
            - --log-level=trace
            {{- end }}
            {{- if .Values.pluginBarmanCloud.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          env:
            - name: SIDECAR_IMAGE
              value: {{ include "cloudnative-pg.plugin-barman-cloud.sidecar.image" . | quote }}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            {{- if .Values.pluginBarmanCloud.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.pluginBarmanCloud.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.pluginBarmanCloud.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.pluginBarmanCloud.resources }}
          resources: {{- toYaml .Values.pluginBarmanCloud.resources | nindent 12 }}
          {{- else if ne .Values.pluginBarmanCloud.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.pluginBarmanCloud.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http-metrics
              containerPort: {{ .Values.pluginBarmanCloud.containerPorts.metrics }}
            - name: http-health
              containerPort: {{ .Values.pluginBarmanCloud.containerPorts.health }}
            - name: grpc
              containerPort: {{ .Values.pluginBarmanCloud.containerPorts.grpc }}
          {{- if .Values.pluginBarmanCloud.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.pluginBarmanCloud.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.pluginBarmanCloud.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-health
          {{- end }}
          {{- if .Values.pluginBarmanCloud.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.pluginBarmanCloud.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.pluginBarmanCloud.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /readyz
              port: http-health
          {{- end }}
          {{- if .Values.pluginBarmanCloud.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.pluginBarmanCloud.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.pluginBarmanCloud.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /readyz
              port: http-health
          {{- end }}
          {{- if .Values.pluginBarmanCloud.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: client-certificate
              mountPath: /certs/client
            - name: server-certificate
              mountPath: /certs/server
            {{- if .Values.pluginBarmanCloud.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.pluginBarmanCloud.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.pluginBarmanCloud.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: client-certificate
          secret:
            defaultMode: 420
            secretName: {{ include "cloudnative-pg.plugin-barman-cloud.tlsClientSecretName" . }}
        - name: server-certificate
          secret:
            defaultMode: 420
            secretName: {{ include "cloudnative-pg.plugin-barman-cloud.tlsServerSecretName" . }}
        {{- if .Values.pluginBarmanCloud.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.pluginBarmanCloud.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}