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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name:  {{ include "jaeger.collector.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: jaeger
    app.kubernetes.io/component: collector
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.collector.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
  replicas: {{ .Values.collector.replicaCount }}
  {{- if .Values.collector.updateStrategy }}
  strategy: {{- toYaml .Values.collector.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: jaeger
        app.kubernetes.io/component: collector
      {{- if .Values.collector.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.collector.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "jaeger.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.collector.automountServiceAccountToken }}
      {{- if .Values.collector.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.collector.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.collector.schedulerName }}
      schedulerName: {{ .Values.collector.schedulerName }}
      {{- end }}
      priorityClassName: {{ .Values.collector.priorityClassName | quote }}
      {{- if .Values.collector.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.collector.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.collector.podAffinityPreset "component" "collector" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.collector.podAntiAffinityPreset "component" "collector" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.collector.nodeAffinityPreset.type "key" .Values.collector.nodeAffinityPreset.key "values" .Values.collector.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.collector.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.collector.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.collector.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.collector.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.collector.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.collector.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.collector.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.collector.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
      {{- include "jaeger.waitForDBInitContainer" (dict "component" "collector" "context" $) | nindent 8 }}
      {{- if .Values.collector.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.collector.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: jaeger-collector
          image: {{ include "jaeger.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.collector.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.collector.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.collector.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.collector.command "context" $) | nindent 12 }}
          {{- else }}
          command: ["/opt/bitnami/jaeger/bin/jaeger-collector"]
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.collector.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.collector.args "context" $) | nindent 12 }}
          {{- else }}
          args: ["--cassandra.basic.allowed-authenticators=org.apache.cassandra.auth.PasswordAuthenticator"]
          {{- end }}
          {{- if .Values.collector.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.collector.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - name: zipkin
              containerPort: {{ .Values.collector.containerPorts.zipkin }}
            - name: binary
              containerPort: {{ .Values.collector.containerPorts.binary }}
            - name: grpc
              containerPort: {{ .Values.collector.containerPorts.grpc }}
            - name: admin
              containerPort: {{ .Values.collector.containerPorts.admin }}
            - name: grpc-otlp
              containerPort: {{ .Values.collector.containerPorts.otlp.grpc }}
            - name: http-otlp
              containerPort: {{ .Values.collector.containerPorts.otlp.http }}
          env:
            - name: CASSANDRA_SERVERS
              value: {{ include "jaeger.cassandra.host" . }}
            - name: CASSANDRA_PORT
              value: {{ include "jaeger.cassandra.port" . }}
            - name: CASSANDRA_USERNAME
              value: {{ include "jaeger.cassandra.user" . }}
            - name: CASSANDRA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "jaeger.cassandra.secretName" . }}
                  key: {{ include "jaeger.cassandra.secretKey" . }}
            - name: CASSANDRA_KEYSPACE
              value: {{ include "jaeger.cassandra.keyspace" . }}
            - name: CASSANDRA_DATACENTER
              value: {{ include "jaeger.cassandra.datacenter" . }}
            - name: ADMIN_HTTP_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.admin | quote }}
            - name: COLLECTOR_ZIPKIN_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.zipkin | quote }}
            - name: COLLECTOR_GRPC_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.grpc | quote }}
            - name: COLLECTOR_HTTP_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.binary | quote }}
            - name: COLLECTOR_OTLP_GRPC_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.otlp.grpc | quote }}
            - name: COLLECTOR_OTLP_HTTP_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.otlp.http | quote }}
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            {{- if .Values.collector.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.collector.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.collector.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.collector.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.collector.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.collector.extraEnvVarsSecret }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.collector.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.collector.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.collector.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.collector.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: admin
          {{- end }}
          {{- if .Values.collector.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.collector.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.collector.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.collector.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: admin
          {{- end }}
          {{- if .Values.collector.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.collector.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.collector.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.collector.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: admin
          {{- end }}
          {{- end }}
          {{- if .Values.query.resources }}
          resources: {{- toYaml .Values.collector.resources | nindent 12 }}
          {{- else if ne .Values.collector.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.collector.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.collector.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" ( dict "value" .Values.collector.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.collector.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.collector.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.collector.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.collector.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
