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

{{- if .Values.metrics.enabled }}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.exporter" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.exporter.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    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 }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.exporter.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | 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.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        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" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.exporter.podAntiAffinityPreset "component" "exporter" "customLabels" $podLabels "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 }}
      serviceAccountName: {{ include "harbor.exporter.serviceAccountName" .}}
      {{- if .Values.exporter.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.exporter.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.exporter.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.exporter.initContainers "context" $) | nindent 8 }}
        {{- end }}
        - name: certificate-volume
          image: {{ include "harbor.exporter.image" . }}
          imagePullPolicy: {{ .Values.exporter.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - cp -r /etc/ssl/certs/* /certs
          {{- if .Values.exporter.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.exporter.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.certificateVolume.resources }}
          resources: {{- toYaml .Values.certificateVolume.resources | nindent 12 }}
          {{- else if ne .Values.certificateVolume.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.certificateVolume.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /certs
              subPath: etc-ssl-certs
      containers:
        - name: exporter
          image: {{ include "harbor.exporter.image" . }}
          imagePullPolicy: {{ .Values.exporter.image.pullPolicy | quote }}
          {{- if .Values.exporter.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.exporter.containerSecurityContext "context" $) | 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:
                {{- if and .Values.externalDatabase.existingSecret (not .Values.postgresql.enabled) }}
                  name: {{ .Values.externalDatabase.existingSecret }}
                  key: {{ .Values.externalDatabase.existingSecretPasswordKey }}
                {{- else }}
                  name: {{ template "harbor.core.envvars.secretName" . }}
                  key: POSTGRESQL_PASSWORD
                {{- end }}
            {{- if .Values.existingSecret }}
            - name: HARBOR_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ tpl .Values.existingSecret . }}
                  key: {{ template "harbor.secret.adminPasswordKey" . }}
            {{- end }}
            {{- if .Values.exporter.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.exporter.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ printf "%s-envvars" (include "harbor.exporter" .) }}
            - secretRef:
                name: {{ printf "%s-envvars" (include "harbor.exporter" .) }}
            {{- if .Values.exporter.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.exporter.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.exporter.containerPorts.metrics }}
              name: metrics
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.exporter.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.customStartupProbe "context" $) | nindent 12 }}
          {{- else 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
          {{- end }}
          {{- if .Values.exporter.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.exporter.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.exporter.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- if .Values.exporter.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.exporter.customReadinessProbe "context" $) | nindent 12 }}
          {{- else 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
          {{- 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 }}
          {{- else if ne .Values.exporter.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.exporter.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /etc/ssl/certs
              subPath: etc-ssl-certs
            {{- 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:
        - name: empty-dir
          emptyDir: {}
        {{- 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 }}
