apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name:  {{ include "jaeger.agent.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/part-of: jaeger
    app.kubernetes.io/component: agent
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.agent.replicaCount }}
  {{- if .Values.agent.updateStrategy }}
  strategy: {{- toYaml .Values.agent.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/part-of: jaeger
        app.kubernetes.io/component: agent
        {{- if .Values.agent.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.agent.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.agent.podAnnotations }}
      annotations:
        {{- if .Values.agent.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.agent.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- if .Values.agent.schedulerName }}
      schedulerName: {{ .Values.agent.schedulerName }}
      {{- end }}
      priorityClassName: {{ .Values.agent.priorityClassName | quote }}
      {{- if .Values.agent.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.agent.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.agent.podAffinityPreset "component" "agent" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.agent.podAntiAffinityPreset "component" "agent" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.agent.nodeAffinityPreset.type "key" .Values.agent.nodeAffinityPreset.key "values" .Values.agent.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.agent.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.agent.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.agent.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.agent.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.agent.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
      {{- include "jaeger.waitForDBInitContainer" . | nindent 8 }}
      {{- if .Values.agent.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.agent.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: jaeger-agent
          image: {{ include "jaeger.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.agent.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.agent.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.agent.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.agent.command "context" $) | nindent 12 }}
          {{- else }}
          command: ["/opt/bitnami/jaeger/bin/jaeger-agent"]
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.agent.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.agent.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.agent.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.agent.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - name: binary
              containerPort: {{ .Values.agent.containerPorts.binary }}
            - name: zipkin
              containerPort: {{ .Values.agent.containerPorts.zipkin }}
            - name: admin
              containerPort: {{ .Values.agent.containerPorts.admin }}
            - name: compact
              containerPort: {{ .Values.agent.containerPorts.compact }}
            - name: config
              containerPort: {{ .Values.agent.containerPorts.config }}
          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: HTTP_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.agent.containerPorts.config | quote }}
            - name: PROCESSOR_JAEGER_BINARY_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.agent.containerPorts.binary | quote }}
            - name: PROCESSOR_JAEGER_COMPACT_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.agent.containerPorts.compact | quote }}
            - name: PROCESSOR_ZIPKIN_COMPACT_SERVER_HOST_PORT
              value: {{ printf ":%v" .Values.agent.containerPorts.zipkin | quote }}
            - name: ADMIN_HTTP_HOST_PORT
              value: {{ printf ":%v" .Values.agent.containerPorts.admin | quote }}
            - name: REPORTER_GRPC_HOST_PORT
              value: {{ printf "%s:%d" (include "jaeger.collector.fullname" .) (int .Values.collector.service.ports.grpc) }}
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            {{- if .Values.agent.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.agent.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.agent.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.agent.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.agent.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.agent.extraEnvVarsSecret }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.agent.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.agent.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.agent.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.agent.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: admin
          {{- end }}
          {{- if .Values.agent.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.agent.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.agent.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.agent.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: admin
          {{- end }}
          {{- if .Values.agent.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.agent.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.agent.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.agent.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: admin
          {{- end }}
          {{- end }}
          {{- if .Values.query.resources }}
          resources: {{- toYaml .Values.agent.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.agent.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.agent.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.agent.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.agent.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.agent.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.agent.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
