apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "prometheus-rabbitmq-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-rabbitmq-exporter.name" . }}
    chart: {{ template "prometheus-rabbitmq-exporter.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "prometheus-rabbitmq-exporter.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "prometheus-rabbitmq-exporter.name" . }}
        release: {{ .Release.Name }}
      annotations:
{{ toYaml .Values.annotations | indent 8 }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: RABBIT_URL
              value: "{{ .Values.rabbitmq.url }}"
            - name: RABBIT_USER
              value: "{{ .Values.rabbitmq.user }}"
            {{- if .Values.rabbitmq.existingPasswordSecret }}
            - name: RABBIT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name:  "{{ .Values.rabbitmq.existingPasswordSecret }}"
                  key: password
            {{- else }}
            - name: RABBIT_PASSWORD
              value: "{{ .Values.rabbitmq.password }}"
            {{- end }}
            - name: PUBLISH_PORT
              value: "{{ .Values.service.internalPort }}"
            - name: LOG_LEVEL
              value: "{{ .Values.loglevel }}"
            - name: RABBIT_CAPABILITIES
              value: "{{ .Values.rabbitmq.capabilities }}"
            - name: INCLUDE_QUEUES
              value: "{{ .Values.rabbitmq.include_queues }}"
            - name: INCLUDE_VHOST
              value: "{{ .Values.rabbitmq.include_vhost }}"
            - name: SKIP_QUEUES
              value: "{{ .Values.rabbitmq.skip_queues }}"
            - name: SKIPVERIFY
              value: "{{ .Values.rabbitmq.skip_verify }}"
            - name: SKIP_VHOST
              value: "{{ .Values.rabbitmq.skip_vhost }}"
            - name: RABBIT_EXPORTERS
              value: "{{ .Values.rabbitmq.exporters }}"
            - name: OUTPUT_FORMAT
              value: "{{ .Values.rabbitmq.output_format }}"
            - name: RABBIT_TIMEOUT
              value: "{{ .Values.rabbitmq.timeout }}"
            - name: MAX_QUEUES
              value: "{{ .Values.rabbitmq.max_queues }}"
          ports:
            - containerPort: {{ .Values.service.internalPort }}
              name: publish
          livenessProbe:
            httpGet:
              path: /
              port: publish
          readinessProbe:
            httpGet:
              path: /
              port: publish
          resources:
{{ toYaml .Values.resources | indent 12 }}
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: ["all"]
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: 10002
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
