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

{{- if eq .Values.exposureType "ingress" }}
{{- if eq .Values.ingress.core.controller "gce" }}
  {{- $_ := set . "portal_path" "/*" -}}
  {{- $_ := set . "api_path" "/api/*" -}}
  {{- $_ := set . "service_path" "/service/*" -}}
  {{- $_ := set . "v2_path" "/v2/*" -}}
  {{- $_ := set . "chartrepo_path" "/chartrepo/*" -}}
  {{- $_ := set . "controller_path" "/c/*" -}}
{{- else if eq .Values.ingress.core.controller "ncp" }}
  {{- $_ := set . "portal_path" "/.*" -}}
  {{- $_ := set . "api_path" "/api/.*" -}}
  {{- $_ := set . "service_path" "/service/.*" -}}
  {{- $_ := set . "v2_path" "/v2/.*" -}}
  {{- $_ := set . "chartrepo_path" "/chartrepo/.*" -}}
  {{- $_ := set . "controller_path" "/c/.*" -}}
{{- else }}
  {{- $_ := set . "portal_path" "/" -}}
  {{- $_ := set . "api_path" "/api/" -}}
  {{- $_ := set . "service_path" "/service/" -}}
  {{- $_ := set . "v2_path" "/v2" -}}
  {{- $_ := set . "chartrepo_path" "/chartrepo/" -}}
  {{- $_ := set . "controller_path" "/c/" -}}
{{- end }}
apiVersion: {{ template "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ include "harbor.ingress" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  annotations:
  {{- if .Values.internalTLS.enabled }}
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
  {{- end }}
  {{- if eq .Values.ingress.core.controller "ncp" }}
    ncp/use-regex: "true"
    {{- if .Values.internalTLS.enabled }}
    ncp/http-redirect: "true"
    {{- end }}
  {{- end }}
  {{- if or .Values.ingress.core.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.ingress.core.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- if eq .Values.ingress.core.controller "ncp" }}
  backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.portal" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.ingress.core.ingressClassName }}
  ingressClassName: {{ .Values.ingress.core.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.ingress.core.hostname }}
    - host: {{ include "common.tplvalues.render" ( dict "value" .Values.ingress.core.hostname "context" $ ) }}
      http:
        paths:
          {{- if .Values.ingress.core.extraPaths }}
          {{- toYaml .Values.ingress.core.extraPaths | nindent 10 }}
          {{- end }}
          - path: {{ .api_path }}
            pathType: {{ .Values.ingress.core.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.core" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 14 }}
          - path: {{ .service_path }}
            pathType: {{ .Values.ingress.core.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.core" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 14 }}
          - path: {{ .v2_path }}
            pathType: {{ .Values.ingress.core.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.core" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 14 }}
          - path: {{ .chartrepo_path }}
            pathType: {{ .Values.ingress.core.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.core" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 14 }}
          - path: {{ .controller_path }}
            pathType: {{ .Values.ingress.core.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.core" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 14 }}
          - path: {{ .portal_path }}
            pathType: {{ .Values.ingress.core.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.portal" .) "servicePort" (ternary "https" "http" .Values.internalTLS.enabled) "context" $) | nindent 14 }}
    {{- end }}
    {{- range .Values.ingress.core.extraHosts }}
    - host: {{ include "common.tplvalues.render" ( dict "value" .name "context" $ ) }}
      http:
        paths:
          - path: {{ default "/" .path }}
            pathType: {{ default "ImplementationSpecific" .pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (include "harbor.core" $) "servicePort" (ternary "https" "http" $.Values.internalTLS.enabled) "context" $) | nindent 14 }}
    {{- end }}
    {{- if .Values.ingress.core.extraRules }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ingress.core.extraRules "context" $) | nindent 4 }}
    {{- end }}
  {{- if or (and .Values.ingress.core.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.ingress.core.annotations )) .Values.ingress.core.selfSigned)) .Values.ingress.core.extraTls }}
  tls:
    {{- if and .Values.ingress.core.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.ingress.core.annotations )) .Values.ingress.core.selfSigned) }}
    - hosts:
        - {{ .Values.ingress.core.hostname | quote }}
      secretName: {{ printf "%s-tls" .Values.ingress.core.hostname }}
    {{- end }}
    {{- if .Values.ingress.core.extraTls }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ingress.core.extraTls "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
