{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- 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.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 "kube-prometheus.blackboxExporter.matchLabels" . | nindent 6 }}
  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 "kube-prometheus.blackboxExporter.podLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ template "kube-prometheus.blackboxExporter.serviceAccountName" . }}
      {{- include "kube-prometheus.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.blackboxExporter.automountServiceAccountToken }}
      {{- 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:
        {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.blackboxExporter.podLabels .Values.commonLabels ) "context" . ) }}
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.blackboxExporter.podAffinityPreset "component" "blackbox-exporter" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.blackboxExporter.podAntiAffinityPreset "component" "blackbox-exporter" "customLabels" $podLabels "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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.blackboxExporter.podSecurityContext "context" $) | 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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.blackboxExporter.containerSecurityContext "context" $) | 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 }}
          {{- else if ne .Values.blackboxExporter.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.blackboxExporter.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.blackboxExporter.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.blackboxExporter.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.blackboxExporter.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.blackboxExporter.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.blackboxExporter.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.blackboxExporter.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              port: http
          {{- end }}
          {{- if .Values.blackboxExporter.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.blackboxExporter.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.blackboxExporter.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              port: http
          {{- 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
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- 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: empty-dir
          emptyDir: {}
        - 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 }}
