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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "grafana-tempo.distributor.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: distributor
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.distributor.replicaCount }}
  {{- if .Values.distributor.updateStrategy }}
  strategy: {{- toYaml .Values.distributor.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.distributor.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: distributor
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/tempo-configmap.yaml") . | sha256sum }}
        {{- if .Values.distributor.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.distributor.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: distributor
        tempo-gossip-member: "true"
    spec:
      serviceAccountName: {{ template "grafana-tempo.serviceAccountName" . }}
      {{- include "grafana-tempo.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.distributor.automountServiceAccountToken }}
      {{- if .Values.distributor.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.distributor.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.distributor.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.distributor.podAffinityPreset "component" "distributor" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.distributor.podAntiAffinityPreset "component" "distributor" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.distributor.nodeAffinityPreset.type "key" .Values.distributor.nodeAffinityPreset.key "values" .Values.distributor.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.distributor.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.distributor.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.distributor.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.distributor.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.distributor.priorityClassName }}
      priorityClassName: {{ .Values.distributor.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.distributor.schedulerName }}
      schedulerName: {{ .Values.distributor.schedulerName }}
      {{- end }}
      {{- if .Values.distributor.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.distributor.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      enableServiceLinks: {{ .Values.distributor.enableServiceLinks }}
      initContainers:
        {{- if .Values.distributor.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.distributor.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: grafana-tempo-distributor
          image: {{ template "grafana-tempo.image" . }}
          imagePullPolicy: {{ .Values.tempo.image.pullPolicy }}
          {{- if .Values.distributor.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.distributor.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.distributor.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.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.distributor.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -target=distributor
            - -config.file=/bitnami/grafana-tempo/conf/tempo.yaml
            - -mem-ballast-size-mbs={{ .Values.tempo.memBallastSizeMbs }}
          {{- end }}
          {{- if .Values.distributor.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.distributor.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.distributor.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.distributor.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.distributor.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.tempo.containerPorts.web }}
              name: http
            - containerPort: {{ .Values.tempo.containerPorts.gossipRing }}
              name: http-memberlist
            - containerPort: {{ .Values.tempo.containerPorts.grpc }}
              name: grpc
            {{- if .Values.tempo.traces.jaeger.thriftCompact }}
            - containerPort: 6831
              name: jaeger-compact
              protocol: UDP
            {{- end }}
            {{- if .Values.tempo.traces.jaeger.thriftBinary }}
            - containerPort: 6832
              name: jaeger-binary
              protocol: UDP
            {{- end }}
            {{- if .Values.tempo.traces.jaeger.thriftHttp }}
            - containerPort: 14268
              name: jaeger-http
              protocol: TCP
            {{- end }}
            {{- if .Values.tempo.traces.jaeger.grpc }}
            - containerPort: 14250
              name: jaeger-grpc
              protocol: TCP
            {{- end }}
            {{- if .Values.tempo.traces.zipkin }}
            - containerPort: 9411
              name: zipkin
              protocol: TCP
            {{- end }}
            {{- if .Values.tempo.traces.otlp.http }}
            - containerPort: 4318
              name: otlp-http
              protocol: TCP
            {{- end }}
            {{- if .Values.tempo.traces.otlp.grpc }}
            - containerPort: 4317
              name: otlp-grpc
              protocol: TCP
            {{- end }}
            {{- if .Values.tempo.traces.opencensus }}
            - containerPort: 55678
              name: opencensus
              protocol: TCP
            {{- end }}
          {{- if .Values.distributor.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.distributor.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.distributor.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.distributor.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.distributor.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.distributor.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ .Values.tempo.containerPorts.web }}
          {{- end }}
          {{- if .Values.distributor.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.distributor.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.distributor.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: {{ .Values.tempo.containerPorts.web }}
          {{- end }}
          {{- if .Values.distributor.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.distributor.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.distributor.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.distributor.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
            # NOTE: The upstream chart does not create a PVC for this, we assume that it is just for tmp data
            - name: empty-dir
              mountPath: /bitnami/grafana-tempo/data
              subPath: app-data-dir
            - name: tempo-config
              mountPath: /bitnami/grafana-tempo/conf/tempo.yaml
              subPath: tempo.yaml
            - name: overrides-config
              mountPath: /bitnami/grafana-tempo/conf/overrides.yaml
              subPath: overrides.yaml
          {{- if .Values.distributor.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.distributor.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.distributor.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.distributor.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: tempo-config
          configMap:
            name: {{ template "grafana-tempo.tempoConfigmapName" . }}
        - name: overrides-config
          configMap:
            name: {{ template "grafana-tempo.overridesConfigmapName" . }}
        {{- if .Values.distributor.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.distributor.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
