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

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