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

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