{{- /*
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 or .Values.collector.podAnnotations (not .Values.collector.existingConfigmap) }}
      annotations:
        {{- if not .Values.collector.existingConfigmap }}
        checksum/configmap: {{ include (print $.Template.BasePath "/collector/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.collector.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.collector.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- 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 }}
          {{- 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:
            - "--config"
            - "/opt/bitnami/jaeger/conf/collector-config.yaml"
          {{- end }}
          {{- if .Values.collector.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.collector.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            {{- if has "otlp" .Values.collector.receivers }}
            - name: grpc-otlp
              containerPort: {{ .Values.collector.containerPorts.otlp.grpc }}
            - name: http-otlp
              containerPort: {{ .Values.collector.containerPorts.otlp.http }}
            {{- end }}
            {{- if has "jaeger" .Values.collector.receivers }}
            - name: grpc-jaeger
              containerPort: {{ .Values.collector.containerPorts.jaeger.grpc }}
            - name: http-jaeger-tf
              containerPort: {{ .Values.collector.containerPorts.jaeger.thrift_http }}
            {{- end }}
            {{- if has "zipkin" .Values.collector.receivers }}
            - name: zipkin
              containerPort: {{ .Values.collector.containerPorts.zipkin }}
            {{- end }}
            - name: metrics
              containerPort: {{ .Values.collector.containerPorts.metrics }}
            - name: healthcheck
              containerPort: {{ .Values.collector.containerPorts.healthcheck }}
          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" . }}
            {{- if has "otlp" .Values.collector.receivers }}
            - 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 }}
            {{- end }}
            {{- if has "jaeger" .Values.collector.receivers }}
            - name: COLLECTOR_JAEGER_GRPC_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.jaeger.grpc | quote }}
            - name: COLLECTOR_JAEGER_THRIFT_HTTP_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.jaeger.thrift_http | quote }}
            {{- end }}
            {{- if has "zipkin" .Values.collector.receivers }}
            - name: COLLECTOR_ZIPKIN_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.zipkin | quote }}
            {{- end }}
            - name: COLLECTOR_HEALTHCHECK_HOST_PORT
              value: {{ printf ":%v" .Values.collector.containerPorts.healthcheck | quote }}
            - name: COLLECTOR_METRICS_PORT
              value: {{ .Values.collector.containerPorts.metrics | 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: /status
              port: healthcheck
          {{- 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 }}
            httpGet:
              path: /status
              port: healthcheck
          {{- 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: /status
              port: healthcheck
          {{- 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 }}
          volumeMounts:
            - name: configuration
              mountPath: /opt/bitnami/jaeger/conf
            {{- if .Values.collector.extraVolumeMounts }}
            {{- 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:
        - name: configuration
          configMap:
            name: {{ include "jaeger.collector.configMapName"  . }}
        {{- if .Values.collector.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.collector.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
