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

{{- if .Values.alertmanager.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus.alertmanager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.alertmanager.labels" . | nindent 4 }}
  {{- if or .Values.alertmanager.service.annotations .Values.commonAnnotations }}
  {{- $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.alertmanager.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 }}
  {{- if and (eq .Values.alertmanager.service.type "LoadBalancer") (not (empty .Values.alertmanager.service.loadBalancerClass)) }}
  loadBalancerClass: {{ .Values.alertmanager.service.loadBalancerClass }}
  {{- end }}
  {{- if and (eq .Values.alertmanager.service.type "LoadBalancer") (eq .Values.alertmanager.service.externalTrafficPolicy "Local") .Values.alertmanager.service.healthCheckNodePort }}
  healthCheckNodePort: {{ .Values.alertmanager.service.healthCheckNodePort }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.alertmanager.service.ports.http }}
      targetPort: 9093
      {{- if and .Values.alertmanager.service.nodePorts.http (or (eq .Values.alertmanager.service.type "NodePort") (eq .Values.alertmanager.service.type "LoadBalancer")) }}
      nodePort: {{ .Values.alertmanager.service.nodePorts.http }}
      {{- end }}
    {{- if .Values.alertmanager.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.alertmanager.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{- include "kube-prometheus.alertmanager.matchLabels" . | nindent 4 }}
{{- end }}
