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

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