{{- if .Values.aggregator.enabled -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "fluentd.fullname" . }}
  {{- if .Values.aggregator.annotations }}
  annotations: {{- include "fluentd.tplValue" (dict "value" .Values.aggregator.annotations "context" $) | nindent 4 }}
  {{- end }}
  labels: {{- include "fluentd.labels" . | nindent 4 }}
    app.kubernetes.io/component: aggregator
    ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
    app: aggregator
spec:
  selector:
    matchLabels: {{- include "fluentd.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: aggregator
  serviceName: {{ include "fluentd.fullname" . }}-headless
  replicas: {{ .Values.aggregator.replicaCount }}
  updateStrategy: {{- toYaml .Values.aggregator.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels: {{- include "fluentd.labels" . | nindent 8 }}
        app.kubernetes.io/component: aggregator
        app: aggregator
        {{- if .Values.aggregator.podLabels }}
        {{- include "fluentd.tplValue" (dict "value" .Values.aggregator.podLabels "context" $) | nindent 8 }}
        {{- end }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/aggregator-configmap.yaml") . | sha256sum }}
        {{- if .Values.aggregator.podAnnotations }}
        {{- include "fluentd.tplValue" (dict "value" .Values.aggregator.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "fluentd.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "fluentd.aggregator.serviceAccountName" . }}
      {{- if .Values.aggregator.securityContext.enabled }}
      securityContext: {{- omit .Values.aggregator.securityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.affinity }}
      affinity: {{- include "fluentd.tplValue" (dict "value" .Values.aggregator.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.nodeSelector }}
      nodeSelector: {{- include "fluentd.tplValue" (dict "value" .Values.aggregator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.tolerations }}
      tolerations: {{- include "fluentd.tplValue" (dict "value" .Values.aggregator.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.initContainers }}
      initContainers:
      {{- include "fluentd.tplValue" ( dict "value" .Values.aggregator.initContainers "context" $ ) | nindent 8 }}
      {{- end }}
      containers:
        - name: fluentd
          image: {{ include "fluentd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            - name: FLUENTD_CONF
              value: {{ .Values.aggregator.configFile }}
            - name: FLUENTD_OPT
              value: {{ .Values.aggregator.extraArgs | quote }}
            {{- if .Values.aggregator.extraEnv }}
            {{- toYaml .Values.aggregator.extraEnv | nindent 12 }}
            {{- end }}
          ports:
            {{- if .Values.aggregator.port }}
            - name: tcp
              containerPort: {{ .Values.aggregator.port }}
              protocol: TCP
            {{- end }}
            {{- if .Values.aggregator.containerPorts }}
            {{- toYaml .Values.aggregator.containerPorts | nindent 12 }}
            {{- end }}
            {{- if .Values.metrics.enabled }}
            - name: tcp-metrics
              containerPort: {{ .Values.metrics.service.port }}
              protocol: TCP
            {{- end }}
          {{- if .Values.aggregator.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /fluentd.healthcheck?json=%7B%22ping%22%3A+%22pong%22%7D
              port: http
            initialDelaySeconds: {{ .Values.aggregator.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.aggregator.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.aggregator.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.aggregator.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.aggregator.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.aggregator.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /fluentd.healthcheck?json=%7B%22ping%22%3A+%22pong%22%7D
              port: http
            initialDelaySeconds: {{ .Values.aggregator.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.aggregator.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.aggregator.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.aggregator.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.aggregator.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.aggregator.resources }}
          resources: {{- toYaml .Values.aggregator.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.aggregator.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.aggregator.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{ end }}
          volumeMounts:
            - name: fluentd-config
              mountPath: /opt/bitnami/fluentd/conf
            {{- if .Values.aggregator.persistence.enabled }}
            - name: {{ include "fluentd.fullname" . }}-buffer
              mountPath: /opt/bitnami/fluentd/logs/buffers
            {{- else }}
            - name: buffer
              mountPath: /opt/bitnami/fluentd/logs/buffers
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: certs
              mountPath: /opt/bitnami/fluentd/certs
            {{- end }}
            {{- if .Values.aggregator.extraVolumeMounts }}
            {{- toYaml .Values.aggregator.extraVolumeMounts | nindent 12 }}
            {{- end }}
        {{- if .Values.aggregator.sidecars }}
        {{- include "fluentd.tplValue" ( dict "value" .Values.aggregator.sidecars "context" $ ) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.aggregator.extraVolumes }}
          {{- toYaml .Values.aggregator.extraVolumes | nindent 8 }}
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: certs
          secret:
            secretName: {{ template "fluentd.tls.secretName" . }}
            items:
              - key: tls.crt
                path: tls.crt
              - key: tls.key
                path: tls.key
        {{- end }}
        - name: fluentd-config
          configMap:
            name: {{ template "fluentd.aggregator.configMap" . }}
        {{- if not .Values.aggregator.persistence.enabled }}
        - name: buffer
          emptyDir: {}
        {{- end }}

  {{- if .Values.aggregator.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: {{ include "fluentd.fullname" . }}-buffer
      spec:
        accessModes: [{{ .Values.aggregator.persistence.accessMode }}]
        storageClassName: {{ .Values.aggregator.persistence.storageClass }}
        resources:
          requests:
            storage: {{ .Values.aggregator.persistence.size }}
  {{- end }}
{{- end -}}
