{{- if .Values.metrics.kafka.enabled }}
{{- $replicaCount := int .Values.replicaCount -}}
{{- $releaseNamespace := .Release.Namespace -}}
{{- $clusterDomain := .Values.clusterDomain -}}
{{- $fullname := include "kafka.fullname" . -}}
{{- $servicePort := int .Values.service.port -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "kafka.fullname" . }}-exporter
  labels: {{- include "kafka.labels" . | nindent 4 }}
    app.kubernetes.io/component: metrics
    {{- if .Values.commonLabels }}
    {{- include "kafka.tplValue" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "kafka.tplValue" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  selector:
    matchLabels: {{- include "kafka.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: metrics
  template:
    metadata:
      labels: {{- include "kafka.labels" . | nindent 8 }}
        app.kubernetes.io/component: metrics
    spec:
{{- include "kafka.imagePullSecrets" . | indent 6 }}
      containers:
        - name: kafka-exporter
          image: {{ include  "kafka.metrics.kafka.image" . }}
          imagePullPolicy: {{ .Values.metrics.kafka.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              read -r -a sasl_passwords <<< "$(tr ',;' ' ' <<< "${SASL_USER_PASSWORD}")"
              kafka_exporter \
              {{- range $i, $e := until $replicaCount }}
              --kafka.server={{ $fullname }}-{{ $i }}.{{ $fullname }}-headless.{{ $releaseNamespace }}.svc.{{ $clusterDomain }}:{{ $servicePort }} \
              {{- end }}
              {{- if (include "kafka.client.saslAuthentication" .) }}
              --sasl.enabled \
              --sasl.username="$SASL_USERNAME" \
              --sasl.password="${sasl_passwords[0]}" \
              {{- end }}
              {{- if (include "kafka.tlsEncryption" .) }}
              --tls.enabled \
              {{- if .Values.metrics.kafka.certificatesSecret }}
              --tls.ca-file="/opt/bitnami/kafka-exporter/certs/ca-file" \
              --tls.cert-file="/opt/bitnami/kafka-exporter/certs/cert-file" \
              --tls.key-file="/opt/bitnami/kafka-exporter/certs/key-file" \
              {{- end }}
              {{- end }}
              {{- range $key, $value := .Values.metrics.kafka.extraFlags }}
              --{{ $key }}{{ if $value }}={{ $value }}{{ end }} \
              {{- end }}
              --web.listen-address=:9308
          {{- if (include "kafka.client.saslAuthentication" .) }}
          env:
            - name: SASL_USERNAME
              value: {{ index .Values.auth.jaas.clientUsers 0 | quote }}
            - name: SASL_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kafka.jaasSecretName" . }}
                  key: client-passwords
          {{- end }}
          ports:
            - name: metrics
              containerPort: 9308
          {{- if .Values.metrics.kafka.resources }}
          resources: {{ toYaml .Values.metrics.kafka.resources | nindent 12 }}
          {{- end }}
      {{- if and (include "kafka.tlsEncryption" .) .Values.metrics.kafka.certificatesSecret }}
          volumeMounts:
            - name: kafka-exporter-certificates
              mountPath: /opt/bitnami/kafka-exporter/certs/
              readOnly: true
      volumes:
        - name: kafka-exporter-certificates
          secret:
            secretName: {{ .Values.metrics.kafka.certificatesSecret }}
            defaultMode: 0440
      {{- end }}
{{- end }}
