{{- if .Values.ingester.statefulSet.enabled -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "cortex.ingesterFullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "cortex.ingesterLabels" . | nindent 4 }}
    app.kubernetes.io/part-of: memberlist
  annotations:
    {{- toYaml .Values.ingester.annotations | nindent 4 }}
spec:
  {{- if not .Values.ingester.autoscaling.enabled }}
  replicas: {{ .Values.ingester.replicas }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "cortex.ingesterSelectorLabels" . | nindent 6 }}
  updateStrategy:
    {{- toYaml .Values.ingester.statefulStrategy | nindent 4 }}          
  podManagementPolicy: "{{ .Values.ingester.statefulSet.podManagementPolicy }}"
  serviceName: {{ template "cortex.fullname" . }}-ingester-headless
  {{- if .Values.ingester.persistentVolume.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: storage
        {{- if .Values.ingester.persistentVolume.annotations }}
        annotations:
          {{ toYaml .Values.ingester.persistentVolume.annotations | nindent 10 }}
        {{- end }}
      spec:
        {{- if .Values.ingester.persistentVolume.storageClass }}
        {{- if (eq "-" .Values.ingester.persistentVolume.storageClass) }}
        storageClassName: ""
        {{- else }}
        storageClassName: "{{ .Values.ingester.persistentVolume.storageClass }}"
        {{- end }}
        {{- end }}
        accessModes:
          {{ toYaml .Values.ingester.persistentVolume.accessModes | nindent 10 }}
        resources:
          requests:
            storage: "{{ .Values.ingester.persistentVolume.size }}"
  {{- end }}
  template:
    metadata:
      labels:
        {{- include "cortex.ingesterLabels" . | nindent 8 }}
        app.kubernetes.io/part-of: memberlist
        {{- with .Values.ingester.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      annotations:
        checksum/config: {{ include "cortex.configChecksum" . }}
        {{- with .Values.ingester.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ .Values.ingester.serviceAccount.name | default (include "cortex.serviceAccountName" . ) }}
      {{- if .Values.ingester.priorityClassName }}
      priorityClassName: {{ .Values.ingester.priorityClassName }}
      {{- end }}
      {{- if .Values.ingester.securityContext.enabled }}
      securityContext: {{- omit .Values.ingester.securityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      initContainers:
        {{- toYaml .Values.ingester.initContainers | nindent 8 }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      nodeSelector:
        {{- toYaml .Values.ingester.nodeSelector | nindent 8 }}
      affinity:
        {{- toYaml .Values.ingester.affinity | nindent 8 }}
      tolerations:
        {{- toYaml .Values.ingester.tolerations | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.ingester.terminationGracePeriodSeconds }}
      volumes:
        {{- include "cortex.configVolume" . | nindent 8 }}
        - name: runtime-config
          configMap:
            name: {{ template "cortex.fullname" . }}-runtime-config
        {{- if not .Values.ingester.persistentVolume.enabled }}
        - name: storage
          emptyDir: {}
        {{- end }}
        {{- if .Values.ingester.extraVolumes }}
        {{- toYaml .Values.ingester.extraVolumes | nindent 8 }}
        {{- end }}
      containers:
        {{- if .Values.ingester.extraContainers }}
        {{- toYaml .Values.ingester.extraContainers | nindent 8 }}
        {{- end }}
        - name: ingester
          image: "{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - "-target=ingester"
            - "-config.file=/etc/cortex/cortex.yaml"
            {{- include "cortex.memcached" . | nindent 12}}
            {{- range $key, $value := .Values.ingester.extraArgs }}
            - "-{{ $key }}={{ $value }}"
            {{- end }}
          volumeMounts:
            {{- if .Values.ingester.extraVolumeMounts }}
            {{- toYaml .Values.ingester.extraVolumeMounts | nindent 12}}
            {{- end }}
            - name: config
              mountPath: /etc/cortex
            - name: runtime-config
              mountPath: /etc/cortex-runtime-config
            - name: storage
              mountPath: "/data"
              {{- with .Values.ingester.persistentVolume.subPath }}
              subPath: {{ . }}
              {{- end }}
          ports:
            - name: http-metrics
              containerPort: {{ .Values.config.server.http_listen_port }}
              protocol: TCP
            - name: grpc
              containerPort: {{ .Values.config.server.grpc_listen_port }}
              protocol: TCP
            - name: gossip
              containerPort: {{ .Values.config.memberlist.bind_port }}
              protocol: TCP
          {{- if .Values.ingester.startupProbe }}
          startupProbe:
            {{- toYaml .Values.ingester.startupProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.ingester.livenessProbe }}
          livenessProbe:
            {{- toYaml .Values.ingester.livenessProbe | nindent 12 }}
          {{- end }}
          readinessProbe:
            {{- toYaml .Values.ingester.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.ingester.resources | nindent 12 }}
          {{- if .Values.ingester.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.ingester.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.ingester.env }}
          env:
            {{- toYaml .Values.ingester.env | nindent 12 }}
          {{- end }}
          {{- with .Values.ingester.lifecycle }}
          lifecycle:
            {{- toYaml . | nindent 12 }}
          {{- end }}
{{- end -}}
