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

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