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

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