{{- if .Values.blackboxExporter.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "kube-prometheus.blackboxExporter.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.blackboxExporter.labels" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.blackboxExporter.replicaCount }}
  {{- if .Values.blackboxExporter.updateStrategy }}
  strategy: {{- toYaml .Values.blackboxExporter.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: blackbox-exporter
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/blackbox-exporter/configmap.yaml") . | sha256sum }}
        {{- if .Values.blackboxExporter.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: blackbox-exporter
        {{- if .Values.blackboxExporter.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "kube-prometheus.blackboxExporter.serviceAccountName" . }}
      {{- include "kube-prometheus.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.blackboxExporter.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.blackboxExporter.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.blackboxExporter.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.blackboxExporter.podAffinityPreset "component" "blackbox-exporter" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.blackboxExporter.podAntiAffinityPreset "component" "blackbox-exporter" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.blackboxExporter.nodeAffinityPreset.type "key" .Values.blackboxExporter.nodeAffinityPreset.key "values" .Values.blackboxExporter.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.blackboxExporter.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.blackboxExporter.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.blackboxExporter.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.blackboxExporter.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.blackboxExporter.priorityClassName }}
      priorityClassName: {{ .Values.blackboxExporter.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.blackboxExporter.schedulerName }}
      schedulerName: {{ .Values.blackboxExporter.schedulerName }}
      {{- end }}
      {{- if .Values.blackboxExporter.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.blackboxExporter.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.blackboxExporter.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.blackboxExporter.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.blackboxExporter.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: blackbox-exporter
          image: {{ template "kube-prometheus.blackboxExporter.image" . }}
          imagePullPolicy: {{ .Values.blackboxExporter.image.pullPolicy }}
          {{- if .Values.blackboxExporter.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.blackboxExporter.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --config.file=/bitnami/blackbox-exporter/conf/config.yml
            - --web.listen-address=:{{ .Values.blackboxExporter.containerPorts.http }}
          {{- end }}
          {{- if .Values.blackboxExporter.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.blackboxExporter.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.blackboxExporter.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.blackboxExporter.containerPorts.http }}
              name: http
          {{- if .Values.blackboxExporter.resources }}
          resources: {{- toYaml .Values.blackboxExporter.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.blackboxExporter.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              port: http
          {{- else if .Values.blackboxExporter.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.blackboxExporter.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              port: http
          {{- else if .Values.blackboxExporter.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.blackboxExporter.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              port: http
          {{- else if .Values.blackboxExporter.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: blackbox-exporter-config
              mountPath: /bitnami/blackbox-exporter/conf
          {{- if .Values.blackboxExporter.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.blackboxExporter.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.blackboxExporter.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: blackbox-exporter-config
          configMap:
            name: {{ template "kube-prometheus.blackboxExporter.configmapName" . }}
        {{- if .Values.blackboxExporter.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
