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

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