{{- if .Values.ingress.enabled -}}
apiVersion: {{ template "grafana.ingress.apiVersion" . }}
kind: Ingress
metadata:
  name: {{ include "grafana.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "grafana.labels" . | nindent 4 }}
    app.kubernetes.io/component: grafana
  annotations:
    {{- if .Values.ingress.certManager }}
    kubernetes.io/tls-acme: "true"
    {{- end }}
    {{- with .Values.ingress.annotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ .name | quote }}
      http:
        paths:
        {{- range .extraPaths }}
          - path: {{ .path }}
            backend:
              serviceName: {{ .backend.serviceName }}
              servicePort: {{ .backend.servicePort}}
        {{- end }}
        {{- $paths := default (list "/") .paths }}
        {{- range $paths }}
          - path: {{ . }}
            backend:
              serviceName: {{ include "grafana.fullname" $ }}
              servicePort: http
        {{- end }}
  {{- end }}
  tls:
  {{- range .Values.ingress.hosts }}
  {{- if .tls }}
    - hosts:
    {{- if .tlsHosts }}
      {{- range $host := .tlsHosts }}
        - {{ $host }}
      {{- end }}
    {{- else }}
        - {{ .name }}
    {{- end }}
      secretName: {{ .tlsSecret }}
  {{- end }}
{{- end }}
{{- end }}
