{{- $isSimpleScalable := eq (include "loki.deployment.isScalable" .) "true" -}}
{{- if and $isSimpleScalable (not .Values.read.legacyReadTarget ) }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "loki.backendFullname" . }}
  namespace: {{ $.Release.Namespace }}
  labels:
    {{- include "loki.backendLabels" . | nindent 4 }}
    app.kubernetes.io/part-of: memberlist
  {{- if or (not (empty .Values.loki.annotations)) (not (empty .Values.backend.annotations))}}
  annotations:
    {{- with .Values.loki.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.backend.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
{{- if not .Values.backend.autoscaling.enabled }}
  {{- if eq .Values.deploymentMode "SingleBinary" }}
  replicas: 0
  {{- else }}
  replicas: {{ .Values.backend.replicas }}
  {{- end }}
{{- end }}
  podManagementPolicy: {{ .Values.backend.podManagementPolicy }}
  updateStrategy:
    rollingUpdate:
      partition: 0
  serviceName: {{ include "loki.backendFullname" . }}-headless
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  {{- if and (semverCompare ">= 1.23-0" (include "loki.kubeVersion" .)) (.Values.backend.persistence.enableStatefulSetAutoDeletePVC) (.Values.backend.persistence.volumeClaimsEnabled) }}
  {{/*
    Data on the backend nodes is easy to replace, so we want to always delete PVCs to make
    operation easier, and will rely on re-fetching data when needed.
  */}}
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Delete
    whenScaled: Delete
  {{- end }}
  selector:
    matchLabels:
      {{- include "loki.backendSelectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include "loki.configMapOrSecretContentHash" (dict "ctx" . "name" "/config.yaml") }}
        {{- with .Values.loki.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.backend.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "loki.backendSelectorLabels" . | nindent 8 }}
        {{- with .Values.loki.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.backend.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.backend.selectorLabels }}
        {{- tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
        app.kubernetes.io/part-of: memberlist
    spec:
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.backendPriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.loki.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.backend.terminationGracePeriodSeconds }}
      {{- if .Values.backend.initContainers }}
      initContainers:
        {{- with .Values.backend.initContainers }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
      {{- if .Values.sidecar.rules.enabled }}
        - name: loki-sc-rules
          {{- if .Values.sidecar.image.sha }}
          image: "{{ .Values.sidecar.image.repository }}:{{ .Values.sidecar.image.tag }}@sha256:{{ .Values.sidecar.image.sha }}"
          {{- else }}
          image: "{{ .Values.sidecar.image.repository }}:{{ .Values.sidecar.image.tag }}"
          {{- end }}
          imagePullPolicy: {{ .Values.sidecar.image.pullPolicy }}
          env:
            - name: METHOD
              value: {{ .Values.sidecar.rules.watchMethod }}
            - name: LABEL
              value: "{{ .Values.sidecar.rules.label }}"
            {{- if .Values.sidecar.rules.labelValue }}
            - name: LABEL_VALUE
              value: {{ quote .Values.sidecar.rules.labelValue }}
            {{- end }}
            - name: FOLDER
              value: "{{ .Values.sidecar.rules.folder }}"
            - name: RESOURCE
              value: {{ quote .Values.sidecar.rules.resource }}
            {{- if .Values.sidecar.enableUniqueFilenames }}
            - name: UNIQUE_FILENAMES
              value: "{{ .Values.sidecar.enableUniqueFilenames }}"
            {{- end }}
            {{- if .Values.sidecar.rules.searchNamespace }}
            - name: NAMESPACE
              value: "{{ .Values.sidecar.rules.searchNamespace | join "," }}"
            {{- end }}
            {{- if .Values.sidecar.skipTlsVerify }}
            - name: SKIP_TLS_VERIFY
              value: "{{ .Values.sidecar.skipTlsVerify }}"
            {{- end }}
            {{- if .Values.sidecar.rules.script }}
            - name: SCRIPT
              value: "{{ .Values.sidecar.rules.script }}"
            {{- end }}
            {{- if .Values.sidecar.rules.watchServerTimeout }}
            - name: WATCH_SERVER_TIMEOUT
              value: "{{ .Values.sidecar.rules.watchServerTimeout }}"
            {{- end }}
            {{- if .Values.sidecar.rules.watchClientTimeout }}
            - name: WATCH_CLIENT_TIMEOUT
              value: "{{ .Values.sidecar.rules.watchClientTimeout }}"
            {{- end }}
            {{- if .Values.sidecar.rules.logLevel }}
            - name: LOG_LEVEL
              value: "{{ .Values.sidecar.rules.logLevel }}"
            {{- end }}
          {{- if .Values.sidecar.livenessProbe }}
          livenessProbe:
          {{- toYaml .Values.sidecar.livenessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.sidecar.readinessProbe }}
          readinessProbe:
          {{- toYaml .Values.sidecar.readinessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.sidecar.resources }}
          resources:
          {{- toYaml .Values.sidecar.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.sidecar.securityContext }}
          securityContext:
          {{- toYaml .Values.sidecar.securityContext | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: sc-rules-volume
              mountPath: {{ .Values.sidecar.rules.folder | quote }}
        {{- end}}
        - name: loki
          image: {{ include "loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          args:
            - -config.file=/etc/loki/config/config.yaml
            - -target={{ .Values.backend.targetModule }}
            - -legacy-read-mode=false
            {{- with .Values.backend.extraArgs }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: http-metrics
              containerPort: {{ .Values.loki.server.http_listen_port }}
              protocol: TCP
            - name: grpc
              containerPort: {{ .Values.loki.server.grpc_listen_port }}
              protocol: TCP
            - name: http-memberlist
              containerPort: 7946
              protocol: TCP
          {{- with .Values.backend.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.backend.extraEnvFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.loki.containerSecurityContext | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.loki.readinessProbe | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /etc/loki/config
            - name: runtime-config
              mountPath: /etc/loki/runtime-config
            - name: tmp
              mountPath: /tmp
            - name: data
              mountPath: /var/loki
            {{- if .Values.enterprise.enabled }}
            - name: license
              mountPath: /etc/loki/license
            {{- end}}
            {{- if .Values.sidecar.rules.enabled }}
            - name: sc-rules-volume
              mountPath: {{ .Values.sidecar.rules.folder | quote }}
            {{- end}}
            {{- with .Values.backend.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.backend.resources | nindent 12 }}
      {{- with .Values.backend.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.backend.dnsConfig }}
      dnsConfig:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.backend.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.backend.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.backend.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
        {{- if not .Values.backend.persistence.volumeClaimsEnabled }}
        - name: data
        {{- toYaml .Values.backend.persistence.dataVolumeParameters | nindent 10 }}
        {{- end}}
        - name: config
          {{- include "loki.configVolume" . | nindent 10 }}
        - name: runtime-config
          configMap:
            name: {{ template "loki.name" . }}-runtime
        {{- if .Values.enterprise.enabled }}
        - name: license
          secret:
          {{- if .Values.enterprise.useExternalLicense }}
            secretName: {{ .Values.enterprise.externalLicenseName }}
          {{- else }}
            secretName: enterprise-logs-license
          {{- end }}
        {{- end }}
        {{- if .Values.sidecar.rules.enabled }}
        - name: sc-rules-volume
        {{- if .Values.sidecar.rules.sizeLimit }}
          emptyDir:
            sizeLimit: {{ .Values.sidecar.rules.sizeLimit }}
        {{- else }}
          emptyDir: {}
        {{- end -}}
        {{- end -}}
        {{- with .Values.backend.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
  {{- if .Values.backend.persistence.volumeClaimsEnabled }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
        {{- with .Values.backend.persistence.annotations }}
        annotations:
          {{- toYaml . | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          - ReadWriteOnce
        {{- with .Values.backend.persistence.storageClass }}
        storageClassName: {{ if (eq "-" .) }}""{{ else }}{{ . }}{{ end }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.backend.persistence.size | quote }}
        {{- with .Values.backend.persistence.selector }}
        selector:
          {{- toYaml . | nindent 10 }}
        {{- end }}
  {{- end }}
{{- end }}