{{- if and .Values.config.datasource.passwordSecret .Values.config.datasource.password -}}
{{ fail (printf "ERROR: only one of .Values.config.datasource.passwordSecret and .Values.config.datasource.password must be defined") }}
{{- end -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "prometheus-postgres-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-postgres-exporter.name" . }}
    chart: {{ template "prometheus-postgres-exporter.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "prometheus-postgres-exporter.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "prometheus-postgres-exporter.name" . }}
        release: {{ .Release.Name }}
        collect.logs: "plain-text"
{{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | trim | indent 8 }}
{{- end }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
{{- if .Values.annotations }}
{{ toYaml .Values.annotations | indent 8 }}
{{- end }}
    spec:
      serviceAccountName: {{ template "prometheus-postgres-exporter.serviceAccountName" . }}
{{- if .Values.initContainers }}
      initContainers:
{{ toYaml .Values.initContainers | indent 8 }}
{{- end }}
    {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          args:
          - "--extend.query-path=/etc/config.yaml"
          {{- if .Values.config.disableDefaultMetrics }}
          - "--disable-default-metrics"
          {{- end }}
          {{- if .Values.config.disableSettingsMetrics }}
          - "--disable-settings-metrics"
          {{- end }}
          {{- if .Values.config.autoDiscoverDatabases }}
          - "--auto-discover-databases"
          {{- if .Values.config.excludeDatabases }}
          - "--exclude-databases"
          - {{ .Values.config.excludeDatabases | join "," }}
          {{- end }}
          {{- if .Values.config.includeDatabases }}
          - "--include-databases"
          - {{ .Values.config.includeDatabases | join "," }}
          {{- end }}
          {{- end }}
          {{- if .Values.config.constantLabels }}
          {{ $firstLabel := true -}}
          - "--constantLabels={{- range $k, $v := .Values.config.constantLabels }}{{- if not $firstLabel -}},{{ end -}}{{ $firstLabel = false -}}{{ $k }}={{ $v }}{{- end }}"
          {{- end }}
          {{- if .Values.config.logLevel }}
          - "--log.level={{ .Values.config.logLevel }}"
          {{- end}}
          env:
          {{- if .Values.config.datasourceSecret }}
          - name: DATA_SOURCE_NAME
            valueFrom:
              secretKeyRef:
                name: {{ .Values.config.datasourceSecret.name }}
                key: {{ .Values.config.datasourceSecret.key }}
          {{- else }}
          - name: DATA_SOURCE_URI
            value: {{ template "prometheus-postgres-exporter.data_source_uri" . }}
          - name: DATA_SOURCE_USER
            value: {{ .Values.config.datasource.user }}
          - name: DATA_SOURCE_PASS
            valueFrom:
              secretKeyRef:
          {{- if .Values.config.datasource.passwordSecret }}
                name: {{ .Values.config.datasource.passwordSecret.name }}
                key: {{ .Values.config.datasource.passwordSecret.key }}
          {{- else }}
                name: {{ template "prometheus-postgres-exporter.fullname" . }}
                key: data_source_password
          {{- end }}
          {{- end }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.targetPort }}
              protocol: TCP
          livenessProbe:
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            httpGet:
              path: /
              port: http
          readinessProbe:
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            httpGet:
              path: /
              port: http
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
            - name: queries
              mountPath: /etc/config.yaml
              subPath: config.yaml
{{- with .Values.extraVolumeMounts }}
{{ toYaml . | indent 12 }}
{{- end }}
{{- with .Values.extraContainers }}
{{ toYaml . | indent 8 }}
{{- end }}
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
     {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
      - configMap:
          defaultMode: 420
          name: {{ template "prometheus-postgres-exporter.fullname" . }}
        name: queries
{{- with .Values.extraVolumes }}
{{ toYaml . | indent 6 }}
{{- end }}
