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

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