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

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