{{- template "check-version" . }}
{{- if .Values.agents.enabled }}
{{- if (or (.Values.datadog.apiKeyExistingSecret) (.Values.datadog.apiKey)) }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "datadog.fullname" . }}
  labels:
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    app.kubernetes.io/name: "{{ template "datadog.fullname" . }}"
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
spec:
  selector:
    matchLabels:
      app: {{ template "datadog.fullname" . }}
        {{- if .Values.agents.podLabels }}
{{ toYaml .Values.agents.podLabels | indent 6 }}
        {{- end }}
  template:
    metadata:
      labels:
        app: {{ template "datadog.fullname" . }}
        {{- if .Values.agents.podLabels }}
{{ toYaml .Values.agents.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.agents.customAgentConfig }}
        checksum/agent-config: {{ tpl (toYaml .Values.agents.customAgentConfig) . | sha256sum }}
        {{- end }}
        {{- if .Values.datadog.systemProbe.enabled }}
        container.apparmor.security.beta.kubernetes.io/system-probe: {{ .Values.datadog.systemProbe.apparmor }}
        container.seccomp.security.alpha.kubernetes.io/system-probe: {{ .Values.datadog.systemProbe.seccomp }}
        {{- end }}
      {{- if .Values.agents.podAnnotations }}
{{ toYaml .Values.agents.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.datadog.securityContext }}
      securityContext:
{{ toYaml .Values.datadog.securityContext| indent 8 }}
      {{- else if or .Values.agents.podSecurity.podSecurityPolicy.create .Values.agents.podSecurity.securityContextConstraints.create -}}
      {{- if and (.Values.agents.podSecurity.securityContext) .Values.agents.podSecurity.securityContext.seLinuxOptions }}
      securityContext:
        seLinuxOptions:
{{ toYaml .Values.agents.podSecurity.securityContext.seLinuxOptions | indent 10 }}
      {{- end }}
      {{- end }}
      {{- if .Values.agents.useHostNetwork }}
      hostNetwork: {{ .Values.agents.useHostNetwork }}
      dnsPolicy: ClusterFirstWithHostNet
      {{- end }}
      {{- if .Values.agents.dnsConfig }}
      dnsConfig:
{{ toYaml .Values.agents.dnsConfig | indent 8 }}
      {{- end }}
      {{- if .Values.datadog.dogstatsd.useHostPID }}
      hostPID: {{ .Values.datadog.dogstatsd.useHostPID }}
      {{- end }}
      {{- if .Values.agents.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.agents.image.pullSecrets | indent 8 }}
      {{- end }}
      {{- if .Values.agents.priorityClassName }}
      priorityClassName: {{ .Values.agents.priorityClassName }}
      {{- end }}
      containers:
        {{- include "container-agent" . | nindent 6 }}
        {{- if .Values.datadog.apm.enabled }}
          {{- include "container-trace-agent" . | nindent 6 }}
        {{- end }}
        {{- if .Values.datadog.processAgent.enabled }}
          {{- include "container-process-agent" . | nindent 6 }}
        {{- end }}
        {{- if .Values.datadog.systemProbe.enabled }}
          {{- include "container-system-probe" . | nindent 6 }}
        {{- end }}
      initContainers:
        {{- if eq .Values.targetSystem "windows" }}
          {{ include "containers-init-windows" . | nindent 6 }}
        {{- end }}
        {{- if eq .Values.targetSystem "linux" }}
          {{ include "containers-init-linux" . | nindent 6 }}
        {{- end }}
        {{- if and .Values.datadog.systemProbe.enabled (eq .Values.datadog.systemProbe.seccomp "localhost/system-probe") }}
          {{ include "system-probe-init" . | nindent 6 }}
        {{- end }}
      volumes:
      - name: config
        emptyDir: {}
      {{- if eq .Values.targetSystem "linux" }}
      - hostPath:
          path: {{ dir (include "datadog.dockerOrCriSocketPath" .) }}
        name: runtimesocketdir
      {{- end }}
      {{- if eq .Values.targetSystem "windows" }}
      - hostPath:
          path: {{ template "datadog.dockerOrCriSocketPath" . }}
        name: runtimesocket
      {{- end }}
      {{- if .Values.datadog.checksd }}
      - name: checksd
        configMap:
          name: {{ template "datadog.fullname" . }}-checksd
      {{- end }}
      {{- if .Values.agents.useConfigMap }}
      - name: {{ template "datadog.fullname" . }}-datadog-yaml
        configMap:
          name: {{ template "datadog.fullname" . }}-datadog-yaml
      {{- end }}
      {{- if eq .Values.targetSystem "windows" }}
        {{ include "daemonset-volumes-windows" . | nindent 6 }}
      {{- end }}
      {{- if eq .Values.targetSystem "linux" }}
        {{ include "daemonset-volumes-linux" . | nindent 6 }}
      {{- end }}
{{- if .Values.agents.volumes }}
{{ toYaml .Values.agents.volumes | indent 6 }}
{{- end }}
      tolerations:
      {{- if eq .Values.targetSystem "windows" }}
      - effect: NoSchedule
        key: node.kubernetes.io/os
        value: windows
        operator: Equal
      {{- end }}
      {{- if .Values.agents.tolerations }}
{{ toYaml .Values.agents.tolerations | indent 6 }}
      {{- end }}
      affinity:
{{ toYaml .Values.agents.affinity | indent 8 }}
      serviceAccountName: {{ if .Values.agents.rbac.create }}{{ template "datadog.fullname" . }}{{ else }}"{{ .Values.agents.rbac.serviceAccountName }}"{{ end }}
      nodeSelector:
        {{ template "label.os" . }}: {{ .Values.targetSystem }}
      {{- if .Values.agents.nodeSelector }}
{{ toYaml .Values.agents.nodeSelector | indent 8 }}
      {{- end }}
  updateStrategy:
{{ toYaml .Values.agents.updateStrategy | indent 4 }}
{{ end }}
{{ end }}
