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

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