{{- if .Values.deployment.enabled }}
{{- if (or (.Values.datadog.apiKeyExistingSecret) (.Values.datadog.apiKey)) }}
apiVersion: apps/v1
kind: Deployment
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" . }}
      type: deployment
  replicas: {{ .Values.deployment.replicas }}
  template:
    metadata:
      labels:
        app: {{ template "datadog.fullname" . }}
        type: deployment
      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 }}
    spec:
      {{- if .Values.datadog.securityContext }}
      securityContext:
{{ toYaml .Values.datadog.securityContext | indent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
      {{- end }}
      {{- if .Values.deployment.priorityClassName }}
      priorityClassName: {{ .Values.deployment.priorityClassName }}
      {{- end }}
      containers:
      - name: {{ default .Chart.Name .Values.datadog.name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        resources:
{{ toYaml .Values.datadog.resources | indent 10 }}
        ports:
        - containerPort: 8125
          name: dogstatsdport
          protocol: UDP
        {{- if .Values.datadog.apmEnabled }}
        - containerPort: 8126
          name: traceport
          protocol: TCP
        {{- end }}
        env:
          - name: DD_API_KEY
            valueFrom:
              secretKeyRef:
                name: {{ template "datadog.apiSecretName" . }}
                key: api-key
          {{- if .Values.datadog.site }}
          - name: DD_SITE
            value: {{ .Values.datadog.site | quote }}
          {{- end }}
          {{- if .Values.datadog.dd_url }}
          - name: DD_DD_URL
            value: {{ .Values.datadog.dd_url | quote }}
          {{- end }}
          {{- 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.dogstatsdOriginDetection }}
          - name: DD_DOGSTATSD_ORIGIN_DETECTION
            value: {{ .Values.datadog.dogstatsdOriginDetection | 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 }}
          - name: KUBERNETES
            value: "yes"
          {{- if .Values.datadog.collectEvents }}
          - name: KUBERNETES_COLLECT_EVENTS
            value: "yes"
          {{- end }}
          {{- if .Values.datadog.criSocketPath }}
          - name: DD_CRI_SOCKET_PATH
            value: {{ .Values.datadog.criSocketPath | quote }}
          {{- end }}
          {{- if .Values.datadog.useDogStatsDSocketVolume }}
          - name: DD_DOGSTATSD_SOCKET
            value: {{ default "/var/run/datadog/dsd.socket" .Values.datadog.dogStatsDSocketPath | quote }}
          {{- end }}
{{- if .Values.datadog.env }}
{{ toYaml .Values.datadog.env | indent 10 }}
{{- end }}
        volumeMounts:
          {{- if .Values.datadog.useCriSocketVolume }}
          - name: runtimesocket
            mountPath: {{ default "/var/run/docker.sock" .Values.datadog.criSocketPath | quote }}
            readOnly: true
          {{- end }}
          {{- if .Values.datadog.useDogStatsDSocketVolume }}
          - name: dsdsocket
            mountPath: "/var/run/datadog"
          {{- end }}
          - 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.volumeMounts }}
{{ toYaml .Values.datadog.volumeMounts | indent 10 }}
{{- end }}
{{- if .Values.datadog.livenessProbe }}
        livenessProbe:
{{ toYaml .Values.datadog.livenessProbe | indent 10 }}
{{- else }}
        livenessProbe:
          exec:
            command:
            - ./probe.sh
          initialDelaySeconds: 15
          periodSeconds: 5
          failureThreshold: 6
{{- end }}
      volumes:
        {{- 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
        {{- 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.volumes }}
{{ toYaml .Values.datadog.volumes | indent 8 }}
{{- end }}
      {{- if .Values.deployment.tolerations }}
      tolerations:
{{ toYaml .Values.deployment.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.deployment.affinity }}
      affinity:
{{ toYaml .Values.deployment.affinity | indent 8 }}
      {{- end }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "datadog.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
{{ end }}
{{ end }}
