{{- if .Values.metrics.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "airflow.fullname" . }}-exporter
  labels:
    app.kubernetes.io/name: {{ include "airflow.name" . }}
    helm.sh/chart: {{ include "airflow.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/component: metrics
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "airflow.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      annotations: {{- toYaml .Values.metrics.podAnnotations | nindent 8 }}
      labels:
        app.kubernetes.io/name: {{ include "airflow.name" . }}
        app.kubernetes.io/component: metrics
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
    {{- include "airflow.imagePullSecrets" . | nindent 6 }}
      containers:
        - image: {{ template "airflow.metrics.image" . }}
          name: airflow-exporter
          env:
            - name: AIRFLOW_PROMETHEUS_DATABASE_BACKEND
              value: "postgres"
            - name: AIRFLOW_PROMETHEUS_DATABASE_HOST
              {{- if .Values.postgresql.enabled }}
              value: {{ template "airflow.postgresql.fullname" . }}
              {{- else }}
              value: {{ .Values.externalDatabase.host | quote }}
              {{- end }}
            - name: AIRFLOW_PROMETHEUS_DATABASE_PORT
              {{- if .Values.postgresql.enabled }}
              value: "5432"
              {{- else }}
              value: {{ .Values.externalDatabase.port | quote }}
              {{- end }}
            - name: AIRFLOW_PROMETHEUS_DATABASE_USER
              {{- if .Values.postgresql.enabled }}
              value: {{ .Values.postgresql.postgresqlUsername }}
              {{- else }}
              value: {{ .Values.externalDatabase.user | quote }}
              {{- end }}
            - name: AIRFLOW_PROMETHEUS_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "airflow.postgresql.secretName" . }}
                  key: postgresql-password
            - name: AIRFLOW_PROMETHEUS_DATABASE_NAME
              {{- if .Values.postgresql.enabled }}
              value: {{ .Values.postgresql.postgresqlDatabase }}
              {{- else }}
              value: {{ .Values.externalDatabase.database | quote }}
              {{- end }}
          ports:
            - name: metrics
              containerPort: 9112
          {{- if .Values.metrics.resources}}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
{{- end }}
