apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "logstash.fullname" . }}
  labels: {{- include "logstash.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "logstash.fullname" . }}-headless
  replicas: {{ .Values.replicaCount }}
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  updateStrategy:
    type: {{ .Values.updateStrategy }}
    {{- if (eq "OnDelete" .Values.updateStrategy) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: {{- include "logstash.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "logstash.labels" . | nindent 8 }}
      annotations:
        checksum/configuration: {{ include (print $.Template.BasePath "/configuration-cm.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
        {{- include "logstash.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
{{- include "logstash.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "logstash.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "logstash.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "logstash.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        runAsUser: {{ .Values.securityContext.runAsUser }}
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      containers:
        - name: logstash
          image: {{ include "logstash.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            {{- if .Values.configFileName }}
            - name: LOGSTASH_CONF_FILENAME
              value: {{ .Values.configFileName | quote }}
            {{- end }}
            - name: LOGSTASH_EXPOSE_API
              value: {{ ternary "yes" "no" .Values.enableMonitoringAPI | quote }}
            - name: LOGSTASH_API_PORT_NUMBER
              value: {{ .Values.monitoringAPIPort | quote }}
            {{- if .Values.extraEnvVars }}
            {{- include "logstash.tplValue" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.containerPorts }}
          ports: {{ toYaml .Values.containerPorts | nindent 12 }}
          {{- end }}
          {{- if .Values.livenessProbe }}
          livenessProbe: {{- toYaml .Values.livenessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe }}
          readinessProbe: {{- toYaml .Values.readinessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
          {{- if or (.Files.Glob "files/conf/*") .Values.input .Values.filter .Values.output .Values.existingConfiguration }}
            - name: configurations
              mountPath: /bitnami/logstash/config
          {{- end }}
          {{- if .Values.extraVolumeMounts }}
            {{- include "logstash.tplValue" ( dict "value" .Values.extraVolumeMounts "context" $ ) | nindent 12 }}
          {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "logstash.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          command:
            - bash
            - -c
            - |
               sleep 60
               exec logstash_exporter --logstash.endpoint=http://127.0.0.1:{{ .Values.monitoringAPIPort }} --web.listen-address=:9198
          ports:
            - name: metrics
              containerPort: 9198
              protocol: TCP
          {{- if .Values.metrics.livenessProbe }}
          livenessProbe: {{- toYaml .Values.metrics.livenessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.metrics.readinessProbe }}
          readinessProbe: {{- toYaml .Values.metrics.readinessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
        {{- end }}
      volumes:
        {{- if or (.Files.Glob "files/conf/*") .Values.input .Values.filter .Values.output .Values.existingConfiguration }}
        - name: configurations
          configMap:
            name: {{ include "logstash.configmapName" . }}
        {{- end }}
      {{- if .Values.extraVolumes }}
        {{- include "logstash.tplValue" ( dict "value" .Values.extraVolumes "context" $ ) | nindent 8 }}
      {{- end }}
