apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
  name: {{ template "filebeat.fullname" . }}
  labels:
    app: {{ template "filebeat.name" . }}
    chart: {{ template "filebeat.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "filebeat.name" . }}
      release: {{ .Release.Name }}
  minReadySeconds: 10
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: {{ template "filebeat.name" . }}
        release: {{ .Release.Name }}
      annotations:
        checksum/secret: {{ toYaml .Values.config | sha256sum }}
{{- if .Values.annotations }}
{{ toYaml .Values.annotations | indent 8 }}
{{- end }}
    spec:
    {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range $sec := .Values.image.pullSecrets }}
        - name: {{ $sec | quote }}
      {{- end }}
    {{- end }}
{{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
{{- end }}
      initContainers:
{{- if .Values.indexTemplateLoad }}
      - name: "load-es-template"
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
        - /bin/bash
        - -c
        - filebeat setup --template
          -E output.logstash.enabled=false
          -E output.file.enabled=false
          -E output.elasticsearch.hosts=[{{- range $index, $host := .Values.indexTemplateLoad }}{{ if $index }}, {{ end }}{{ $host | quote }}{{- end }}]
{{- end }}
{{- if .Values.extraInitContainers }}
{{ toYaml .Values.extraInitContainers | indent 6 }}
{{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.command }}
        command:
{{ toYaml .Values.command | indent 8 }}
{{- end }}
        args:
{{- if .Values.args }}
{{ toYaml .Values.args | indent 8 }}
{{- else }}
        - "-e"
{{- if .Values.plugins }}
        - "--plugin"
        - {{ .Values.plugins | join "," | quote }}
{{- end }}
{{- end }}
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
{{- if .Values.extraVars }}
{{ toYaml .Values.extraVars | indent 8 }}
{{- end }}
        {{- if index .Values.config "http.enabled" }}
        ports:
          - containerPort: {{ index .Values.config "http.port" }}
        {{- end }}
        securityContext:
          runAsUser: 0
{{- if .Values.resources }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
{{- end }}
        volumeMounts:
        - name: filebeat-config
          mountPath: /usr/share/filebeat/filebeat.yml
          readOnly: true
          subPath: filebeat.yml
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: varlog
          mountPath: /var/log
          readOnly: true
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
{{- if .Values.extraVolumeMounts }}
{{ toYaml .Values.extraVolumeMounts | indent 8 }}
{{- end }}
{{- if .Values.monitoring.enabled }}
      - name: {{ template "filebeat.fullname" . }}-prometheus-exporter
        image: "{{ .Values.monitoring.image.repository }}:{{ .Values.monitoring.image.tag }}"
        imagePullPolicy: {{ .Values.monitoring.image.pullPolicy }}
        args:
{{- if .Values.monitoring.args }}
{{ toYaml .Values.monitoring.args | indent 8 }}
{{- end }}
{{- if .Values.monitoring.resources }}
        resources:
{{ toYaml .Values.monitoring.resources | indent 10 }}
{{- end }}
{{- end }}
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: filebeat-config
        secret:
          secretName: {{ template "filebeat.fullname" . }}
      - name: data
        hostPath:
          path: /var/lib/filebeat
          type: DirectoryOrCreate
{{- if .Values.extraVolumes }}
{{ toYaml .Values.extraVolumes | indent 6 }}
{{- end }}
      terminationGracePeriodSeconds: 60
      serviceAccountName: {{ template "filebeat.serviceAccountName" . }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
