apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ template "grafana-loki.ingester.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: grafana-loki
    app.kubernetes.io/component: ingester
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.ingester.replicaCount }}
  {{- if .Values.ingester.updateStrategy }}
  updateStrategy: {{- toYaml .Values.ingester.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/part-of: grafana-loki
      app.kubernetes.io/component: ingester
  serviceName: {{ template "grafana-loki.ingester.fullname" . }}
  podManagementPolicy: {{ .Values.ingester.podManagementPolicy }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/loki-configmap.yaml") . | sha256sum }}
        {{- if .Values.ingester.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ingester.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/part-of: grafana-loki
        app.kubernetes.io/component: ingester
        loki-gossip-member: "true"
        {{- if .Values.ingester.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ingester.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "grafana-loki.serviceAccountName" . }}
      {{- include "grafana-loki.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.ingester.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingester.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.ingester.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.ingester.podAffinityPreset "component" "ingester" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.ingester.podAntiAffinityPreset "component" "ingester" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.ingester.nodeAffinityPreset.type "key" .Values.ingester.nodeAffinityPreset.key "values" .Values.ingester.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.ingester.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.ingester.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingester.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingester.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingester.priorityClassName }}
      priorityClassName: {{ .Values.ingester.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.ingester.schedulerName }}
      schedulerName: {{ .Values.ingester.schedulerName }}
      {{- end }}
      {{- if .Values.ingester.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.ingester.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.ingester.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.ingester.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.ingester.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "grafana-loki.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p {{ .Values.loki.dataDir }}{{- if .Values.ingester.persistence.subPath }}/{{ .Values.ingester.persistence.subPath }}{{- end }}
              {{- if and .Values.ingester.podSecurityContext.enabled .Values.ingester.containerSecurityContext.enabled }}
              find {{ .Values.loki.dataDir }}{{- if .Values.ingester.persistence.subPath }}/{{ .Values.ingester.persistence.subPath }}{{- end }} -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.ingester.containerSecurityContext.runAsUser }}:{{ .Values.ingester.podSecurityContext.fsGroup }}
              {{- end }}
          {{- if eq ( toString ( .Values.volumePermissions.containerSecurityContext.runAsUser )) "auto" }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "runAsUser" | toYaml | nindent 12 }}
          {{- else }}
          securityContext: {{- .Values.volumePermissions.containerSecurityContext | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.loki.dataDir }}
        {{- end }}
      containers:
        - name: grafana-loki-ingester
          image: {{ template "grafana-loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          {{- if .Values.ingester.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.ingester.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.ingester.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.ingester.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -target=ingester
            - -config.file=/bitnami/grafana-loki/conf/loki.yaml
          {{- end }}
          {{- if .Values.ingester.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.ingester.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.ingester.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.ingester.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.ingester.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.loki.containerPorts.http }}
              name: http
            - containerPort: {{ .Values.loki.containerPorts.gossipRing }}
              name: http-memberlist
            - containerPort: {{ .Values.loki.containerPorts.grpc }}
              name: grpc
          {{- if .Values.ingester.resources }}
          resources: {{- toYaml .Values.ingester.resources | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.ingester.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ingester.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.ingester.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.ingester.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ingester.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.ingester.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.ingester.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ingester.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.ingester.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.ingester.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: loki-config
              mountPath: /bitnami/grafana-loki/conf/loki.yaml
              subPath: loki.yaml
            - name: data
              mountPath: {{ .Values.loki.dataDir }}
          {{- if .Values.ingester.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.ingester.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.ingester.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.ingester.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: loki-config
          configMap:
            name: {{ template "grafana-loki.loki.configmapName" . }}
        {{- if .Values.ingester.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ingester.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if not .Values.ingester.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- if .Values.commonLabels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
        {{- end }}
        annotations:
          {{- if .Values.ingester.persistence.annotations }}
          {{- include "common.tplvalues.render" (dict "value" .Values.ingester.persistence.annotations "context" $) | nindent 10 }}
          {{- end }}
          {{- if .Values.commonAnnotations }}
          {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 10 }}
          {{- end }}
      spec:
        accessModes:
        {{- range .Values.ingester.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.ingester.persistence.size | quote }}
        {{- if .Values.ingester.persistence.selector }}
        selector: {{- include "common.tplvalues.render" (dict "value" .Values.ingester.persistence.selector "context" $) | nindent 10 }}
        {{- end }}
        {{- include "common.storage.class" (dict "persistence" .Values.ingester.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
