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

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