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

apiVersion: v1
kind: Service
metadata:
  name: {{ template "grafana-loki.distributor.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: distributor
  {{- if or .Values.commonAnnotations .Values.distributor.service.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.distributor.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.distributor.service.type }}
  {{- if .Values.distributor.service.sessionAffinity }}
  sessionAffinity: {{ .Values.distributor.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.distributor.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.distributor.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.distributor.service.clusterIP }}
  clusterIP: {{ .Values.distributor.service.clusterIP }}
  {{- end }}
  {{- if (or (eq .Values.distributor.service.type "LoadBalancer") (eq .Values.distributor.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.distributor.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{ if eq .Values.distributor.service.type "LoadBalancer" }}
  loadBalancerSourceRanges: {{ .Values.distributor.service.loadBalancerSourceRanges }}
  {{ end }}
  {{- if (and (eq .Values.distributor.service.type "LoadBalancer") (not (empty .Values.distributor.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.distributor.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.distributor.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if (and (or (eq .Values.distributor.service.type "NodePort") (eq .Values.distributor.service.type "LoadBalancer")) (not (empty .Values.distributor.service.nodePorts.http))) }}
      nodePort: {{ .Values.distributor.service.nodePorts.http }}
      {{- else if eq .Values.distributor.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: grpc
      port: {{ .Values.distributor.service.ports.grpc }}
      protocol: TCP
      targetPort: grpc
      {{- if (and (or (eq .Values.distributor.service.type "NodePort") (eq .Values.distributor.service.type "LoadBalancer")) (not (empty .Values.distributor.service.nodePorts.grpc))) }}
      nodePort: {{ .Values.distributor.service.nodePorts.grpc }}
      {{- else if eq .Values.distributor.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.distributor.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.distributor.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.distributor.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: distributor
