{{- if and .Values.clusterAgent.enabled .Values.clusterAgent.clusterChecks.enabled .Values.clusterchecksDeployment.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "datadog.fullname" . }}-clusterchecks
  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" . }}-clusterchecks
  replicas: {{ .Values.clusterchecksDeployment.replicas }}
  template:
    metadata:
      labels:
        app: {{ template "datadog.fullname" . }}-clusterchecks
      name: {{ template "datadog.fullname" . }}-clusterchecks
    spec:
      {{- if .Values.clusterchecksDeployment.rbac.dedicated }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "datadog.fullname" . }}-cluster-checks{{ else }}"{{ .Values.clusterchecksDeployment.rbac.serviceAccountName }}"{{ end }}
      {{- else }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "datadog.fullname" . }}{{ else }}"{{ .Values.rbac.serviceAccountName }}"{{ end }}
      {{- end }}
      containers:
      - name: {{ default .Chart.Name .Values.datadog.name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
          - name: DD_API_KEY
            valueFrom:
              secretKeyRef:
                name: {{ template "datadog.apiSecretName" . }}
                key: api-key
          - name: DD_EXTRA_CONFIG_PROVIDERS
            value: "clusterchecks"
          - {name: DD_HEALTH_PORT, value: "5555"}
          # Cluster checks
          - name: DD_CLUSTER_AGENT_KUBERNETES_SERVICE_NAME
            value: {{ template "datadog.fullname" . }}-cluster-agent
          - name: DD_CLUSTER_AGENT_AUTH_TOKEN
            valueFrom:
              secretKeyRef:
                name: {{ template "clusterAgent.tokenSecretName" . }}
                key: token
          - name: DD_CLUSTER_AGENT_ENABLED
            value: {{ .Values.clusterAgent.enabled | quote }}
          - {name: DD_EXTRA_CONFIG_PROVIDERS, value: "clusterchecks"}
          # Remove unused features
          - {name: DD_APM_ENABLED, value: "false"}
          - {name: DD_PROCESS_AGENT_ENABLED, value: "false"}
          - {name: DD_LOGS_ENABLED, value: "false"}
          # Safely run alongside the daemonset
          - {name: DD_ENABLE_METADATA_COLLECTION, value: "false"}
          - name: DD_HOSTNAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
{{- if .Values.clusterchecksDeployment.env }}
{{ toYaml .Values.clusterchecksDeployment.env | indent 10 }}
{{- end }}
        resources:
{{ toYaml .Values.clusterchecksDeployment.resources | indent 10 }}
        volumeMounts:
          - {name: s6-run, mountPath: /var/run/s6}
          - {name: remove-corechecks, mountPath: /etc/datadog-agent/conf.d}
{{- if .Values.clusterchecksDeployment.livenessProbe }}
        livenessProbe:
{{ toYaml .Values.clusterchecksDeployment.livenessProbe | indent 10 }}
{{- else }}
        livenessProbe:
          httpGet:
            path: /health
            port: 5555
          initialDelaySeconds: 15
          periodSeconds: 15
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
{{- end }}
      volumes:
        - {name: s6-run, emptyDir: {}}
        - {name: remove-corechecks, emptyDir: {}}
      affinity:
{{- if .Values.clusterchecksDeployment.affinity }}
{{ toYaml .Values.clusterchecksDeployment.affinity | indent 8 }}
{{- else }}
        # Ensure we only run one worker per node, to avoid name collisions
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: {{ template "datadog.fullname" . }}-clusterchecks
            topologyKey: kubernetes.io/hostname
{{- end }}
      {{- if .Values.clusterchecksDeployment.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.clusterchecksDeployment.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.clusterchecksDeployment.tolerations }}
      tolerations:
{{ toYaml .Values.clusterchecksDeployment.tolerations | indent 8 }}
      {{- end }}
{{ end }}
