{{- define "container-agent" -}}
- name: agent
  image: "{{ include "image-path" (dict "root" .Values "image" .Values.agents.image) }}"
  imagePullPolicy: {{ .Values.agents.image.pullPolicy }}
  command: ["agent", "run"]
  {{- if .Values.agents.containers.agent.securityContext }}
  securityContext:
    {{ toYaml .Values.agents.containers.agent.securityContext | nindent 4 }}
  {{- end }}
  resources:
{{ toYaml .Values.agents.containers.agent.resources | indent 4 }}
  ports:
  - containerPort: {{ .Values.datadog.dogstatsd.port }}
    {{- if .Values.datadog.dogstatsd.useHostPort }}
    hostPort: {{ .Values.datadog.dogstatsd.port }}
    {{- end }}
    name: dogstatsdport
    protocol: UDP
{{- if .Values.agents.containers.agent.ports }}
{{ toYaml .Values.agents.containers.agent.ports | indent 2 }}
{{- end }}
{{- if or .Values.datadog.envFrom .Values.agents.containers.agent.envFrom }}
  envFrom:
{{ concat .Values.datadog.envFrom .Values.agents.containers.agent.envFrom | toYaml | indent 4 }}
{{- end }}
  env:
    {{- include "containers-common-env" . | nindent 4 }}
    {{- if .Values.datadog.logLevel }}
    - name: DD_LOG_LEVEL
      value: {{ .Values.agents.containers.agent.logLevel | default .Values.datadog.logLevel | quote }}
    {{- end }}
    {{- if .Values.datadog.dogstatsd.port }}
    - name: DD_DOGSTATSD_PORT
      value: {{ .Values.datadog.dogstatsd.port | quote }}
    {{- end }}
    {{- if .Values.datadog.dogstatsd.nonLocalTraffic }}
    - name: DD_DOGSTATSD_NON_LOCAL_TRAFFIC
      value: {{ .Values.datadog.dogstatsd.nonLocalTraffic | quote }}
    {{- end }}
    {{- if .Values.datadog.dogstatsd.originDetection }}
    - name: DD_DOGSTATSD_ORIGIN_DETECTION
      value: {{ .Values.datadog.dogstatsd.originDetection | quote }}
    - name: DD_DOGSTATSD_TAG_CARDINALITY
      value: {{ .Values.datadog.dogstatsd.tagCardinality | quote }}
    {{- end }}
    {{- if .Values.datadog.dogstatsd.tags }}
    - name: DD_DOGSTATSD_TAGS
      value: {{ tpl (.Values.datadog.dogstatsd.tags | join " " | quote) . }}
    {{- end }}
    {{- if eq (include "cluster-agent-enabled" .) "false" }}
    {{- if .Values.datadog.leaderElection }}
    - name: DD_LEADER_ELECTION
      value: {{ .Values.datadog.leaderElection | quote}}
    {{- end }}
    {{- if .Values.datadog.leaderLeaseDuration }}
    - name: DD_LEADER_LEASE_DURATION
      value: {{ .Values.datadog.leaderLeaseDuration | quote }}
    {{- end }}
    {{- if .Values.datadog.collectEvents }}
    - name: DD_COLLECT_KUBERNETES_EVENTS
      value: {{.Values.datadog.collectEvents | quote}}
    {{- end }}
    {{- else }}
    {{- include "containers-cluster-agent-env" . | nindent 4 }}
    {{- end }}
    - name: DD_APM_ENABLED
      value: "false"
    - name: DD_LOGS_ENABLED
      value: {{  (default false (or .Values.datadog.logs.enabled .Values.datadog.logsEnabled)) | quote}}
    - name: DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL
      value: {{  (default false (or .Values.datadog.logs.containerCollectAll .Values.datadog.logsConfigContainerCollectAll)) | quote}}
    - name: DD_LOGS_CONFIG_K8S_CONTAINER_USE_FILE
      value: {{ .Values.datadog.logs.containerCollectUsingFiles | quote }}
    - name: DD_HEALTH_PORT
    {{- $healthPort := .Values.agents.containers.agent.healthPort }}
      value: {{ $healthPort | quote }}
    {{- if eq .Values.targetSystem "linux" }}
    - name: DD_DOGSTATSD_SOCKET
      value: {{ .Values.datadog.dogstatsd.socketPath | quote }}
    {{- end }}
    {{- if and (eq (include "cluster-agent-enabled" .) "true") .Values.datadog.clusterChecks.enabled }}
    {{- if or (and (not .Values.existingClusterAgent.join) .Values.clusterChecksRunner.enabled) (and .Values.existingClusterAgent.join (not .Values.existingClusterAgent.clusterchecksEnabled)) }}
    - name: DD_EXTRA_CONFIG_PROVIDERS
      value: "endpointschecks"
    {{ else }}
    - name: DD_EXTRA_CONFIG_PROVIDERS
      value: "clusterchecks endpointschecks"
    {{- end }}
    {{- end }}
    {{- if .Values.datadog.prometheusScrape.enabled }}
    - name: DD_PROMETHEUS_SCRAPE_ENABLED
      value: "true"
    {{- if .Values.datadog.prometheusScrape.additionalConfigs }}
    - name: DD_PROMETHEUS_SCRAPE_CHECKS
      value: {{ .Values.datadog.prometheusScrape.additionalConfigs | toJson | quote }}
    {{- end }}
    {{- end }}
    {{- $ignoreAutoConfig := .Values.datadog.ignoreAutoConfig }}
    {{- if and .Values.datadog.kubeStateMetricsCore.enabled .Values.datadog.kubeStateMetricsCore.ignoreLegacyKSMCheck }}
      {{- $ignoreAutoConfig = append $ignoreAutoConfig "kubernetes_state" }}
    {{- end }}
    {{- if not (empty $ignoreAutoConfig) }}
    - name: DD_IGNORE_AUTOCONF
      value: {{ join " " $ignoreAutoConfig | quote }}
    {{- end }}
    {{- if .Values.datadog.checksCardinality }}
    - name: DD_CHECKS_TAG_CARDINALITY
      value: {{ .Values.datadog.checksCardinality | quote }}
    {{- end }}
{{- if .Values.agents.containers.agent.env }}
{{ toYaml .Values.agents.containers.agent.env | indent 4 }}
{{- end }}
  volumeMounts:
    {{- if eq .Values.targetSystem "linux" }}
    - name: installinfo
      subPath: install_info
      mountPath: /etc/datadog-agent/install_info
      readOnly: true
    - name: logdatadog
      mountPath: /var/log/datadog
    - name: tmpdir
      mountPath: /tmp
      readOnly: false
    {{- end }}
    - name: config
      mountPath: {{ template "datadog.confPath" . }}
    {{- include "container-crisocket-volumemounts" . | nindent 4 }}
    {{- if .Values.agents.useConfigMap }}
    - name: {{ template "datadog.fullname" . }}-datadog-yaml
      mountPath: {{ template "datadog.confPath" . }}/datadog.yaml
      subPath: datadog.yaml
    {{- end }}
    {{- if eq .Values.targetSystem "linux" }}
    - name: dsdsocket
      mountPath: {{ (dir .Values.datadog.dogstatsd.socketPath) }}
    {{- if eq (include "should-enable-system-probe" .) "true" }}
    - name: sysprobe-socket-dir
      mountPath: /var/run/sysprobe
      readOnly: true
    - name: sysprobe-config
      mountPath: /etc/datadog-agent/system-probe.yaml
      subPath: system-probe.yaml
    {{- end }}
    - name: procdir
      mountPath: /host/proc
      mountPropagation: {{ .Values.datadog.hostVolumeMountPropagation }}
      readOnly: true
    - name: cgroups
      mountPath: /host/sys/fs/cgroup
      mountPropagation: {{ .Values.datadog.hostVolumeMountPropagation }}
      readOnly: true
    {{- if or .Values.datadog.logs.enabled .Values.datadog.logsEnabled }}
    - name: pointerdir
      mountPath: /opt/datadog-agent/run
      mountPropagation: {{ .Values.datadog.hostVolumeMountPropagation }}
    - name: logpodpath
      mountPath: /var/log/pods
      mountPropagation: {{ .Values.datadog.hostVolumeMountPropagation }}
      readOnly: true
    - name: logscontainerspath
      mountPath: /var/log/containers
      mountPropagation: {{ .Values.datadog.hostVolumeMountPropagation }}
      readOnly: true
    {{- if not .Values.datadog.criSocketPath }}
    - name: logdockercontainerpath
      mountPath: /var/lib/docker/containers
      mountPropagation: {{ .Values.datadog.hostVolumeMountPropagation }}
      readOnly: true
    {{- end }}
    {{- end }}
    {{- end }}
    {{- if eq .Values.targetSystem "windows" }}
    {{- if or .Values.datadog.logs.enabled .Values.datadog.logsEnabled }}
    - name: pointerdir
      mountPath: C:/var/log
    - name: logpodpath
      mountPath: C:/var/log/pods
      readOnly: true
    - name: logdockercontainerpath
      mountPath: C:/ProgramData
      readOnly: true
    {{- end }}
    {{- end }}
    {{- if .Values.datadog.kubelet.hostCAPath }}
{{ include "datadog.kubelet.volumeMount" . | indent 4 }}
    {{- end }}
{{- if .Values.agents.volumeMounts }}
{{ toYaml .Values.agents.volumeMounts | indent 4 }}
{{- end }}
  livenessProbe:
{{- $live := .Values.agents.containers.agent.livenessProbe }}
{{ include "probe.http" (dict "path" "/live" "port" $healthPort "settings" $live) | indent 4 }}
  readinessProbe:
{{- $ready := .Values.agents.containers.agent.readinessProbe }}
{{ include "probe.http" (dict "path" "/ready" "port" $healthPort "settings" $ready) | indent 4 }}
{{- end -}}
