{{- $isSimpleScalable := eq (include "loki.deployment.isScalable" .) "true" -}}
{{- if $isSimpleScalable }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "loki.writeFullname" . }}
  namespace: {{ $.Release.Namespace }}
  labels:
    {{- include "loki.writeLabels" . | 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.write.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
{{- if not .Values.write.autoscaling.enabled }}
  replicas: {{ .Values.write.replicas }}
{{- end }}
  podManagementPolicy: {{ .Values.write.podManagementPolicy }}
  updateStrategy:
    rollingUpdate:
      partition: 0
  serviceName: {{ include "loki.writeFullname" . }}-headless
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  {{- if and (semverCompare ">= 1.23-0" .Capabilities.KubeVersion.Version) (.Values.write.persistence.enableStatefulSetAutoDeletePVC) (.Values.write.persistence.volumeClaimsEnabled) }}
  {{/*
    Data on the write 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.writeSelectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print .Template.BasePath "/config.yaml") . | sha256sum }}
        {{- with .Values.loki.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.write.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "loki.writeSelectorLabels" . | nindent 8 }}
        {{- with .Values.loki.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.write.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.write.selectorLabels }}
        {{- tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
        app.kubernetes.io/part-of: memberlist
    spec:
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }}
      {{ include "loki.enableServiceLinks" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.writePriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.loki.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.write.terminationGracePeriodSeconds }}
      {{- if .Values.write.initContainers }}
      initContainers:
        {{- with .Values.write.initContainers }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: loki
          image: {{ include "loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          args:
            - -config.file=/etc/loki/config/config.yaml
            - -target={{ .Values.write.targetModule }}
            {{- with .Values.write.extraArgs }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: http-metrics
              containerPort: 3100
              protocol: TCP
            - name: grpc
              containerPort: 9095
              protocol: TCP
            - name: http-memberlist
              containerPort: 7946
              protocol: TCP
          {{- with .Values.write.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.write.extraEnvFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.loki.containerSecurityContext | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.loki.readinessProbe | nindent 12 }}
          {{- if .Values.write.lifecycle }}
          lifecycle:
            {{- toYaml .Values.write.lifecycle | nindent 12 }}
          {{- else if .Values.write.autoscaling.enabled }}
          lifecycle:
            preStop:
              httpGet:
                path: "/ingester/shutdown?terminate=false"
                port: http-metrics
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: /etc/loki/config
            - name: runtime-config
              mountPath: /etc/loki/runtime-config
            - name: data
              mountPath: /var/loki
            {{- if .Values.enterprise.enabled }}
            - name: license
              mountPath: /etc/loki/license
            {{- end}}
            {{- with .Values.write.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.write.resources | nindent 12 }}
        {{- with .Values.write.extraContainers }}
        {{- toYaml . | nindent 8}}
        {{- end }}
      {{- with .Values.write.affinity }}
      affinity:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.write.dnsConfig }}
      dnsConfig:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.write.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.write.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.write.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- if not .Values.write.persistence.volumeClaimsEnabled }}
        - name: data
        {{- toYaml .Values.write.persistence.dataVolumeParameters | nindent 10 }}
        {{- end}}
        - name: config
          {{- if .Values.loki.existingSecretForConfig }}
          secret:
            secretName: {{ .Values.loki.existingSecretForConfig }}
          {{- else }}
          {{- include "loki.configVolume" . | nindent 10 }}
          {{- end }}
        - 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.write.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
  {{- if .Values.write.persistence.volumeClaimsEnabled }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        {{- with .Values.write.persistence.storageClass }}
        storageClassName: {{ if (eq "-" .) }}""{{ else }}{{ . }}{{ end }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.write.persistence.size | quote }}
        {{- with .Values.write.persistence.selector }}
        selector:
          {{- toYaml . | nindent 10 }}
        {{- end }}
    {{- with .Values.write.extraVolumeClaimTemplates }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}