{{- if (eq .Values.kind "DaemonSet") }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "prometheus-blackbox-exporter.fullname" . }}
  namespace: {{ template "prometheus-blackbox-exporter.namespace" . }}
  labels:
    {{- include "prometheus-blackbox-exporter.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "prometheus-blackbox-exporter.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "prometheus-blackbox-exporter.labels" . | nindent 8 }}
        {{- if .Values.pod.labels }}
{{ toYaml .Values.pod.labels | indent 8 }}
        {{- end }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "prometheus-blackbox-exporter.serviceAccountName" . }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 6 }}
    {{- end }}
    {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
    {{- end }}
      {{- if .Values.hostAliases }}
      hostAliases:
        {{- range .Values.hostAliases }}
        - ip: {{ .ip }}
          hostnames:
          {{- range .hostNames }}
          - {{ . }}
          {{- end }}
        {{- end }}
      {{- end }}
      restartPolicy: {{ .Values.restartPolicy }}

      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
      {{- if .Values.extraInitContainers }}
      initContainers:
{{ toYaml .Values.extraInitContainers | indent 8 }}
      {{- end }}
      containers:
        - name: blackbox-exporter
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            readOnlyRootFilesystem: {{ .Values.readOnlyRootFilesystem }}
            runAsNonRoot: {{ .Values.runAsNonRoot  }}
            {{- if .Values.runAsUser }}
            runAsUser: {{ .Values.runAsUser }}
            {{- end }}
          env:
          {{- range $key, $value := .Values.extraEnv }}
            - name: {{ $key }}
              value: {{ $value | quote }}
          {{- end }}
          args:
{{- if (or .Values.config .Values.configExistingSecretName) }}
   {{- if .Values.configPath }}
            - "--config.file={{ .Values.configPath }}"
   {{- else }}
            - "--config.file=/config/blackbox.yaml"
   {{- end }}
{{- else }}
            - "--config.file=/etc/blackbox_exporter/config.yml"
{{- end }}
        {{- if .Values.extraArgs }}
{{ toYaml .Values.extraArgs | indent 12 }}
        {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          ports:
            - containerPort: {{ .Values.service.port }}
              name: http
          livenessProbe:
            {{- toYaml .Values.livenessProbe | trim | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | trim | nindent 12 }}
          volumeMounts:
            - mountPath: /config
              name: config
          {{- range .Values.extraConfigmapMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath | default "" }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- range .Values.extraSecretMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
{{- if .Values.dnsPolicy }}
      dnsPolicy: {{ .Values.dnsPolicy | toString }}
{{- end }}
      hostNetwork: {{ .Values.hostNetwork }}
{{- if .Values.dnsConfig }}
      dnsConfig:
        {{- toYaml .Values.dnsConfig | nindent 8 }}
{{- end }}
      volumes:
        - name: config
{{- if .Values.secretConfig }}
          secret:
            secretName: {{ template "prometheus-blackbox-exporter.fullname" . }}
{{- else if .Values.configExistingSecretName }}
          secret:
            secretName: {{ .Values.configExistingSecretName }}
{{- else }}
          configMap:
            name: {{ template "prometheus-blackbox-exporter.fullname" . }}
{{- end }}
      {{- range .Values.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
            defaultMode: {{ .defaultMode }}
      {{- end }}
      {{- range .Values.extraSecretMounts }}
        - name: {{ .name }}
          secret:
            secretName: {{ .secretName }}
            defaultMode: {{ .defaultMode }}
      {{- end }}
{{- end }}
