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

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