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

{{- if .Values.ruler.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "grafana-loki.ruler.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: grafana-loki
    app.kubernetes.io/component: ruler
  {{- if or .Values.commonAnnotations .Values.ruler.service.annotations }}
  {{- $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 .Values.ruler.service.sessionAffinity }}
  sessionAffinity: {{ .Values.ruler.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.ruler.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.ruler.service.clusterIP }}
  clusterIP: {{ .Values.ruler.service.clusterIP }}
  {{- end }}
  {{- if (or (eq .Values.ruler.service.type "LoadBalancer") (eq .Values.ruler.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.ruler.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{ if eq .Values.ruler.service.type "LoadBalancer" }}
  loadBalancerSourceRanges: {{ .Values.ruler.service.loadBalancerSourceRanges }}
  {{ end }}
  {{- if (and (eq .Values.ruler.service.type "LoadBalancer") (not (empty .Values.ruler.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.ruler.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.ruler.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if (and (or (eq .Values.ruler.service.type "NodePort") (eq .Values.ruler.service.type "LoadBalancer")) (not (empty .Values.ruler.service.nodePorts.http))) }}
      nodePort: {{ .Values.ruler.service.nodePorts.http }}
      {{- else if eq .Values.ruler.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: grpc
      port: {{ .Values.ruler.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      {{- if (and (or (eq .Values.ruler.service.type "NodePort") (eq .Values.ruler.service.type "LoadBalancer")) (not (empty .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 }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.ruler.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: grafana-loki
    app.kubernetes.io/component: ruler
{{- end }}
