{{- if .Values.metrics.enabled}}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.exporter" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: exporter
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.exporter.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: exporter
  {{- if .Values.exporter.updateStrategy }}
  strategy: {{- toYaml .Values.exporter.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.matchLabels" . | nindent 8 }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.exporter.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.exporter.podLabels "context" $) | nindent 8 }}
        {{- end }}
        app.kubernetes.io/component: exporter
      annotations:
        checksum/configmap-env: {{ include (print $.Template.BasePath "/exporter/exporter-cm-envvars.yaml") . | sha256sum }}
        checksum/secret-core: {{ include (print $.Template.BasePath "/core/core-secret.yaml") . | sha256sum }}
        {{- if and .Values.internalTLS.enabled (not .Values.core.tls.existingSecret) }}
        checksum/tls: {{ include (print $.Template.BasePath "/internal/internal-crt-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.exporter.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.exporter.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.exporter.automountServiceAccountToken }}
      {{- if .Values.exporter.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.exporter.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.exporter.podAffinityPreset "component" "exporter" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.exporter.podAntiAffinityPreset "component" "exporter" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.exporter.nodeAffinityPreset.type "key" .Values.exporter.nodeAffinityPreset.key "values" .Values.exporter.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.exporter.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.exporter.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.exporter.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.exporter.priorityClassName }}
      priorityClassName: {{ .Values.exporter.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.exporter.schedulerName }}
      schedulerName: {{ .Values.exporter.schedulerName }}
      {{- end }}
      {{- if .Values.exporter.serviceAccountName }}
      serviceAccountName: {{ .Values.exporter.serviceAccountName }}
      {{- end }}
      {{- if .Values.exporter.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.exporter.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.exporter.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: exporter
          image: {{ include "harbor.exporter.image" . }}
          imagePullPolicy: {{ .Values.exporter.image.pullPolicy | quote }}
          {{- if .Values.exporter.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.exporter.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.exporter.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.exporter.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.exporter.image.debug | quote }}
            - name: HARBOR_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "harbor.core" . }}-envvars
                  key: POSTGRESQL_PASSWORD
            - name: HARBOR_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "harbor.core" . }}-envvars
                  key: HARBOR_ADMIN_PASSWORD
            {{- if .Values.exporter.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.exporter.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: "{{ include "harbor.exporter" . }}-envvars"
            - secretRef:
                name: "{{ include "harbor.exporter" . }}-envvars"
          ports:
            - containerPort: {{ .Values.exporter.containerPorts.metrics }}
              name: metrics
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.exporter.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.exporter.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: {{ .Values.metrics.path | quote }}
              port: metrics
              # Metrics are exposed only though HTTP https://github.com/goharbor/harbor/issues/16252
              scheme: HTTP
          {{- else if .Values.exporter.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.exporter.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.exporter.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: {{ .Values.metrics.path | quote }}
              port: metrics
              scheme: HTTP
          {{- else if .Values.exporter.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.exporter.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.exporter.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: {{ .Values.metrics.path | quote }}
              port: metrics
              scheme: HTTP
          {{- else if .Values.exporter.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.exporter.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.exporter.resources }}
          resources: {{- toYaml .Values.exporter.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/exporter
            {{- end }}
            {{- if .Values.internalTLS.caBundleSecret }}
            {{- include "harbor.caBundleVolumeMount" . | nindent 12 }}
            {{- end }}
            {{- if .Values.exporter.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.exporter.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.exporter.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.exporter.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.internalTLS.enabled }}
        - name: internal-tls-certs
          secret:
            secretName: {{ include "harbor.core.tls.secretName" . }}
            items:
              - key: ca.crt
                path: ca.crt
        {{- end }}
        {{- if .Values.internalTLS.caBundleSecret }}
        {{- include "harbor.caBundleVolume" . | nindent 8 }}
        {{- end }}
      {{- if .Values.exporter.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.exporter.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{ end }}
