{{- $kubeTargetVersion := default .Capabilities.KubeVersion.GitVersion .Values.kubeTargetVersionOverride }}
{{- if and .Values.prometheus.enabled .Values.prometheus.service.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.ipDualStack.enabled }}
  ipFamilies: {{ toYaml .Values.prometheus.service.ipDualStack.ipFamilies | nindent 4 }}
  ipFamilyPolicy: {{ .Values.prometheus.service.ipDualStack.ipFamilyPolicy }}
{{- 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 }}
{{- if ne .Values.prometheus.service.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.prometheus.service.externalTrafficPolicy }}
{{- 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.reloaderWebPort }}
  - name: reloader-web
    {{- if semverCompare "> 1.20.0-0" $kubeTargetVersion }}
    appProtocol: http
    {{- end }}
    {{- if and (eq .Values.prometheus.service.type "NodePort") (not (empty .Values.prometheus.service.reloaderWebNodePort)) }}
    nodePort: {{ .Values.prometheus.service.reloaderWebNodePort }}
    {{- end }}
    port: {{ .Values.prometheus.service.reloaderWebPort }}
    targetPort: reloader-web
  {{- end }}
  {{- if .Values.prometheus.thanosIngress.enabled }}
  - name: grpc
    {{- if eq .Values.prometheus.service.type "NodePort" }}
    nodePort: {{ .Values.prometheus.thanosIngress.nodePort }}
    {{- end }}
    port: {{ .Values.prometheus.thanosIngress.servicePort }}
    targetPort: {{ .Values.prometheus.thanosIngress.servicePort }}
  {{- end }}
{{- if .Values.prometheus.service.additionalPorts }}
{{ toYaml .Values.prometheus.service.additionalPorts | indent 2 }}
{{- end }}
  publishNotReadyAddresses: {{ .Values.prometheus.service.publishNotReadyAddresses }}
  selector:
    {{- if .Values.prometheus.agentMode }}
    app.kubernetes.io/name: prometheus-agent
    {{- else }}
    app.kubernetes.io/name: prometheus
    {{- end }}
    operator.prometheus.io/name: {{ template "kube-prometheus-stack.prometheus.crname" . }}
{{- if .Values.prometheus.service.sessionAffinity }}
  sessionAffinity: {{ .Values.prometheus.service.sessionAffinity }}
{{- end }}
{{- if eq .Values.prometheus.service.sessionAffinity "ClientIP" }}
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: {{ .Values.prometheus.service.sessionAffinityConfig.clientIP.timeoutSeconds }}
{{- end }}
  type: "{{ .Values.prometheus.service.type }}"
{{- end }}
