{{- if .Values.daemonset.enabled }}
{{- if (or (.Values.datadog.apiKeyExistingSecret) (.Values.datadog.apiKey)) }}
apiVersion: extensions/v1beta1
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:
  template:
    metadata:
      labels:
        app: {{ template "datadog.fullname" . }}
      name: {{ template "datadog.fullname" . }}
      annotations:
        checksum/autoconf-config: {{ toYaml .Values.datadog.autoconf | sha256sum }}
        checksum/confd-config: {{ toYaml .Values.datadog.confd | sha256sum }}
        checksum/checksd-config: {{ toYaml .Values.datadog.checksd | sha256sum }}
      {{- if .Values.daemonset.podAnnotations }}
{{ toYaml .Values.daemonset.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.daemonset.useHostNetwork }}
      hostNetwork: {{ .Values.daemonset.useHostNetwork }}
      {{- end }}
      {{- if .Values.daemonset.useHostPID }}
      hostPID: {{ .Values.daemonset.useHostPID }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        resources:
{{ toYaml .Values.datadog.resources | indent 12 }}
        ports:
        - containerPort: 8125
          {{- if .Values.daemonset.useHostPort }}
          hostPort: 8125
          {{- end }}
          name: dogstatsdport
          protocol: UDP
        {{- if .Values.datadog.apmEnabled }}
        - containerPort: 8126
          {{- if .Values.daemonset.useHostPort }}
          hostPort: 8126
          {{- end }}
          name: traceport
          protocol: TCP
        {{- end }}
        env:
          - name: DD_API_KEY
            valueFrom:
              secretKeyRef:
                name: {{ template "datadog.apiSecretName" . }}
                key: api-key
          {{- if .Values.datadog.logLevel }}
          - name: DD_LOG_LEVEL
            value: {{ .Values.datadog.logLevel | quote }}
          {{- end }}
          {{- if .Values.datadog.nonLocalTraffic }}
          - name: DD_DOGSTATSD_NON_LOCAL_TRAFFIC
            value: {{ .Values.datadog.nonLocalTraffic | quote }}
          {{- end }}
          {{- if .Values.datadog.tags }}
          - name: DD_TAGS
            value: {{ .Values.datadog.tags | quote }}
          {{- end }}
          {{- if .Values.datadog.apmEnabled }}
          - name: DD_APM_ENABLED
            value: {{ .Values.datadog.apmEnabled | quote }}
          {{- end }}
          {{- 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 }}
          - name: KUBERNETES
            value: "yes"
          {{- if semverCompare "^1.7-0" .Capabilities.KubeVersion.GitVersion }}
          - name: DD_KUBERNETES_KUBELET_HOST
            valueFrom:
              fieldRef:
                fieldPath: status.hostIP
          {{- end }}
          {{- if .Values.datadog.logsEnabled }}
          - name: DD_LOGS_ENABLED
            value: {{.Values.datadog.logsEnabled | quote}}
          {{- end }}
          {{- if .Values.datadog.logsConfigContainerCollectAll }}
          - name: DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL
            value: {{.Values.datadog.logsConfigContainerCollectAll | quote}}
          {{- end }}
{{- if .Values.datadog.env }}
{{ toYaml .Values.datadog.env | indent 10 }}
{{- end }}
        volumeMounts:
          - name: dockersocket
            mountPath: /var/run/docker.sock
            readOnly: true
          - name: procdir
            mountPath: /host/proc
            readOnly: true
          - name: cgroups
            mountPath: /host/sys/fs/cgroup
            readOnly: true
          {{- if (or (.Values.datadog.confd) (.Values.datadog.autoconf)) }}
          - name: confd
            mountPath: /conf.d
            readOnly: true
          {{- end }}
          {{- if .Values.datadog.checksd }}
          - name: checksd
            mountPath: /checks.d
            readOnly: true
          {{- end }}
          {{- if .Values.datadog.logsEnabled }}
          - name: pointerdir
            mountPath: /var/lib/datadog-agent/logs
          {{- end }}
{{- if .Values.datadog.volumeMounts }}
{{ toYaml .Values.datadog.volumeMounts | indent 10 }}
{{- end }}
        livenessProbe:
          exec:
            command:
            - ./probe.sh
          initialDelaySeconds: 15
          periodSeconds: 5
      volumes:
        - hostPath:
            path: /var/run/docker.sock
          name: dockersocket
        - hostPath:
            path: /proc
          name: procdir
        - hostPath:
            path: /sys/fs/cgroup
          name: cgroups
        {{- if (or (.Values.datadog.confd) (.Values.datadog.autoconf)) }}
        - name: confd
          configMap:
            name: {{ template "datadog.confd.fullname" . }}
        {{- end }}
        {{- if .Values.datadog.checksd }}
        - name: checksd
          configMap:
            name: {{ template "datadog.checksd.fullname" . }}
        {{- end }}
        {{- if .Values.datadog.logsEnabled }}
        - hostPath:
            path: /var/lib/datadog-agent/logs
          name: pointerdir
        {{- end }}
{{- if .Values.datadog.volumes }}
{{ toYaml .Values.datadog.volumes | indent 8 }}
{{- end }}
      {{- if .Values.daemonset.tolerations }}
      tolerations:
{{ 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 "OnDelete" .Values.daemonset.updateStrategy | quote }}
{{ end }}
{{ end }}
