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

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