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

{{- if and .Values.applicationSet.enabled .Values.applicationSet.webhook.ingress.enabled -}}
apiVersion: {{ include "common.capabilities.webhook.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ include "argocd.applicationSet" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.applicationSet.webhook.ingress.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: applicationSet
  {{- if or .Values.applicationSet.webhook.ingress.annotations .Values.commonAnnotations .Values.applicationSet.webhook.ingress.certManager }}
  annotations:
    {{- if .Values.applicationSet.webhook.ingress.certManager }}
    kubernetes.io/tls-acme: "true"
    {{- end }}
    {{- if or .Values.applicationSet.webhook.ingress.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.applicationSet.webhook.ingress.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  {{- if .Values.applicationSet.webhook.ingress.ingressClassName }}
  ingressClassName: {{ .Values.applicationSet.webhook.ingress.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.applicationSet.webhook.ingress.hostname }}
    - host: {{ .Values.applicationSet.webhook.ingress.hostname }}
      http:
        paths:
          {{- if .Values.applicationSet.webhook.ingress.extraPaths }}
          {{- toYaml .Values.applicationSet.webhook.ingress.extraPaths | nindent 10 }}
          {{- end }}
          - path: {{ .Values.applicationSet.webhook.ingress.path }}
            pathType: {{ .Values.applicationSet.webhook.ingress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "argocd.server" .) "servicePort" (ternary "https" "http" .Values.applicationSet.webhook.ingress.tls) "context" $)  | nindent 14 }}
    {{- end }}
    {{- range .Values.applicationSet.webhook.ingress.extraHosts }}
    - host: {{ .name | quote }}
      http:
        paths:
          - path: {{ default "/" .path }}
            pathType: {{ default "ImplementationSpecific" .pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "argocd.server" $) "servicePort" (ternary "https" "http" $.Values.applicationSet.webhook.ingress.tls) "context" $) | nindent 14 }}
    {{- end }}
  tls:
    {{- if .Values.applicationSet.webhook.ingress.tls }}
    - hosts:
        - {{ .Values.applicationSet.webhook.ingress.hostname }}
      secretName: {{ printf "%s-tls" .Values.applicationSet.webhook.ingress.hostname }}
    {{- end }}
    {{- if .Values.applicationSet.webhook.ingress.extraTls }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.applicationSet.webhook.ingress.extraTls "context" $ ) | nindent 4 }}
    {{- end }}
{{- end -}}
