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

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