{{- if .Values.daemonset.enabled }}
{{- if (or (.Values.datadog.apiKeyExistingSecret) (.Values.datadog.apiKey)) }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "datadog.fullname" . }}
  labels:
    app: "{{ template "datadog.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  selector:
    matchLabels:
      app: {{ template "datadog.fullname" . }}
  template:
    metadata:
      labels:
        app: {{ template "datadog.fullname" . }}
        {{- if .Values.daemonset.podLabels }}
{{ toYaml .Values.daemonset.podLabels | indent 8 }}
        {{- end }}
      name: {{ template "datadog.fullname" . }}
      annotations:
        checksum/autoconf-config: {{ tpl (toYaml .Values.datadog.autoconf) . | sha256sum }}
        checksum/confd-config: {{ tpl (toYaml .Values.datadog.confd) . | sha256sum }}
        checksum/checksd-config: {{ tpl (toYaml .Values.datadog.checksd) . | sha256sum }}
      {{- if .Values.daemonset.podAnnotations }}
{{ toYaml .Values.daemonset.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.datadog.securityContext }}
      securityContext:
{{ toYaml .Values.datadog.securityContext | indent 8 }}
      {{- end }}
      {{- if .Values.daemonset.useHostNetwork }}
      hostNetwork: {{ .Values.daemonset.useHostNetwork }}
      dnsPolicy: ClusterFirstWithHostNet
      {{- end }}
      {{- if .Values.daemonset.useHostPID }}
      hostPID: {{ .Values.daemonset.useHostPID }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
      {{- end }}
      {{- if .Values.daemonset.priorityClassName }}
      priorityClassName: {{ .Values.daemonset.priorityClassName }}
      {{- end }}
      containers:
      {{- if .Values.daemonset.useDedicatedContainers }}
        {{- include "container-agent" . | nindent 8 }}
        {{- if .Values.datadog.apmEnabled }}
          {{- include "container-trace-agent" . | nindent 8 }}
        {{- end }}
        {{- if .Values.datadog.processAgentEnabled }}
          {{- include "container-process-agent" . | nindent 8 }}
        {{- end }}
      initContainers:
        {{ include "containers-init" . | nindent 8 }}
      {{- else }}
        {{- include "container-agents" . | nindent 8 }}
      {{- end}}
      volumes:
        {{- if .Values.daemonset.useDedicatedContainers }}
        - name: config
          emptyDir: {}
        {{- end }}
        {{- if .Values.datadog.useCriSocketVolume }}
        - hostPath:
            path: {{ default "/var/run/docker.sock" .Values.datadog.criSocketPath | quote }}
          name: runtimesocket
        {{- end }}
        {{- if .Values.datadog.useDogStatsDSocketVolume }}
        - hostPath:
            path: "/var/run/datadog/"
          name: dsdsocket
        {{- end }}
        - hostPath:
            path: /proc
          name: procdir
        - hostPath:
            path: /sys/fs/cgroup
          name: cgroups
        - name: s6-run
          emptyDir: {}
        {{- if (or (.Values.datadog.confd) (.Values.datadog.autoconf)) }}
        - name: confd
          configMap:
            name: {{ template "datadog.fullname" . }}-confd
        {{- end }}
        {{- if .Values.datadog.checksd }}
        - name: checksd
          configMap:
            name: {{ template "datadog.fullname" . }}-checksd
        {{- end }}
        {{- if .Values.datadog.logsEnabled }}
        - hostPath:
            path: {{ default "/var/lib/datadog-agent/logs" .Values.datadog.logsPointerHostPath | quote }}
          name: pointerdir
        - hostPath:
            path: /var/log/pods
          name: logpodpath
        {{- if .Values.datadog.containerLogsPath }}
        - hostPath:
            path: {{ .Values.datadog.containerLogsPath | quote }}
          name: logcontainerpath
        {{- end }}
        {{- end }}
        {{- if .Values.datadog.processAgentEnabled }}
        - hostPath:
            path: /etc/passwd
          name: passwd
        {{- end }}
        {{- if .Values.daemonset.useConfigMap }}
        - name: {{ template "datadog.fullname" . }}-datadog-yaml
          configMap:
            name: {{ template "datadog.fullname" . }}-datadog-yaml
        {{- end }}
{{- if .Values.datadog.volumes }}
{{ toYaml .Values.datadog.volumes | indent 8 }}
{{- end }}
      {{- if .Values.daemonset.tolerations }}
      tolerations:
      {{- if .Values.daemonset.rkeETCDControlPlane }}
      - key: "node-role.kubernetes.io/etcd"
        value: "true"
      - key: "node-role.kubernetes.io/controlplane"
        value: "true"
      {{- end }}
{{ toYaml .Values.daemonset.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.daemonset.affinity }}
      affinity:
{{ toYaml .Values.daemonset.affinity | indent 8 }}
      {{- end }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "datadog.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
      {{- if .Values.daemonset.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.daemonset.nodeSelector | indent 8 }}
      {{- end }}
  updateStrategy:
    type: {{ default "RollingUpdate" .Values.daemonset.updateStrategy | quote }}
{{ end }}
{{ end }}
