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

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