{{- if .Values.querier.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "thanos.fullname" . }}-querier
  labels: {{- include "thanos.labels" . | nindent 4 }}
    app.kubernetes.io/component: querier
spec:
  replicas: {{ .Values.querier.replicaCount }}
  strategy:
    type: {{ .Values.querier.strategyType }}
    {{- if (eq "Recreate" .Values.querier.strategyType) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: {{- include "thanos.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: querier
  template:
    metadata:
      labels: {{- include "thanos.labels" . | nindent 8 }}
        app.kubernetes.io/component: querier
      {{- if or (include "thanos.querier.createSDConfigmap" .) .Values.querier.existingSDConfigmap .Values.querier.podAnnotations }}
      annotations:
        {{- if or (include "thanos.querier.createSDConfigmap" .) .Values.querier.existingSDConfigmap }}
        checksum/ruler-configuration: {{ include (print $.Template.BasePath "/querier/sd-configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.querier.podAnnotations }}
        {{- include "thanos.tplValue" (dict "value" .Values.querier.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
{{- include "thanos.imagePullSecrets" . | nindent 6 }}
      serviceAccount: {{ include "thanos.serviceaccount.name" (dict "component" "querier" "context" $) }}
      {{- if .Values.querier.affinity }}
      affinity: {{- include "thanos.tplValue" (dict "value" .Values.querier.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.nodeSelector }}
      nodeSelector: {{- include "thanos.tplValue" (dict "value" .Values.querier.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.tolerations }}
      tolerations: {{- include "thanos.tplValue" (dict "value" .Values.querier.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.querier.priorityClassName }}
      priorityClassName: {{ .Values.querier.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.querier.securityContext.enabled }}
      securityContext:
        runAsUser: {{ .Values.querier.securityContext.runAsUser }}
        fsGroup: {{ .Values.querier.securityContext.fsGroup }}
      {{- end }}
      containers:
        - name: querier
          image: {{ include "thanos.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          args:
            - query
            - --log.level={{ .Values.querier.logLevel }}
            - --grpc-address=0.0.0.0:10901
            - --http-address=0.0.0.0:10902
            {{- if kindIs "string" .Values.querier.replicaLabel }}
            - --query.replica-label={{ .Values.querier.replicaLabel }}
            {{- else }}
            {{- range .Values.querier.replicaLabel }}
            - --query.replica-label={{ . }}
            {{- end }}
            {{- end }}
            {{- if or (include "thanos.querier.createSDConfigmap" .) .Values.querier.existingSDConfigmap }}
            - --store.sd-files=/conf/servicediscovery.yml
            {{- end }}
            {{- if and .Values.querier.dnsDiscovery.enabled .Values.querier.dnsDiscovery.sidecarsService .Values.querier.dnsDiscovery.sidecarsNamespace }}
            - --store=dnssrv+_grpc._tcp.{{- include "thanos.tplValue" ( dict "value" .Values.querier.dnsDiscovery.sidecarsService "context" $) -}}.{{- include "thanos.tplValue"  ( dict "value" .Values.querier.dnsDiscovery.sidecarsNamespace "context" $) -}}.svc.{{ .Values.clusterDomain }}
            {{- end }}
            {{- if and .Values.storegateway.enabled .Values.querier.dnsDiscovery.enabled }}
            - --store=dnssrv+_grpc._tcp.{{ include "thanos.fullname" . }}-storegateway.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}
            {{- end }}
            {{- if and .Values.ruler.enabled .Values.querier.dnsDiscovery.enabled }}
            - --store=dnssrv+_grpc._tcp.{{ include "thanos.fullname" . }}-ruler.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}
            {{- end }}
            {{- range .Values.querier.stores }}
            - --store={{ . }}
            {{- end }}
            {{- if .Values.querier.grpcTLS.server.secure }}
            - --grpc-server-tls-cert=/tls/server/cert.pem
            - --grpc-server-tls-key=/tls/server/key.pem
            - --grpc-server-tls-client-ca=/tls/server/ca.pem
            {{- end }}
            {{- if .Values.querier.grpcTLS.client.secure }}
            - --grpc-client-tls-secure
            {{- if .Values.querier.grpcTLS.client.cert }}
            - --grpc-client-tls-cert=/tls/client/cert.pem
            {{- end }}
            {{- if .Values.querier.grpcTLS.client.key }}
            - --grpc-client-tls-key=/tls/client/key.pem
            {{- end }}
            {{- if .Values.querier.grpcTLS.client.ca }}
            - --grpc-client-tls-ca=/tls/client/ca.pem
            {{- end }}
            {{- if .Values.querier.grpcTLS.client.servername }}
            - --grpc-client-server-name={{.Values.querier.grpcTLS.client.servername}}
            {{- end }}
            {{- end }}
            {{- range .Values.querier.extraFlags }}
            - {{ . }}
            {{- end }}
          ports:
            - name: http
              containerPort: 10902
              protocol: TCP
            - name: grpc
              containerPort: 10901
              protocol: TCP
          {{- if .Values.querier.livenessProbe }}
          livenessProbe: {{- include "thanos.tplValue" (dict "value" .Values.querier.livenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.querier.readinessProbe }}
          readinessProbe: {{- include "thanos.tplValue" (dict "value" .Values.querier.readinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.querier.resources }}
          resources: {{- toYaml .Values.querier.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
      {{- if or (include "thanos.querier.createSDConfigmap" .) .Values.querier.existingSDConfigmap }}
            - name: sd-config
              mountPath: /conf/servicediscovery.yml
              subPath: servicediscovery.yml
      {{- end }}
      {{- if .Values.querier.grpcTLS.server.secure }}
            - name: tls-server
              mountPath: /tls/server
      {{- end }}
      {{- if .Values.querier.grpcTLS.client.secure }}
            - name: tls-client
              mountPath: /tls/client
      {{- end }}
      volumes:
      {{- if or (include "thanos.querier.createSDConfigmap" .) .Values.querier.existingSDConfigmap }}
        - name: sd-config
          configMap:
            name: {{ include "thanos.querier.SDConfigmapName" . }}
      {{- end }}
      {{- if .Values.querier.grpcTLS.server.secure }}
        - name: tls-server
          secret:
            secretName: {{ include "thanos.fullname" . }}-querier-tls-server
      {{- end }}
      {{- if .Values.querier.grpcTLS.client.secure }}
        - name: tls-client
          secret:
            secretName: {{ include "thanos.fullname" . }}-querier-tls-client
      {{- end }}
{{- end }}
