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

{{- if and .Values.receive.enabled .Values.receive.ingress.enabled -}}
apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ include "thanos.receive.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.receive.ingress.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: receive
  {{- if or .Values.receive.ingress.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.receive.ingress.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.receive.ingress.ingressClassName }}
  ingressClassName: {{ .Values.receive.ingress.ingressClassName | quote }}
  {{- end }}
  rules:
    {{- if .Values.receive.ingress.hostname }}
    - host: {{ include "common.tplvalues.render" ( dict "value" .Values.receive.ingress.hostname "context" $ ) }}
      http:
        paths:
          - path: /api/v1/receive
            pathType: {{ .Values.receive.ingress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (printf "%s-%s"  (include "common.names.fullname" .) "receive") "servicePort" "remote" "context" $)  | nindent 14 }}

          - path: {{ .Values.receive.ingress.path }}
            pathType: {{ .Values.receive.ingress.pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (printf "%s-%s"  (include "common.names.fullname" .) "receive") "servicePort" "http" "context" $)  | nindent 14 }}
    {{- end }}
    {{- range .Values.receive.ingress.extraHosts }}
    - host: {{ .name | quote }}
      http:
        paths:
          - path: {{ default "/" .path }}
            pathType: {{ default "ImplementationSpecific" .pathType }}
            backend: {{- include "common.ingress.backend" (dict "serviceName" (printf "%s-%s"  (include "common.names.fullname" $) "receive") "servicePort" (default "http" .portName) "context" $) | nindent 14 }}
    {{- end }}
    {{- if .Values.receive.ingress.extraRules }}
    {{- include "common.tplvalues.render" (dict "value" .Values.receive.ingress.extraRules "context" $) | nindent 4 }}
    {{- end }}
  {{- if or (and .Values.receive.ingress.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.receive.ingress.annotations )) .Values.receive.ingress.selfSigned)) .Values.receive.ingress.extraTls }}
  tls:
    {{- if and .Values.receive.ingress.tls (or (include "common.ingress.certManagerRequest" ( dict "annotations" .Values.receive.ingress.annotations )) .Values.receive.ingress.selfSigned) }}
    - hosts:
        - {{ .Values.receive.ingress.hostname }}
        {{- range .Values.receive.ingress.extraHosts }}
        - {{ .name | quote }}
        {{- end }}
      secretName: {{ printf "%s-tls" .Values.receive.ingress.hostname }}
    {{- end }}
    {{- if .Values.receive.ingress.extraTls }}
    {{- include "common.tplvalues.render" (dict "value" .Values.receive.ingress.extraTls "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
