{{- if .Values.query.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "jaeger.query.name" . }}
  namespace: {{ .Release.Namespace  }}
  labels:
    {{- include "jaeger.labels" . | nindent 4 }}
    app.kubernetes.io/component: query
{{- if .Values.query.annotations }}
  annotations:
    {{- toYaml .Values.query.annotations | nindent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.query.replicaCount }}
  selector:
    matchLabels:
      {{- include "jaeger.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: query
  strategy:
    type: Recreate
  template:
    metadata:
{{- if .Values.query.podAnnotations }}
      annotations:
        {{- toYaml .Values.query.podAnnotations | nindent 8 }}
{{- end }}
      labels:
        {{- include "jaeger.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: query
{{- if .Values.query.podLabels }}
        {{- toYaml .Values.query.podLabels | nindent 8 }}
{{- end }}
    spec:
      {{- with .Values.query.priorityClassName }}
      priorityClassName: {{ . }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.query.podSecurityContext | nindent 8 }}
      serviceAccountName: {{ template "jaeger.query.serviceAccountName" . }}
      {{- with .Values.query.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: {{ template "jaeger.query.name" . }}
        securityContext:
          {{- toYaml .Values.query.securityContext | nindent 10 }}
        image: {{ .Values.query.image }}:{{ .Values.tag }}
        imagePullPolicy: {{ .Values.query.pullPolicy }}
        args:
          {{- range $key, $value := .Values.query.cmdlineParams }}
          {{- if $value }}
          - --{{ $key }}={{ $value }}
          {{- else }}
          - --{{ $key }}
          {{- end }}
          {{- end }}
          {{- include "storage.cmdArgs" . | nindent 10 }}
        env:
        {{- if .Values.query.extraEnv }}
          {{- toYaml .Values.query.extraEnv | nindent 10 }}
        {{- end }}
          - name: SPAN_STORAGE_TYPE
            value: {{ .Values.storage.type }}
          {{- include "storage.env" . | nindent 10 }}
          {{- if .Values.query.basePath }}
          - name: QUERY_BASE_PATH
            value: {{ .Values.query.basePath | quote }}
          {{- end }}
          - name: JAEGER_AGENT_PORT
            value: "6831"
          {{- if .Values.query.config}}
          - name: QUERY_UI_CONFIG
            value: /etc/conf/query-ui-config.json
          {{- end }}
        ports:
        - name: query
          containerPort: 16686
          protocol: TCP
        - name: admin
          containerPort: 16687
          protocol: TCP
        resources:
          {{- toYaml .Values.query.resources | nindent 10 }}
        volumeMounts:
        {{- range .Values.query.extraSecretMounts }}
          - name: {{ .name }}
            mountPath: {{ .mountPath }}
            subPath: {{ .subPath }}
            readOnly: {{ .readOnly }}
        {{- end }}
        {{- range .Values.query.extraConfigmapMounts }}
          - name: {{ .name }}
            mountPath: {{ .mountPath }}
            subPath: {{ .subPath }}
            readOnly: {{ .readOnly }}
        {{- end }}
        {{- if .Values.storage.cassandra.tls.enabled }}
          - name: {{ .Values.storage.cassandra.tls.secretName }}
            mountPath: "/cassandra-tls/ca-cert.pem"
            subPath: "ca-cert.pem"
            readOnly: true
          - name: {{ .Values.storage.cassandra.tls.secretName }}
            mountPath: "/cassandra-tls/client-cert.pem"
            subPath: "client-cert.pem"
            readOnly: true
          - name: {{ .Values.storage.cassandra.tls.secretName }}
            mountPath: "/cassandra-tls/client-key.pem"
            subPath: "client-key.pem"
            readOnly: true
        {{- end }}
        {{- if .Values.query.config}}
          - name: ui-configuration
            mountPath: /etc/conf/
        {{- end }}
        livenessProbe:
          httpGet:
            path: /
            port: admin
        readinessProbe:
          httpGet:
            path: /
            port: admin
{{- if .Values.query.agentSidecar.enabled }}
      - name: {{ template "jaeger.agent.name" . }}-sidecar
        securityContext:
          {{- toYaml .Values.query.securityContext | nindent 10 }}
        image: {{ .Values.agent.image }}:{{ .Values.tag }}
        imagePullPolicy: {{ .Values.agent.pullPolicy }}
        args:
          {{- range $key, $value := .Values.agent.cmdlineParams }}
          {{- if $value }}
          - --{{ $key }}={{ $value }}
          {{- else }}
          - --{{ $key }}
          {{- end }}
          {{- end }}
        env:
        {{- if not (hasKey .Values.agent.cmdlineParams "reporter.grpc.host-port") }}
        - name: REPORTER_GRPC_HOST_PORT
          value: {{ include "jaeger.collector.name" . }}:{{ .Values.collector.service.grpc.port }}
        {{- end }}
        ports:
        - name: admin
          containerPort: 14271
          protocol: TCP
        volumeMounts:
        {{- range .Values.agent.extraConfigmapMounts }}
          - name: {{ .name }}
            mountPath: {{ .mountPath }}
            subPath: {{ .subPath }}
            readOnly: {{ .readOnly }}
        {{- end }}
        {{- range .Values.agent.extraSecretMounts }}
          - name: {{ .name }}
            mountPath: {{ .mountPath }}
            subPath: {{ .subPath }}
            readOnly: {{ .readOnly }}
        {{- end }}
        livenessProbe:
          httpGet:
            path: /
            port: admin
        readinessProbe:
          httpGet:
            path: /
            port: admin
{{- end }}
      dnsPolicy: {{ .Values.query.dnsPolicy }}
      restartPolicy: Always
      volumes:
      {{- range .Values.query.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
      {{- end }}
      {{- range .Values.query.extraSecretMounts }}
        - name: {{ .name }}
          secret:
            secretName: {{ .secretName }}
      {{- end }}
      {{- if .Values.query.config}}
        - name: ui-configuration
          configMap:
            name: {{ include "jaeger.fullname" . }}-ui-configuration
      {{- end }}
{{- if .Values.query.agentSidecar.enabled }}
      {{- range .Values.agent.extraSecretMounts }}
        - name: {{ .name }}
          secret:
            secretName: {{ .secretName }}
      {{- end }}
      {{- range .Values.agent.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
      {{- end }}
{{- end }}
    {{- with .Values.query.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.query.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.query.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
{{- end -}}
