{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.prometheus.enabled .Values.prometheus.thanos.create }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus.thanos.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.prometheus.labels" . | nindent 4 }}
    app.kubernetes.io/subcomponent: thanos
    {{- if .Values.prometheus.thanos.service.labels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.prometheus.thanos.service.labels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.prometheus.thanos.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.prometheus.thanos.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.prometheus.thanos.service.type }}
  {{- if and (eq .Values.prometheus.thanos.service.type "LoadBalancer") (not (empty .Values.prometheus.thanos.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.prometheus.thanos.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.prometheus.thanos.service.type "LoadBalancer") (not (empty .Values.prometheus.thanos.service.loadBalancerClass)) }}
  loadBalancerClass: {{ .Values.prometheus.thanos.service.loadBalancerClass }}
  {{- end }}
  {{- if and (eq .Values.prometheus.thanos.service.type "LoadBalancer") (not (empty .Values.prometheus.thanos.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.prometheus.thanos.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and .Values.prometheus.thanos.service.clusterIP (eq .Values.prometheus.thanos.service.type "ClusterIP") }}
  clusterIP: {{ .Values.prometheus.thanos.service.clusterIP }}
  {{- end }}
  {{- if .Values.prometheus.thanos.service.sessionAffinity }}
  sessionAffinity: {{ .Values.prometheus.thanos.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.prometheus.thanos.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.thanos.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.prometheus.thanos.service.type "LoadBalancer") (eq .Values.prometheus.thanos.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.prometheus.thanos.service.externalTrafficPolicy | quote }}
  {{- end }}
  ports:
    {{- if .Values.prometheus.thanos.serviceMonitor.enabled }}
    - name: http
      port: {{ .Values.prometheus.thanos.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if and .Values.prometheus.thanos.service.nodePorts.http (or (eq .Values.prometheus.thanos.service.type "NodePort") (eq .Values.prometheus.thanos.service.type "LoadBalancer")) }}
      nodePort: {{ .Values.prometheus.thanos.service.nodePorts.http }}
      {{- end }}
    {{- end }}
    - name: grpc
      port: {{ .Values.prometheus.thanos.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      {{- if and .Values.prometheus.thanos.service.nodePorts.grpc (or (eq .Values.prometheus.thanos.service.type "NodePort") (eq .Values.prometheus.thanos.service.type "LoadBalancer")) }}
      nodePort: {{ .Values.prometheus.thanos.service.nodePorts.grpc }}
      {{- end }}
    {{- if .Values.prometheus.thanos.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.thanos.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{- include "kube-prometheus.prometheus.matchLabels" . | nindent 4 }}
{{- end }}
