{{- if and .Values.prometheus.enabled .Values.prometheus.servicePerReplica.enabled }}
{{- $count := .Values.prometheus.prometheusSpec.replicas | int -}}
{{- $serviceValues := .Values.prometheus.servicePerReplica -}}
apiVersion: v1
kind: List
metadata:
  name: {{ include "kube-prometheus-stack.fullname" $ }}-prometheus-serviceperreplica
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
items:
{{- range $i, $e := until $count }}
  - apiVersion: v1
    kind: Service
    metadata:
      name: {{ include "kube-prometheus-stack.fullname" $ }}-prometheus-{{ $i }}
      namespace: {{ template "kube-prometheus-stack.namespace" $ }}
      labels:
        app: {{ include "kube-prometheus-stack.name" $ }}-prometheus
{{ include "kube-prometheus-stack.labels" $ | indent 8 }}
      {{- if $serviceValues.annotations }}
      annotations:
{{ toYaml $serviceValues.annotations | indent 8 }}
      {{- end }}
    spec:
      {{- if $serviceValues.clusterIP }}
      clusterIP: {{ $serviceValues.clusterIP }}
      {{- end }}
      {{- if $serviceValues.loadBalancerSourceRanges }}
      loadBalancerSourceRanges:
      {{- range $cidr := $serviceValues.loadBalancerSourceRanges }}
        - {{ $cidr }}
      {{- end }}
      {{- end }}
      {{- if ne $serviceValues.type "ClusterIP" }}
      externalTrafficPolicy: {{ $serviceValues.externalTrafficPolicy }}
      {{- end }}
      ports:
        - name: {{ $.Values.prometheus.prometheusSpec.portName }}
          {{- if eq $serviceValues.type "NodePort" }}
          nodePort: {{ $serviceValues.nodePort }}
          {{- end }}
          port: {{ $serviceValues.port }}
          targetPort: {{ $serviceValues.targetPort }}
      selector:
        {{- if $.Values.prometheus.agentMode }}
        app.kubernetes.io/name: prometheus-agent
        statefulset.kubernetes.io/pod-name: prom-agent-{{ include "kube-prometheus-stack.prometheus.crname" $ }}-{{ $i }}
        {{- else }}
        app.kubernetes.io/name: prometheus
        statefulset.kubernetes.io/pod-name: prometheus-{{ include "kube-prometheus-stack.prometheus.crname" $ }}-{{ $i }}
        {{- end }}
        operator.prometheus.io/name: {{ template "kube-prometheus-stack.prometheus.crname" $ }}
      type: "{{ $serviceValues.type }}"
{{- end }}
{{- end }}
