{{- if and .Values.prometheus.enabled .Values.prometheus.thanosService.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-thanos-discovery
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.name" . }}-thanos-discovery
{{ include "kube-prometheus-stack.labels" . | indent 4 }}
{{- if .Values.prometheus.thanosService.labels }}
{{ toYaml .Values.prometheus.thanosService.labels | indent 4 }}
{{- end }}
{{- if .Values.prometheus.thanosService.annotations }}
  annotations:
{{ toYaml .Values.prometheus.thanosService.annotations | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.prometheus.thanosService.type }}
  clusterIP: {{ .Values.prometheus.thanosService.clusterIP }}
{{- if .Values.prometheus.thanosService.ipDualStack.enabled }}
  ipFamilies: {{ toYaml .Values.prometheus.thanosService.ipDualStack.ipFamilies | nindent 4 }}
  ipFamilyPolicy: {{ .Values.prometheus.thanosService.ipDualStack.ipFamilyPolicy }}
{{- end }}
{{- if ne .Values.prometheus.thanosService.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.prometheus.thanosService.externalTrafficPolicy }}
{{- end }}
  ports:
  - name: {{ .Values.prometheus.thanosService.portName }}
    port: {{ .Values.prometheus.thanosService.port }}
    targetPort: {{ .Values.prometheus.thanosService.targetPort }}
    {{- if eq .Values.prometheus.thanosService.type "NodePort" }}
    nodePort: {{ .Values.prometheus.thanosService.nodePort }}
    {{- end }}
  - name: {{ .Values.prometheus.thanosService.httpPortName }}
    port: {{ .Values.prometheus.thanosService.httpPort }}
    targetPort: {{ .Values.prometheus.thanosService.targetHttpPort }}
    {{- if eq .Values.prometheus.thanosService.type "NodePort" }}
    nodePort: {{ .Values.prometheus.thanosService.httpNodePort }}
    {{- end }}
  selector:
    app.kubernetes.io/name: prometheus
    operator.prometheus.io/name: {{ template "kube-prometheus-stack.prometheus.crname" . }}
{{- end }}
