{{- if and .Values.prometheus.enabled .Values.prometheus.thanosIngress.enabled }}
{{- $pathType := .Values.prometheus.thanosIngress.pathType | default "" }}
{{- $serviceName := printf "%s-%s" (include "kube-prometheus-stack.fullname" .) "thanos-discovery" }}
{{- $thanosPort := .Values.prometheus.thanosIngress.servicePort -}}
{{- $routePrefix := list .Values.prometheus.prometheusSpec.routePrefix }}
{{- $paths := .Values.prometheus.thanosIngress.paths | default $routePrefix -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
{{- if .Values.prometheus.thanosIngress.annotations }}
  annotations:
    {{- tpl (toYaml .Values.prometheus.thanosIngress.annotations) . | nindent 4 }}
{{- end }}
  name: {{ template "kube-prometheus-stack.fullname" . }}-thanos-gateway
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.name" . }}-prometheus
{{ include "kube-prometheus-stack.labels" . | indent 4 }}
{{- if .Values.prometheus.thanosIngress.labels }}
{{ toYaml .Values.prometheus.thanosIngress.labels | indent 4 }}
{{- end }}
spec:
  {{- if .Values.prometheus.thanosIngress.ingressClassName }}
  ingressClassName: {{ .Values.prometheus.thanosIngress.ingressClassName }}
  {{- end }}
  rules:
  {{- if .Values.prometheus.thanosIngress.hosts }}
  {{- range $host := .Values.prometheus.thanosIngress.hosts }}
    - host: {{ tpl $host $ }}
      http:
        paths:
  {{- range $p := $paths }}
          - path: {{ tpl $p $ }}
            pathType: {{ $pathType }}
            backend:
              service:
                name: {{ $serviceName }}
                port:
                  number: {{ $thanosPort }}
    {{- end -}}
  {{- end -}}
  {{- else }}
    - http:
        paths:
  {{- range $p := $paths }}
          - path: {{ tpl $p $ }}
            pathType: {{ $pathType }}
            backend:
              service:
                name: {{ $serviceName }}
                port:
                  number: {{ $thanosPort }}
  {{- end -}}
  {{- end -}}
  {{- if .Values.prometheus.thanosIngress.tls }}
  tls:
{{ tpl (toYaml .Values.prometheus.thanosIngress.tls | indent 4) . }}
  {{- end -}}
{{- end -}}
