---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: datadog-agent
  namespace: datadog
spec:
  selector:
    matchLabels:
      app: datadog-agent
  template:
    metadata:
      labels:
        app: datadog-agent
      name: datadog-agent
    spec:
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      - key: node-role.kubernetes.io/control-plane
        effect: NoSchedule
      serviceAccountName: datadog
      containers:
      - image: datadog/agent:7
        imagePullPolicy: Always
        name: datadog-agent
        ports:
        - containerPort: 8125
          # Custom metrics via DogStatsD - uncomment this section to enable custom metrics collection
          # hostPort: 8125
          name: dogstatsdport
          protocol: UDP
        - containerPort: 8126
          # Trace Collection (APM) - uncomment this section to enable APM
          # hostPort: 8126
          name: traceport
          protocol: TCP
        env:
        - name: DD_API_KEY
          valueFrom:
            secretKeyRef:
              name: datadog-secret
              key: api-key
        - name: DD_COLLECT_KUBERNETES_EVENTS
          value: "true"
        - name: KUBERNETES
          value: "true"
        - name: DD_HEALTH_PORT
          value: "5555"
        - name: DD_KUBERNETES_KUBELET_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: DD_CLUSTER_AGENT_ENABLED
          value: "true"
        - name: DD_CLUSTER_AGENT_AUTH_TOKEN
          valueFrom:
            secretKeyRef:
              name: datadog-auth-token
              key: token
        - name: DD_APM_ENABLED
          value: "true"
        - name: DD_KUBELET_TLS_VERIFY
          value: "false"
        - name: DD_CRI_SOCKET_PATH
          value: "/var/run/containerd/containerd.sock"
        resources:
          requests:
            memory: "512Mi"
            cpu: "400m"
          limits:
            memory: "512Mi"
            cpu: "400m"
        volumeMounts:
        - name: containerdsocket
          mountPath: /var/run/containerd/containerd.sock
        - name: procdir
          mountPath: /host/proc
          readOnly: true
        - name: var-run
          mountPath: /host/var/run
          readOnly: true
        - name: cgroups
          mountPath: /host/sys/fs/cgroup
          readOnly: true
        - name: kube-controller-manager-config
          mountPath: /etc/datadog-agent/conf.d/kube_controller_manager.d/auto_conf.yaml
          subPath: auto_conf.yaml
        - name: kube-scheduler-config
          mountPath: /etc/datadog-agent/conf.d/kube_scheduler.d/auto_conf.yaml
          subPath: auto_conf.yaml
        - name: etcd-config
          mountPath: /etc/datadog-agent/conf.d/etcd.d/auto_conf.yaml
          subPath: auto_conf.yaml
        - name: etcd-certs
          mountPath: /host/etc/kubernetes/pki/etcd
          readOnly: true
        livenessProbe:
          httpGet:
            path: /health
            port: 5555
          initialDelaySeconds: 15
          periodSeconds: 15
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
      volumes:
      - hostPath:
          path: /var/run/containerd/containerd.sock
        name: containerdsocket
      - hostPath:
          path: /proc
        name: procdir
      - hostPath:
          path: /var/run
        name: var-run
      - hostPath:
          path: /sys/fs/cgroup
        name: cgroups
      - name: kube-controller-manager-config
        configMap:
          name: kube-controller-manager-config
      - name: kube-scheduler-config
        configMap:
          name: kube-scheduler-config
      - name: etcd-config
        configMap:
          name: etcd-config
      - name: etcd-certs
        hostPath:
          path: /etc/kubernetes/pki/etcd/
