{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ template "grafana-loki.querier.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: grafana-loki
    app.kubernetes.io/component: querier
    loki-gossip-member: "true"
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.querier.replicaCount }}
  {{- if .Values.querier.updateStrategy }}
  updateStrategy: {{- toYaml .Values.querier.updateStrategy | nindent 4 }}
  {{- end }}
  podManagementPolicy: {{ .Values.querier.podManagementPolicy }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.querier.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: grafana-loki
      app.kubernetes.io/component: querier
      loki-gossip-member: "true"
  serviceName: {{ template "grafana-loki.querier.fullname" . }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/loki-configmap.yaml") . | sha256sum }}
        {{- if .Values.querier.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.querier.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: grafana-loki
        app.kubernetes.io/component: querier
        loki-gossip-member: "true"
    spec:
      serviceAccountName: {{ template "grafana-loki.serviceAccountName" . }}
      {{- include "grafana-loki.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.querier.automountServiceAccountToken }}
      {{- if .Values.querier.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.querier.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.querier.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.querier.podAffinityPreset "component" "querier" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.querier.podAntiAffinityPreset "component" "querier" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.querier.nodeAffinityPreset.type "key" .Values.querier.nodeAffinityPreset.key "values" .Values.querier.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.querier.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.querier.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.querier.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.querier.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.priorityClassName }}
      priorityClassName: {{ .Values.querier.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.querier.schedulerName }}
      schedulerName: {{ .Values.querier.schedulerName }}
      {{- end }}
      {{- if .Values.querier.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.querier.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      enableServiceLinks: {{ .Values.querier.enableServiceLinks }}
      initContainers:
        {{- if .Values.querier.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.querier.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.querier.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "grafana-loki.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p {{ .Values.loki.dataDir }}{{- if .Values.querier.persistence.subPath }}/{{ .Values.querier.persistence.subPath }}{{- end }}
              {{- if and .Values.querier.podSecurityContext.enabled .Values.querier.containerSecurityContext.enabled }}
              find {{ .Values.loki.dataDir }}{{- if .Values.querier.persistence.subPath }}/{{ .Values.querier.persistence.subPath }}{{- end }} -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.querier.containerSecurityContext.runAsUser }}:{{ .Values.querier.podSecurityContext.fsGroup }}
              {{- end }}
          {{- if eq ( toString ( .Values.volumePermissions.containerSecurityContext.runAsUser )) "auto" }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "runAsUser" | toYaml | nindent 12 }}
          {{- else }}
          securityContext: {{- .Values.volumePermissions.containerSecurityContext | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.volumePermissions.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.loki.dataDir }}
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- end }}
      containers:
        - name: grafana-loki-querier
          image: {{ template "grafana-loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          {{- if .Values.querier.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.querier.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.querier.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.querier.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.querier.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.querier.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -target=querier
            - -config.file={{ .Values.loki.dataDir }}/conf/loki.yaml
            {{- if .Values.queryScheduler.enabled }}
            - -querier.scheduler-address={{ template "grafana-loki.query-scheduler.fullname" . }}:{{ .Values.queryScheduler.service.ports.grpc }}
            {{- end }}
          {{- if .Values.querier.extraArgs }}
          {{- include "common.tplvalues.render" (dict "value" .Values.querier.extraArgs "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.querier.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.querier.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.querier.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.querier.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.querier.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.querier.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.loki.containerPorts.http }}
              name: http
            - containerPort: {{ .Values.loki.containerPorts.gossipRing }}
              name: http-memberlist
            - containerPort: {{ .Values.loki.containerPorts.grpc }}
              name: grpc
          {{- if .Values.querier.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.querier.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.querier.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.querier.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.querier.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.querier.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.querier.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.querier.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.querier.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.querier.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.querier.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.querier.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: http
          {{- end }}
          {{- if .Values.querier.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.querier.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.querier.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.querier.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.querier.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.querier.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: loki-config
              mountPath: {{ .Values.loki.dataDir }}/conf/loki.yaml
              subPath: loki.yaml
            - name: data
              mountPath: {{ .Values.loki.dataDir }}
          {{- if .Values.querier.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.querier.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.querier.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.querier.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: loki-config
          configMap:
            name: {{ template "grafana-loki.loki.configmapName" . }}
        {{- if .Values.querier.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.querier.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if not .Values.querier.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
        {{- if .Values.commonLabels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
        {{- end }}
        {{- if or .Values.querier.persistence.annotations .Values.commonAnnotations }}
        {{- $claimAnnotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.querier.persistence.annotations .Values.commonAnnotations ) "context" . ) }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" $claimAnnotations "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.querier.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.querier.persistence.size | quote }}
        {{- if .Values.querier.persistence.selector }}
        selector: {{- include "common.tplvalues.render" (dict "value" .Values.querier.persistence.selector "context" $) | nindent 10 }}
        {{- end }}
        {{- include "common.storage.class" (dict "persistence" .Values.querier.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
