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

{{- if .Values.queryFrontend.enabled -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "thanos.query-frontend.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.queryFrontend.service.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: query-frontend
    {{- include "thanos.servicemonitor.matchLabels" . | nindent 4 -}}
  {{- if or .Values.queryFrontend.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.queryFrontend.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.queryFrontend.service.type }}
  {{- if and .Values.queryFrontend.service.clusterIP (eq .Values.queryFrontend.service.type "ClusterIP") }}
  clusterIP: {{ .Values.queryFrontend.service.clusterIP }}
  {{- end }}
  {{- if ne .Values.queryFrontend.service.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.queryFrontend.service.externalTrafficPolicy }}
  {{- end }}
  {{- if and .Values.queryFrontend.service.loadBalancerIP (eq .Values.queryFrontend.service.type "LoadBalancer") }}
  loadBalancerIP: {{ .Values.queryFrontend.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.queryFrontend.service.type "LoadBalancer") .Values.queryFrontend.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml .Values.queryFrontend.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if .Values.queryFrontend.service.ipFamilyPolicy }}
  ipFamilyPolicy: {{ .Values.queryFrontend.service.ipFamilyPolicy }}
  {{- end }}
  {{- if .Values.queryFrontend.service.ipFamilies }}
  ipFamilies: {{ toYaml .Values.queryFrontend.service.ipFamilies | nindent 4 }}
  {{- end }}
  ports:
    - port: {{ .Values.queryFrontend.service.ports.http }}
      targetPort: http
      protocol: TCP
      name: http
      {{- if and (or (eq .Values.queryFrontend.service.type "NodePort") (eq .Values.queryFrontend.service.type "LoadBalancer")) .Values.queryFrontend.service.nodePorts.http }}
      nodePort: {{ .Values.queryFrontend.service.nodePorts.http }}
      {{- else if eq .Values.queryFrontend.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.queryFrontend.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.queryFrontend.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector:
    {{- if .Values.queryFrontend.service.labelSelectorsOverride }}
    {{- include "common.tplvalues.render" (dict "value" .Values.queryFrontend.service.labelSelectorsOverride "context" $) | nindent 4 }}
    {{- else }}
    {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.queryFrontend.podLabels .Values.commonLabels ) "context" . ) }}
    {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: query-frontend
    {{- end }}
{{- end }}
