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

{{- if .Values.blackboxExporter.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus.blackboxExporter.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.blackboxExporter.labels" . | nindent 4 }}
  {{- if or .Values.commonAnnotations .Values.blackboxExporter.service.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.blackboxExporter.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.blackboxExporter.service.type }}
  {{- if .Values.blackboxExporter.service.sessionAffinity }}
  sessionAffinity: {{ .Values.blackboxExporter.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.blackboxExporter.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if and .Values.blackboxExporter.service.clusterIP (eq .Values.blackboxExporter.service.type "ClusterIP") }}
  clusterIP: {{ .Values.blackboxExporter.service.clusterIP }}
  {{- end }}
  {{- if or (eq .Values.blackboxExporter.service.type "LoadBalancer") (eq .Values.blackboxExporter.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.blackboxExporter.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.blackboxExporter.service.type "LoadBalancer") (not (empty .Values.blackboxExporter.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.blackboxExporter.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.blackboxExporter.service.type "LoadBalancer") (not (empty .Values.blackboxExporter.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.blackboxExporter.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.blackboxExporter.service.type "LoadBalancer") (not (empty .Values.blackboxExporter.service.loadBalancerClass)) }}
  loadBalancerClass: {{ .Values.blackboxExporter.service.loadBalancerClass }}
  {{- end }}
  publishNotReadyAddresses: true
  ports:
    - name: http
      port: {{ .Values.blackboxExporter.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if (and (or (eq .Values.blackboxExporter.service.type "NodePort") (eq .Values.blackboxExporter.service.type "LoadBalancer")) (not (empty .Values.blackboxExporter.service.nodePorts.http))) }}
      nodePort: {{ .Values.blackboxExporter.service.nodePorts.http }}
      {{- else if eq .Values.blackboxExporter.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.blackboxExporter.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.blackboxExporter.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{- include "kube-prometheus.blackboxExporter.matchLabels" . | nindent 4 }}
{{- end }}
