apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "fluentd.fullname" . }}
  labels:
    app: {{ template "fluentd.name" . }}
    chart: {{ template "fluentd.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "fluentd.name" . }} # has to match .spec.template.metadata.labels
      release: {{ .Release.Name }}
  serviceName: {{ include "fluentd.fullname" . }}-headless
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  updateStrategy:
{{ toYaml .Values.updateStrategy | indent 4 }}
  replicas: {{ default 1 .Values.replicas }}
  template:
    metadata:
      labels:
        app: {{ template "fluentd.name" . }}
        release: {{ .Release.Name }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
{{- if .Values.annotations }}
{{ toYaml .Values.annotations | indent 8 }}
{{- end }}
    spec:
{{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
{{- end }}
{{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
{{- end }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
{{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range $pullSecret := .Values.image.pullSecrets }}
        - name: {{ $pullSecret }}
      {{- end }}
{{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
          - name: RELEASENAME
            value: {{ .Release.Name | quote }}
          - name: OUTPUT_BUFFER_FLUSH_INTERVAL
            value: {{ .Values.output.flushInterval | quote }}
          - name: OUTPUT_BUFFER_CHUNK_LIMIT
            value: {{ .Values.output.bufferChunkLimit | quote }}
          - name: OUTPUT_BUFFER_QUEUE_LIMIT
            value: {{ .Values.output.bufferQueueLimit | quote }}
          {{- range $key, $value := .Values.env }}
          - name: {{ $key | quote }}
            value: {{ $value | quote }}
          {{- end }}
        resources:
{{ toYaml .Values.resources | indent 12 }}
        ports:
{{- range $port := .Values.service.ports }}
          - name: {{ $port.name }}
            containerPort: {{ $port.containerPort }}
            protocol: {{ $port.protocol }}
{{- end }}
          - name: http-input
            containerPort: 9880
            protocol: TCP
        livenessProbe:
          httpGet:
            # Use percent encoding for query param.
            # The value is {"log": "health check"}.
            # the endpoint itself results in a new fluentd
            # tag 'fluentd.pod-healthcheck'
            path: /fluentd.pod.healthcheck?json=%7B%22log%22%3A+%22health+check%22%7D
            port: 9880
          initialDelaySeconds: 5
          timeoutSeconds: 1
        volumeMounts:
        - name: config-volume
          mountPath: /etc/fluent/config.d
        - name: buffer
          mountPath: "/var/log/fluentd-buffers"
        - name: extra
          mountPath: {{ .Values.extraPersistence.mountPath | default "/extra" | quote }}
        - name: ca
          mountPath: "/etc/fluent/ssl"
          readOnly: true
      volumes:
        - name: config-volume
          configMap:
            name: {{ template "fluentd.fullname" . }}
        - name: ca
          configMap:
            name: {{ template "fluentd.fullname" . }}-ca
        {{- if not .Values.persistence.enabled }}
        - name: buffer
          emptyDir: {}
        {{- end }}
        {{- if not .Values.extraPersistence.enabled }}
        - name: extra
          emptyDir: {}
        {{- end }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
  {{- if or .Values.persistence.enabled .Values.extraPersistence.enabled }}
  volumeClaimTemplates:
  {{- end }}
  {{- if .Values.persistence.enabled }}
  - metadata:
      name: buffer
    spec:
      accessModes:
      - {{ .Values.persistence.accessMode | quote }}
      resources:
        requests:
          storage: {{ .Values.persistence.size }}
      {{- if .Values.persistence.storageClass }}
      {{- if (eq "-" .Values.persistence.storageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: "{{ .Values.persistence.storageClass }}"
      {{- end }}
      {{- end }}
  {{- end }}
  {{- if .Values.extraPersistence.enabled }}
  - metadata:
      name: extra
    spec:
      accessModes:
      - {{ .Values.extraPersistence.accessMode | quote }}
      resources:
        requests:
          storage: {{ .Values.extraPersistence.size }}
      {{- if .Values.extraPersistence.storageClass }}
      {{- if (eq "-" .Values.extraPersistence.storageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: "{{ .Values.extraPersistence.storageClass }}"
      {{- end }}
      {{- end }}
  {{- end }}
