{{- if .Values.prometheus.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-prometheus
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.name" . }}-prometheus
    self-monitor: {{ .Values.prometheus.serviceMonitor.selfMonitor | quote }}
{{ include "kube-prometheus-stack.labels" . | indent 4 }}
{{- if .Values.prometheus.service.labels }}
{{ toYaml .Values.prometheus.service.labels | indent 4 }}
{{- end }}
{{- if .Values.prometheus.service.annotations }}
  annotations:
{{ toYaml .Values.prometheus.service.annotations | indent 4 }}
{{- end }}
spec:
{{- if .Values.prometheus.service.clusterIP }}
  clusterIP: {{ .Values.prometheus.service.clusterIP }}
{{- end }}
{{- if .Values.prometheus.service.externalIPs }}
  externalIPs:
{{ toYaml .Values.prometheus.service.externalIPs | indent 4 }}
{{- end }}
{{- if .Values.prometheus.service.loadBalancerIP }}
  loadBalancerIP: {{ .Values.prometheus.service.loadBalancerIP }}
{{- end }}
{{- if .Values.prometheus.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
  {{- range $cidr := .Values.prometheus.service.loadBalancerSourceRanges }}
    - {{ $cidr }}
  {{- end }}
{{- end }}
  ports:
  - name: {{ .Values.prometheus.prometheusSpec.portName }}
    {{- if eq .Values.prometheus.service.type "NodePort" }}
    nodePort: {{ .Values.prometheus.service.nodePort }}
    {{- end }}
    port: {{ .Values.prometheus.service.port }}
    targetPort: {{ .Values.prometheus.service.targetPort }}
{{- if .Values.prometheus.service.additionalPorts }}
{{ toYaml .Values.prometheus.service.additionalPorts | indent 2 }}
{{- end }}
  selector:
    app: prometheus
    prometheus: {{ template "kube-prometheus-stack.fullname" . }}-prometheus
{{- if .Values.prometheus.service.sessionAffinity }}
  sessionAffinity: {{ .Values.prometheus.service.sessionAffinity }}
{{- end }}
  type: "{{ .Values.prometheus.service.type }}"
{{- end }}
