# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "dmarc2logstash.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 "dmarc2logstash.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 "dmarc2logstash.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "dmarc2logstash.fullname" . }}-config
  labels:
    app: {{ template "dmarc2logstash.name" . }}
    chart: {{ template "dmarc2logstash.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
data:
  filebeat.yml: |
    filebeat.registry_file: /tmp/filebeat_registry
    filebeat.prospectors:
    - type: log
      enabled: true
      paths:
        - "/app/log/forward/*.log"
      json.keys_under_root: true
      json.add_error_key: true
      fields_under_root: true
      fields:
        source_type: {{ .Values.filebeat.sourceType }}
    output.logstash:
      hosts: 
        - {{ .Values.filebeat.logstash.host }}:{{ .Values.filebeat.logstash.port }}
      index: {{ .Values.filebeat.logstash.index }}
      timeout: {{ .Values.filebeat.logstash.timeout }}
{{ if .Values.filebeat.logstash.extraOutput }}
      {{- .Values.filebeat.logstash.extraOutput | indent 8 }}
{{ end }}
    logging.level: info
 # deployment.yaml
{{ if .Values.filebeat.logstash.host }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "dmarc2logstash.fullname" . }}
  labels:
    app: {{ template "dmarc2logstash.name" . }}
    chart: {{ template "dmarc2logstash.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "dmarc2logstash.name" . }}
      release: {{ .Release.Name }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: {{ template "dmarc2logstash.name" . }}
        release: {{ .Release.Name }}
    spec:
{{ if .Values.extraSpec }}
      {{- toYaml .Values.extraSpec | indent 6 }}
{{ end }}
      volumes:
        - name: shared-log
          emptyDir: {}
        - name: filebeat-config
          configMap:
            name: {{ template "dmarc2logstash.fullname" . }}-config
            items:
            - key: filebeat.yml
              path: filebeat.yml
      containers:
        - name: dmarc2logstash
          image: "{{ .Values.dmarc2logstash.image.repository }}:{{ .Values.dmarc2logstash.image.tag }}"
          imagePullPolicy: {{ .Values.dmarc2logstash.image.pullPolicy }}
{{ if .Values.dmarc2logstash.extraContainer }}
          {{- .Values.dmarc2logstash.extraContainer | indent 10 }}
{{ end }}
          env:
{{ if .Values.dmarc2logstash.extraEnvs }}
            {{- .Values.dmarc2logstash.extraEnvs | indent 12 }}
{{ end }}
            - name: JSON_OUTPUT_FILE
              value: /opt/dmarc2logstash/output/dmarc.log
            - name: POP3_SERVER
              valueFrom:
                secretKeyRef:
                  name: {{ template "dmarc2logstash.name" . }}-secrets
                  key: pop3_server
            - name: POP3_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ template "dmarc2logstash.name" . }}-secrets
                  key: pop3_username
            - name: POP3_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "dmarc2logstash.name" . }}-secrets
                  key: pop3_password
            - name: DELETE_MESSAGES
              value: "{{ .Values.dmarc2logstash.deleteMessages }}"
          volumeMounts:
            - name: shared-log
              mountPath: /opt/dmarc2logstash/output
          resources:
            {{- toYaml .Values.dmarc2logstash.resources | indent 12 }}
    {{- with .Values.dmarc2logstash.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.dmarc2logstash.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.dmarc2logstash.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
        - name: filebeat
          image: "{{ .Values.filebeat.image.repository }}:{{ .Values.filebeat.image.tag }}"
          imagePullPolicy: {{ .Values.filebeat.image.pullPolicy }}
{{ if .Values.filebeat.extraContainer }}
          {{- .Values.filebeat.extraContainer | indent 10 }}
{{ end }}
          volumeMounts:
            - name: filebeat-config
              mountPath: /usr/share/filebeat/filebeat.yml
              subPath: filebeat.yml
            - name: shared-log
              mountPath: /app/log/forward
          resources:
            {{- toYaml .Values.filebeat.resources | indent 12 }}
{{ end }}
dmarc2logstash:
  image:
    repository: jertel/dmarc2logstash
    tag: 1.0.3
    pullPolicy: IfNotPresent
  deleteMessages: 1
  resources: {}
  nodeSelector: {}
  tolerations: []
  affinity: {}

filebeat:
  image:
    repository: docker.elastic.co/beats/filebeat
    tag: 6.6.0
    pullPolicy: IfNotPresent
  logstash:
    host: ""
    port: 5000
    sourceType: json-logs
    index: dmarc
    timeout: 15
  resources: {}
