{{- if .Values.query.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "jaeger.query.name" . }}
  labels:
    app.kubernetes.io/name: {{ include "jaeger.name" . }}
    helm.sh/chart: {{ include "jaeger.chart" . }}
    app.kubernetes.io/component: query
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.query.annotations }}
  annotations:
{{ toYaml .Values.query.annotations | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.query.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "jaeger.name" . }}
      app.kubernetes.io/component: query
      app.kubernetes.io/instance: {{ .Release.Name }}
  strategy:
    type: Recreate
  template:
    metadata:
{{- if .Values.query.podAnnotations }}
      annotations:
{{ toYaml .Values.query.podAnnotations | indent 8 }}
{{- end }}
      labels:
        app.kubernetes.io/name: {{ include "jaeger.name" . }}
        app.kubernetes.io/component: query
        app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.query.podLabels }}
{{ toYaml .Values.query.podLabels | indent 8 }}
{{- end }}
    spec:
      nodeSelector:
{{ toYaml .Values.query.nodeSelector | indent 8 }}
{{- if .Values.query.tolerations }}
      tolerations:
{{ toYaml .Values.query.tolerations | indent 8 }}
{{- end }}
      serviceAccountName: {{ template "jaeger.query.serviceAccountName" . }}
      containers:
      - name: {{ template "jaeger.query.name" . }}
        image: {{ .Values.query.image }}:{{ .Values.tag }}
        imagePullPolicy: {{ .Values.query.pullPolicy }}
        env:
          {{- range $key, $value := .Values.query.cmdlineParams }}
          - name: {{ $key | replace "." "_" | replace "-" "_" | upper | quote }}
            value: {{ $value | quote }}
          {{- end }}
          - name: SPAN_STORAGE_TYPE
            value: {{ .Values.storage.type }}
          {{- if eq .Values.storage.type "cassandra" }}
          - name: CASSANDRA_SERVERS
            value: {{ template "cassandra.host" . }}
          - name: CASSANDRA_PORT
            value: {{ .Values.storage.cassandra.port | quote }}
          {{ if .Values.storage.cassandra.tls.enabled }}
          - name: CASSANDRA_TLS
            value: "true"
          - name: CASSANDRA_TLS_SERVER_NAME
            valueFrom:
              secretKeyRef:
                name: {{ .Values.storage.cassandra.tls.secretName }}
                key: commonName
          - name: CASSANDRA_TLS_KEY
            value: "/cassandra-tls/client-key.pem"
          - name: CASSANDRA_TLS_CERT
            value: "/cassandra-tls/client-cert.pem"
          - name: CASSANDRA_TLS_CA
            value: "/cassandra-tls/ca-cert.pem"
          {{- end }}    
          - name: CASSANDRA_KEYSPACE
            value: {{ printf "%s_%s" "jaeger_v1" .Values.cassandra.config.dc_name | quote }}
          - name: CASSANDRA_USERNAME
            value: {{ .Values.storage.cassandra.user }}
          - name: CASSANDRA_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ if .Values.storage.cassandra.existingSecret }}{{ .Values.storage.cassandra.existingSecret }}{{- else }}{{ include "jaeger.fullname" . }}-cassandra{{- end }}
                key: password
          {{- end }}
          {{- if eq .Values.storage.type "elasticsearch" }}
          {{- if .Values.storage.elasticsearch.usePassword }}
          - name: ES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ if .Values.storage.elasticsearch.existingSecret }}{{ .Values.storage.elasticsearch.existingSecret }}{{- else }}{{ include "jaeger.fullname" . }}-elasticsearch{{- end }}
                key: password
          {{- end }}
          - name: ES_SERVER_URLS
            value: {{ template "elasticsearch.client.url" . }}
          - name: ES_USERNAME
            value: {{ .Values.storage.elasticsearch.user }}
          {{- end }}
          - name: QUERY_BASE_PATH
            value: {{ .Values.query.basePath | quote }}
        ports:
        - name: query
          containerPort: 16686
          protocol: TCP
        - name: admin
          containerPort: 16687
          protocol: TCP
        resources:
{{ toYaml .Values.query.resources | indent 10 }}
        volumeMounts:
        {{- 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 }}
        livenessProbe:
          httpGet:
            path: /
            port: admin
        readinessProbe:
          httpGet:
            path: /
            port: admin
{{- if .Values.query.agentSidecar.enabled }}
      - name: {{ template "jaeger.agent.name" . }}-sidecar
        image: {{ .Values.agent.image }}:{{ .Values.tag }}
        imagePullPolicy: {{ .Values.agent.pullPolicy }}
        env:
        - name: REPORTER_GRPC_HOST_PORT
          value: {{ include "jaeger.collector.name" . }}:{{ .Values.collector.service.grpcPort }}
        ports:
        - name: admin
          containerPort: 14271
          protocol: TCP
        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 }}
{{- end -}}
