{{- if (eq .Values.kind "DaemonSet") }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "prometheus-blackbox-exporter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "prometheus-blackbox-exporter.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "prometheus-blackbox-exporter.chart" . }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "prometheus-blackbox-exporter.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "prometheus-blackbox-exporter.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        helm.sh/chart: {{ include "prometheus-blackbox-exporter.chart" . }}
      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 }}

      restartPolicy: {{ .Values.restartPolicy }}

      {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
      {{- end }}
      containers:
        - name: blackbox-exporter
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            readOnlyRootFilesystem: {{ .Values.readOnlyRootFilesystem }}
            runAsNonRoot: {{ .Values.runAsNonRoot  }}
            runAsUser: {{ .Values.runAsUser }}
          args:
{{- if .Values.config }}
            - "--config.file=/config/blackbox.yaml"
{{- 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 }}
      volumes:
        - name: config
{{- if .Values.secretConfig }}
          secret:
            secretName: {{ template "prometheus-blackbox-exporter.fullname" . }}
{{- 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 }}
