{{- $isDistributed := eq (include "loki.deployment.isDistributed" .) "true" -}}
{{- if and $isDistributed .Values.ingester.zoneAwareReplication.enabled }}
{{- $replicas := (include "loki.ingester.replicaCount" .) -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "loki.ingesterFullname" . }}-zone-a
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "loki.ingesterLabels" . | nindent 4 }}
    app.kubernetes.io/part-of: memberlist
    name: {{ include "loki.prefixIngesterName" . }}ingester-zone-a
    rollout-group: {{ include "loki.prefixRolloutGroup" . }}ingester
  annotations:
    rollout-max-unavailable: "{{ include "loki.ingester.maxUnavailable" (dict "ctx" . "replicas" $replicas)}}"
    {{- with .Values.loki.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.ingester.zoneAwareReplication.zoneA.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
{{- if not .Values.ingester.autoscaling.enabled }}
  replicas: {{ $replicas }}
{{- end }}
  podManagementPolicy: Parallel
  serviceName: {{ include "loki.ingesterFullname" . }}-zone-a
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  {{- if and (semverCompare ">= 1.23-0" (include "loki.kubeVersion" .)) (.Values.ingester.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: {{ .Values.ingester.persistence.whenDeleted }}
    whenScaled: {{ .Values.ingester.persistence.whenScaled }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "loki.ingesterSelectorLabels" . | nindent 6 }}
      name: {{ include "loki.prefixIngesterName" . }}ingester-zone-a
      rollout-group: {{ include "loki.prefixRolloutGroup" . }}ingester
{{- with .Values.ingester.updateStrategy }}
  updateStrategy:
     {{- tpl (. | toYaml) $ | nindent 4 }}
{{- end }}
  template:
    metadata:
      annotations:
        {{- include "loki.config.checksum" . | nindent 8 }}
        {{- with .Values.loki.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.ingester.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.ingester.zoneAwareReplication.zoneA.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "loki.ingesterLabels" . | nindent 8 }}
        app.kubernetes.io/part-of: memberlist
        name: {{ include "loki.prefixIngesterName" . }}ingester-zone-a
        rollout-group: {{ include "loki.prefixRolloutGroup" . }}ingester
        {{- with merge (dict) .Values.loki.podLabels .Values.ingester.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.Version }}
      {{- with .Values.ingester.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- end }}
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ingester.hostAliases }}
      hostAliases:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.ingesterPriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.loki.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.ingester.terminationGracePeriodSeconds }}
      {{- with .Values.ingester.initContainers }}
      initContainers:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: ingester
          image: {{ include "loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          {{- if or .Values.loki.command .Values.ingester.command }}
          command:
            - {{ coalesce .Values.ingester.command .Values.loki.command | quote }}
          {{- end }}
          args:
            - -config.file=/etc/loki/config/config.yaml
            - -ingester.availability-zone=zone-a
            - -ingester.unregister-on-shutdown=false
            - -ingester.tokens-file-path=/var/loki/ring-tokens
            - -target=ingester
            {{- with (concat .Values.global.extraArgs .Values.ingester.extraArgs) | uniq }}
            {{- 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 (concat .Values.global.extraEnv .Values.ingester.extraEnv) | uniq }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with (concat .Values.global.extraEnvFrom .Values.ingester.extraEnvFrom) | uniq }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.loki.containerSecurityContext | nindent 12 }}
          {{- include "loki.ingester.readinessProbe" . | nindent 10 }}
          {{- include "loki.ingester.livenessProbe" . | nindent 10 }}
          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 (concat .Values.global.extraVolumeMounts .Values.ingester.extraVolumeMounts) | uniq }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- with .Values.ingester.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.ingester.lifecycle }}
          lifecycle:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- if .Values.ingester.extraContainers }}
        {{- toYaml .Values.ingester.extraContainers | nindent 8}}
        {{- end }}
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: rollout-group
                    operator: In
                    values:
                      - {{ include "loki.prefixRolloutGroup" . }}ingester
                  - key: name
                    operator: NotIn
                    values:
                      - {{ include "loki.prefixIngesterName" . }}ingester-zone-a
              topologyKey: kubernetes.io/hostname
        {{- with .Values.ingester.zoneAwareReplication.zoneA.extraAffinity }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.ingester.zoneAwareReplication.zoneA.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ingester.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - 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 (concat .Values.global.extraVolumes .Values.ingester.extraVolumes) | uniq }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
  {{- if not .Values.ingester.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else if .Values.ingester.persistence.inMemory }}
        - name: data
        {{- if .Values.ingester.persistence.inMemory }}
          emptyDir:
            medium: Memory
        {{- end }}
        {{- if .Values.ingester.persistence.size }}
            sizeLimit: {{ .Values.ingester.persistence.size }}
        {{- end }}
  {{- else }}
  volumeClaimTemplates:
  {{- range .Values.ingester.persistence.claims }}
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: {{ .name }}
        {{- with .annotations }}
        annotations:
          {{- . | toYaml | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          - ReadWriteOnce
        {{- with .storageClass }}
        storageClassName: {{ if (eq "-" .) }}""{{ else }}{{ . }}{{ end }}
        {{- end }}
        resources:
          requests:
            storage: {{ .size | quote }}
  {{- end }}
  {{- end }}
{{- end }}
