{{- if and .Values.prometheus.enabled .Values.prometheus.thanosServiceExternal.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-thanos-external
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
{{ include "kube-prometheus-stack.labels" . | indent 4 }}
{{- if .Values.prometheus.thanosServiceExternal.labels }}
{{ toYaml .Values.prometheus.thanosServiceExternal.labels | indent 4 }}
{{- end }}
{{- if .Values.prometheus.thanosServiceExternal.annotations }}
  annotations:
{{ toYaml .Values.prometheus.thanosServiceExternal.annotations | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.prometheus.thanosServiceExternal.type }}
{{- if .Values.prometheus.thanosServiceExternal.loadBalancerIP }}
  loadBalancerIP: {{ .Values.prometheus.thanosServiceExternal.loadBalancerIP }}
{{- end }}
{{- if .Values.prometheus.thanosServiceExternal.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
  {{- range $cidr := .Values.prometheus.thanosServiceExternal.loadBalancerSourceRanges }}
    - {{ $cidr }}
  {{- end }}
{{- end }}
{{- if ne .Values.prometheus.thanosServiceExternal.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.prometheus.thanosServiceExternal.externalTrafficPolicy }}
{{- end }}
  ports:
  - name: {{ .Values.prometheus.thanosServiceExternal.portName }}
    port: {{ .Values.prometheus.thanosServiceExternal.port }}
    targetPort: {{ .Values.prometheus.thanosServiceExternal.targetPort }}
    {{- if eq .Values.prometheus.thanosServiceExternal.type "NodePort" }}
    nodePort: {{ .Values.prometheus.thanosServiceExternal.nodePort }}
    {{- end }}
  - name: {{ .Values.prometheus.thanosServiceExternal.httpPortName }}
    port: {{ .Values.prometheus.thanosServiceExternal.httpPort }}
    targetPort: {{ .Values.prometheus.thanosServiceExternal.targetHttpPort }}
    {{- if eq .Values.prometheus.thanosServiceExternal.type "NodePort" }}
    nodePort: {{ .Values.prometheus.thanosServiceExternal.httpNodePort }}
    {{- end }}
  selector:
    app.kubernetes.io/name: prometheus
    operator.prometheus.io/name: {{ template "kube-prometheus-stack.prometheus.crname" . }}
{{- end }}
