# grafana_charts_statefulset-querier.yaml
{{- if not .Values.indexGateway.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "loki.querierFullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "loki.querierLabels" . | nindent 4 }}
    app.kubernetes.io/part-of: memberlist
  {{- with .Values.loki.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.querier.replicas }}
  podManagementPolicy: Parallel
  updateStrategy:
    rollingUpdate:
      partition: 0
  serviceName: {{ include "loki.querierFullname" . }}-headless
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "loki.querierSelectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- include "loki.config.checksum" . | nindent 8 }}
        {{- with .Values.loki.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.querier.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "loki.querierSelectorLabels" . | nindent 8 }}
        app.kubernetes.io/part-of: memberlist
        {{- with .Values.loki.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.querier.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion }}
      {{- with .Values.querier.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- end }}
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.querier.hostAliases }}
      hostAliases:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.querierPriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.loki.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.querier.terminationGracePeriodSeconds }}
      {{- with .Values.querier.initContainers }}
      initContainers:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: querier
          image: {{ include "loki.querierImage" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          {{- if or .Values.loki.command .Values.querier.command }}
          command:
            - {{ coalesce .Values.querier.command .Values.loki.command | quote }}
          {{- end }}
          args:
            - -config.file=/etc/loki/config/config.yaml
            - -target=querier
            {{- with .Values.querier.extraArgs }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: http
              containerPort: 3100
              protocol: TCP
            - name: grpc
              containerPort: 9095
              protocol: TCP
            - name: http-memberlist
              containerPort: 7946
              protocol: TCP
          {{- with .Values.querier.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.querier.extraEnvFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.loki.containerSecurityContext | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.loki.readinessProbe | nindent 12 }}
          livenessProbe:
            {{- toYaml .Values.loki.livenessProbe | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /etc/loki/config
            - name: runtime-config
              mountPath: /var/{{ include "loki.name" . }}-runtime
            - name: data
              mountPath: /var/loki
            {{- with .Values.querier.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.querier.resources | nindent 12 }}
        {{- if .Values.querier.extraContainers }}
        {{- toYaml .Values.querier.extraContainers | nindent 8}}
        {{- end }}
      {{- with .Values.querier.affinity }}
      affinity:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.querier.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.querier.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.querier.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: config
          {{- if .Values.loki.existingSecretForConfig }}
          secret:
            secretName: {{ .Values.loki.existingSecretForConfig }}
          {{- else if .Values.loki.configAsSecret }}
          secret:
            secretName: {{ include "loki.fullname" . }}-config
          {{- else }}
          configMap:
            name: {{ include "loki.fullname" . }}
          {{- end }}
        - name: runtime-config
          configMap:
            name: {{ template "loki.fullname" . }}-runtime
        {{- with .Values.querier.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
  {{- if not .Values.querier.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- with .Values.querier.persistence.annotations }}
        annotations:
          {{- . | toYaml | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          - ReadWriteOnce
        {{- with .Values.querier.persistence.storageClass }}
        storageClassName: {{ if (eq "-" .) }}""{{ else }}{{ . }}{{ end }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.querier.persistence.size | quote }}
  {{- end }}
{{- end }}