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

{{- if .Values.ruler.enabled -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "thanos.ruler.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.ruler.service.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: ruler
    {{- include "thanos.servicemonitor.matchLabels" . | nindent 4 -}}
  {{- if or .Values.ruler.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.ruler.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.ruler.service.type }}
  {{- if and .Values.ruler.service.clusterIP (eq .Values.ruler.service.type "ClusterIP") }}
  clusterIP: {{ .Values.ruler.service.clusterIP }}
  {{- end }}
  {{- if ne .Values.ruler.service.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.ruler.service.externalTrafficPolicy }}
  {{- end }}
  {{- if and .Values.ruler.service.loadBalancerIP (eq .Values.ruler.service.type "LoadBalancer") }}
  loadBalancerIP: {{ .Values.ruler.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.ruler.service.type "LoadBalancer") .Values.ruler.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml .Values.ruler.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if .Values.ruler.service.ipFamilyPolicy }}
  ipFamilyPolicy: {{ .Values.ruler.service.ipFamilyPolicy }}
  {{- end }}
  {{- if .Values.ruler.service.ipFamilies }}
  ipFamilies: {{ toYaml .Values.ruler.service.ipFamilies | nindent 4 }}
  {{- end }}
  ports:
    - port: {{ .Values.ruler.service.ports.http }}
      targetPort: http
      protocol: TCP
      name: http
      {{- if and (or (eq .Values.ruler.service.type "NodePort") (eq .Values.ruler.service.type "LoadBalancer")) .Values.ruler.service.nodePorts.http }}
      nodePort: {{ .Values.ruler.service.nodePorts.http }}
      {{- else if eq .Values.ruler.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - port: {{ .Values.ruler.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      name: grpc
      {{- if and (or (eq .Values.ruler.service.type "NodePort") (eq .Values.ruler.service.type "LoadBalancer")) .Values.ruler.service.nodePorts.grpc }}
      nodePort: {{ .Values.ruler.service.nodePorts.grpc }}
      {{- else if eq .Values.ruler.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.ruler.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ruler.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector:
    {{- if .Values.ruler.service.labelSelectorsOverride }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ruler.service.labelSelectorsOverride "context" $) | nindent 4 }}
    {{- else }}
    {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.ruler.podLabels .Values.commonLabels ) "context" . ) }}
    {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: ruler
    {{- end }}
{{- end }}
