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

{{- if .Values.alertmanager.enabled }}
apiVersion: v1
kind: Service
metadata:
  name:  {{ template "grafana-mimir.alertmanager.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: alertmanager
  {{- if or .Values.commonAnnotations .Values.alertmanager.service.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.alertmanager.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.alertmanager.service.type }}
  {{- if and .Values.alertmanager.service.clusterIP (eq .Values.service.type "ClusterIP") }}
  clusterIP: {{ .Values.alertmanager.service.clusterIP }}
  {{- end }}
  {{- if .Values.alertmanager.service.sessionAffinity }}
  sessionAffinity: {{ .Values.alertmanager.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.alertmanager.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.alertmanager.service.type "LoadBalancer") (eq .Values.alertmanager.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.alertmanager.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.alertmanager.service.type "LoadBalancer") (not (empty .Values.alertmanager.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.alertmanager.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.alertmanager.service.type "LoadBalancer") (not (empty .Values.alertmanager.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.alertmanager.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.alertmanager.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if and (or (eq .Values.alertmanager.service.type "NodePort") (eq .Values.alertmanager.service.type "LoadBalancer")) (not (empty .Values.alertmanager.service.nodePorts.http)) }}
      nodePort: {{ .Values.alertmanager.service.nodePorts.http }}
      {{- else if eq .Values.alertmanager.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: grpc
      port: {{ .Values.alertmanager.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      {{- if and (or (eq .Values.alertmanager.service.type "NodePort") (eq .Values.alertmanager.service.type "LoadBalancer")) (not (empty .Values.alertmanager.service.nodePorts.grpc)) }}
      nodePort: {{ .Values.alertmanager.service.nodePorts.grpc }}
      {{- else if eq .Values.alertmanager.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.alertmanager.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.alertmanager.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: alertmanager
{{- end }}
