# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "elastabot.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "elastabot.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "elastabot.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}} # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "elastabot.fullname" . }}-config
  labels:
    app: {{ template "elastabot.name" . }}
    chart: {{ template "elastabot.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
data:
  elastabot.json: |-
    {
      "elasticsearch": {
        "host": "{{ .Values.elasticsearch.host }}",
        "port": {{ .Values.elasticsearch.port }},
        "sslEnabled": {{ .Values.elasticsearch.sslEnabled }},
        "sslStrictEnabled": {{ .Values.elasticsearch.sslStrictEnabled }},
        "timeoutSeconds": {{ .Values.elasticsearch.timeoutSeconds }},
        "urlPrefix":""
      },
      "elastalert": {
        "index": "{{ .Values.elastalert.index }}",
        "silenceMinutes": {{ .Values.elastalert.defaultSilenceMinutes }},
        "recentMinutes": {{ .Values.elastalert.recentAlertMinutes }}
      },
      "smtp": {
        "host": "{{ .Values.smtp.host }}",
        "port": {{ .Values.smtp.port }},
        "secure": {{ .Values.smtp.secure }},
        "starttls": {{ .Values.smtp.starttls }},
        "timeoutSeconds": {{ .Values.smtp.timeoutSeconds }},
        "to": "{{ .Values.smtp.to }}",
        "from": "{{ .Values.smtp.from }}",
        "subjectPrefix": "{{ .Values.smtp.subjectPrefix }}",
        "debug": {{ .Values.smtp.debug }}
      },
      "commandPrefix": "{{ .Values.commandPrefix }}",
      "triageTarget": "{{ .Values.triageTarget }}",
      "searchEnabled": "{{ .Values.searchEnabled }}"
    } # deployment.yaml
{{ if .Values.elasticsearch.host }}
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "elastabot.fullname" . }}
  labels:
    app: {{ template "elastabot.name" . }}
    chart: {{ template "elastabot.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "elastabot.name" . }}
      release: {{ .Release.Name }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: {{ template "elastabot.name" . }}
        release: {{ .Release.Name }}
    spec:
{{ if .Values.extraSpec }}
      {{- toYaml .Values.extraSpec | indent 6 }}
{{ end }}
      volumes:
        - name: config
          configMap:
            name: {{ template "elastabot.fullname" . }}-config
            items:
            - key: elastabot.json
              path: elastabot.json
      containers:
        - name: elastabot
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
{{ if .Values.extraContainer }}
          {{- .Values.extraContainer | indent 10 }}
{{ end }}
          env:
{{ if .Values.extraEnvs }}
            {{- .Values.extraEnvs | indent 12 }}
{{ end }}
            - name: ELASTICSEARCH_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "elastabot.fullname" . }}-secrets
                  key: elasticsearchUsername
            - name: ELASTICSEARCH_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "elastabot.fullname" . }}-secrets
                  key: elasticsearchPassword
            - name: SLACK_BOT_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ template "elastabot.fullname" . }}-secrets
                  key: slackBotToken
            - name: SMTP_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "elastabot.fullname" . }}-secrets
                  key: smtpUsername
            - name: SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "elastabot.fullname" . }}-secrets
                  key: smtpPassword
          volumeMounts:
            - name: config
              mountPath: /opt/elastabot/elastabot.json
              subPath: elastabot.json
          resources:
            {{- toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{ end }}
# Docker image repository, tag/version and related settings
image:
  repository: jertel/elastabot
  tag: 1.1.0
  pullPolicy: IfNotPresent

# Elasticsearch settings, required.
elasticsearch:
  host: ""
  port: 9200

  # Elasticsearch connection timeout, in seconds
  timeoutSeconds: 10

  # Set sslEnabled to True to use SSL/TLS to connect to Elasticsearch
  sslEnabled: False

  # Set sslStrictEnabled to True to validate SSL/TLS certificate authenticity
  sslStrictEnabled: False

# Elastalert settings. Elastalert chart information is available here:
# https://github.com/kubernetes/charts/tree/master/stable/elastalert
elastalert:
  # Index prefix for elastalert
  index: "elastalert"

  # How far back in time to search for recently-fired alerts, in Elasticsearch
  recentAlertMinutes: 4320

  # Default silence duration
  defaultSilenceMinutes: 240

# Emails can be sent when a user acks or triages an issue.
smtp:
  host: ""
  port: 25

  # Set secure to True to use SSL/TLS to connect to the SMTP host
  secure: False

  # Set starttls to True to switch to TLS after connecting to the SMTP host (not needed when secure: True)
  starttls: False

  # SMTP host communication timeout
  timeoutSeconds: 4

  # Where triaged SMTP emails should be sent
  to: ""

  # Address that is sending the SMTP emails
  from: ""

  # Optional prefix to prepend to each email's subject
  subjectPrefix: ""

  # Set debug to True to troubleshoot SMTP connectivity problems
  debug: False

# Typically users will interact with this bot via an exclamation point, however
# custom prefixes, such as "!prod " could be used if multiple bots will be listening to a
# single channel, but interacting with different Elasticsearch clusters
commandPrefix: "!"

# Currently only smtp is supported for initiating a triage workflow.
triageTarget: smtp

# Allow all Slack users to search the Elasticsearch cluster for any data.
# CAUTION: Disable this when this bot is exposed to a public Slack community and the cluster contains sensitive data.
searchEnabled: true

resources: {}
nodeSelector: {}
tolerations: []
affinity: {}
