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

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