{{- if .Values.aggregator.enabled -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: aggregator
    ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
    app: aggregator
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.aggregator.annotations }}
    {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: aggregator
  serviceName: {{ include "common.names.fullname" . }}-headless
  podManagementPolicy: {{ .Values.aggregator.podManagementPolicy }}
  replicas: {{ .Values.aggregator.replicaCount }}
  updateStrategy: {{- toYaml .Values.aggregator.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: aggregator
        app: aggregator
        {{- if .Values.aggregator.podLabels }}
        {{- include "common.tplvalues.render" (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 "common.tplvalues.render" (dict "value" .Values.aggregator.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "fluentd.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.aggregator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "fluentd.aggregator.serviceAccountName" . }}
      {{- if .Values.aggregator.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.aggregator.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.aggregator.podAffinityPreset "component" "aggregator" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.aggregator.podAntiAffinityPreset "component" "aggregator" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.aggregator.nodeAffinityPreset.type "key" .Values.aggregator.nodeAffinityPreset.key "values" .Values.aggregator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.aggregator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.schedulerName }}
      schedulerName: {{ .Values.aggregator.schedulerName }}
      {{- end }}
      {{- if .Values.aggregator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.aggregator.priorityClassName }}
      priorityClassName: {{ .Values.aggregator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.aggregator.initContainers }}
      initContainers: {{- include "common.tplvalues.render" ( dict "value" .Values.aggregator.initContainers "context" $ ) | nindent 8 }}
      {{- end }}
      terminationGracePeriodSeconds: {{ .Values.aggregator.terminationGracePeriodSeconds }}
      containers:
        - name: fluentd
          image: {{ include "fluentd.aggregator.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.aggregator.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.aggregator.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{ end }}
          {{- if .Values.aggregator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" ( dict "value" .Values.aggregator.lifecycleHooks "context" $ ) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          {{- if .Values.aggregator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.aggregator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.args "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: FLUENTD_CONF
              value: {{ .Values.aggregator.configFile }}
            - name: FLUENTD_OPT
              value: {{ .Values.aggregator.extraArgs | quote }}
            {{- if .Values.aggregator.extraEnvVars }}
            {{- toYaml .Values.aggregator.extraEnvVars | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.aggregator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.aggregator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.aggregator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.aggregator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.aggregator.resources }}
          resources: {{- toYaml .Values.aggregator.resources | 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 not .Values.diagnosticMode.enabled }}
          {{- if .Values.aggregator.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: {{ .Values.aggregator.startupProbe.httpGet.path }}
              port: {{ .Values.aggregator.startupProbe.httpGet.port }}
            initialDelaySeconds: {{ .Values.aggregator.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.aggregator.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.aggregator.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.aggregator.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.aggregator.startupProbe.failureThreshold }}
          {{- else if .Values.aggregator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.aggregator.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ .Values.aggregator.livenessProbe.httpGet.path }}
              port: {{ .Values.aggregator.livenessProbe.httpGet.port }}
            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 }}
          {{- else if .Values.aggregator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.aggregator.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ .Values.aggregator.readinessProbe.httpGet.path }}
              port: {{ .Values.aggregator.readinessProbe.httpGet.port }}
            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 }}
          {{- else if .Values.aggregator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: fluentd-config
              mountPath: /opt/bitnami/fluentd/conf
            {{- if .Values.aggregator.persistence.enabled }}
            - name: {{ include "common.names.fullname" . }}-buffer
              mountPath: /opt/bitnami/fluentd/logs/buffers
            {{- else }}
            - name: buffer
              mountPath: /opt/bitnami/fluentd/logs/buffers
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: fluentd-aggregator-certificates
              mountPath: /opt/bitnami/fluentd/certs/in_forward
              readOnly: true
            {{- end }}
            {{- if .Values.aggregator.extraVolumeMounts }}
            {{- toYaml .Values.aggregator.extraVolumeMounts | nindent 12 }}
            {{- end }}
        {{- if .Values.aggregator.sidecars }}
        {{- include "common.tplvalues.render" ( 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: fluentd-aggregator-certificates
          secret:
            secretName: {{ include "fluentd.aggregator.tlsSecretName" . }}
            defaultMode: 256
        {{- end }}
        - name: fluentd-config
          configMap:
            name: {{ template "fluentd.aggregator.configMap" . }}
        {{- if not .Values.aggregator.persistence.enabled }}
        - name: buffer
          emptyDir: {}
        {{- end }}

  {{- if or .Values.aggregator.persistence.enabled .Values.aggregator.extraVolumeClaimTemplates }}
  volumeClaimTemplates:
    {{- if or .Values.aggregator.persistence.enabled }}
    - metadata:
        name: {{ include "common.names.fullname" . }}-buffer
        {{- if .Values.aggregator.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.persistence.annotations "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.aggregator.persistence.accessModes }}
            - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.aggregator.persistence.size }}
        {{- if .Values.aggregator.persistence.selector }}
        selector: {{- include "common.tplvalues.render" ( dict "value" .Values.aggregator.persistence.selector "context" $) | nindent 10 }}
        {{- end }}
        {{- include "common.storage.class" (dict "persistence" .Values.aggregator.persistence "global" .Values.global) | nindent 8 }}
    {{- end }}
    {{- if .Values.aggregator.extraVolumeClaimTemplates }}
    {{- include "common.tplvalues.render" (dict "value" .Values.aggregator.extraVolumeClaimTemplates "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end -}}
