{{- $isSimpleScalable := eq (include "loki.deployment.isScalable" .) "true" -}}
{{- if and $isSimpleScalable (.Values.read.legacyReadTarget ) }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "loki.readFullname" . }}
  namespace: {{ $.Release.Namespace }}
  labels:
    app.kubernetes.io/part-of: memberlist
    {{- include "loki.readLabels" . | nindent 4 }}
  {{- if or (not (empty .Values.loki.annotations)) (not (empty .Values.read.annotations))}}
  annotations:
    {{- with .Values.loki.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.read.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
{{- if not .Values.read.autoscaling.enabled }}
  {{- if eq .Values.deploymentMode "SingleBinary" }}
  replicas: 0
  {{- else }}
  replicas: {{ .Values.read.replicas }}
  {{- end }}
{{- end }}
  podManagementPolicy: {{ .Values.read.podManagementPolicy }}
  updateStrategy:
    rollingUpdate:
      partition: 0
  serviceName: {{ printf "%s-headless" (include "loki.readFullname" .) }}
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  {{- if and (semverCompare ">= 1.23-0" (include "loki.kubeVersion" .)) (.Values.read.persistence.enableStatefulSetAutoDeletePVC)  }}
  {{/*
    Data on the read 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.readSelectorLabels" . | 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.read.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        app.kubernetes.io/part-of: memberlist
        {{- include "loki.readSelectorLabels" . | nindent 8 }}
        {{- with .Values.loki.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.read.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.read.selectorLabels }}
        {{- tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }}
      {{ include "loki.enableServiceLinks" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.readPriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.loki.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.read.terminationGracePeriodSeconds }}
      containers:
        - name: loki
          image: {{ include "loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          args:
            - -config.file=/etc/loki/config/config.yaml
            - -target={{ .Values.read.targetModule }}
            {{- with .Values.read.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.read.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.read.extraEnvFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.loki.containerSecurityContext | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.loki.readinessProbe | nindent 12 }}
          {{- with .Values.read.lifecycle }}
          lifecycle:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          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}}
            {{- with .Values.read.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.read.resources | nindent 12 }}
        {{- with .Values.read.extraContainers }}
        {{- toYaml . | nindent 8}}
        {{- end }}
      {{- with .Values.read.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.read.dnsConfig }}
      dnsConfig:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.read.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.read.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.read.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
        - 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 }}
        {{- with .Values.read.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
        {{- with .Values.read.persistence.annotations }}
        annotations:
          {{- toYaml . | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          - ReadWriteOnce
        {{- with .Values.read.persistence.storageClass }}
        storageClassName: {{ if (eq "-" .) }}""{{ else }}{{ . }}{{ end }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.read.persistence.size | quote }}
        {{- with .Values.read.persistence.selector }}
        selector:
          {{- toYaml . | nindent 10 }}
        {{- end }}
{{- end }}