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