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

{{- if and .Values.ingressController.enabled .Values.ingressController.ingress.enabled }}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ template "apisix.ingress-controller.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.ingressController.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/part-of: apisix
    app.kubernetes.io/component: ingress-controller
  {{- if or .Values.ingressController.ingress.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.ingressController.ingress.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingressController.ingress.ingressClassName }}
  ingressClassName: {{ .Values.ingressController.ingress.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.ingressController.ingress.hostname }}
    - host: {{ .Values.ingressController.ingress.hostname }}
      http:
        paths:
          {{- if .Values.ingressController.ingress.extraPaths }}
          {{- toYaml .Values.ingressController.ingress.extraPaths | nindent 10 }}
          {{- end }}
          - path: {{ .Values.ingressController.ingress.path }}
            pathType: {{ .Values.ingressController.ingress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "apisix.ingress-controller.fullname" . | trunc 63 | trimSuffix "-") "servicePort" "http" "context" $)  | nindent 14 }}
    {{- end }}
    {{- range .Values.ingressController.ingress.extraHosts }}
    - host: {{ .name | quote }}
      http:
        paths:
          - path: {{ default "/" .path }}
            pathType: {{ default "ImplementationSpecific" .pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "apisix.ingress-controller.fullname" $ | trunc 63 | trimSuffix "-") "servicePort" "http" "context" $) | nindent 14 }}
    {{- end }}
    {{- if .Values.ingressController.ingress.extraRules }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.ingress.extraRules "context" $) | nindent 4 }}
    {{- end }}
  {{- if or (and .Values.ingressController.ingress.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.ingressController.ingress.annotations )) .Values.ingressController.ingress.selfSigned)) .Values.ingressController.ingress.extraTls }}
  tls:
    {{- if and .Values.ingressController.ingress.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.ingressController.ingress.annotations )) .Values.ingressController.ingress.selfSigned) }}
    - hosts:
        - {{ .Values.ingressController.ingress.hostname | quote }}
      secretName: {{ printf "%s-tls" .Values.ingressController.ingress.hostname }}
    {{- end }}
    {{- if .Values.ingressController.ingress.extraTls }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.ingress.extraTls "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
