{{ if .Values.elasticsearch.host }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "elastalert.fullname" . }}
  labels:
    app: {{ template "elastalert.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "elastalert.name" . }}
      release: {{ .Release.Name }}
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/config.yaml") . | sha256sum }}
        checksum/rules: {{ include (print $.Template.BasePath "/rules.yaml") . | sha256sum }}
{{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
      labels:
        name: {{ template "elastalert.fullname" . }}-elastalert
        app: {{ template "elastalert.name" . }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ include "elastalert.serviceAccountName" . }}
      containers:
      - name: elastalert
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.command }}
        command:
{{ toYaml .Values.command | indent 10 }}
{{- end }}
{{- if .Values.args }}
        args:
{{ toYaml .Values.args | indent 10 }}
{{- end }}
        volumeMounts:
          - name: config
            mountPath: '/opt/config'
          - name: rules
            mountPath: '/opt/rules'
{{- if .Values.elasticsearch.certsVolumeMounts }}
{{ toYaml .Values.elasticsearch.certsVolumeMounts | indent 10 }}
{{- end }}
{{- if .Values.extraVolumeMounts }}
{{ toYaml .Values.extraVolumeMounts | indent 10 }}
{{- end }}
        resources:
{{ toYaml .Values.resources | indent 12 }}
        env:
{{- if .Values.elasticsearch.credentialsSecret }}
{{- if .Values.elasticsearch.credentialsSecretUsernameKey }}
          - name: ES_USERNAME
            valueFrom:
              secretKeyRef:
                name: {{ .Values.elasticsearch.credentialsSecret }}
                key: {{ .Values.elasticsearch.credentialsSecretUsernameKey }}
{{- end }}
{{- if .Values.elasticsearch.credentialsSecretPasswordKey }}
          - name: ES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ .Values.elasticsearch.credentialsSecret }}
                key: {{ .Values.elasticsearch.credentialsSecretPasswordKey }}
{{- end }}
{{- end }}
{{- if .Values.optEnv }}
{{ .Values.optEnv | toYaml | indent 10}}
{{- end }}
      restartPolicy: Always
{{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
{{- end }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
      volumes:
        - name: rules
          configMap:
            name: {{ template "elastalert.fullname" . }}-rules
            items:
{{- range $key, $value := .Values.rules }}
            - key: {{ $key }}
              path: {{ $key}}.yaml
{{- end }}
        - name: config
          configMap:
            name: {{ template "elastalert.fullname" . }}-config
            items:
            - key: elastalert_config
              path: elastalert_config.yaml
{{- if .Values.elasticsearch.certsVolumes }}
{{ toYaml .Values.elasticsearch.certsVolumes | indent 8 }}
{{- end }}
{{- if .Values.extraVolumes }}
{{ toYaml .Values.extraVolumes | indent 8 }}
{{- end }}
{{- end }}
