{{- if and .Values.grafana.enabled .Values.grafana.sidecar.datasources.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "prometheus-operator.fullname" . }}-grafana-datasource
  namespace: {{ template "prometheus-operator.namespace" . }}
{{- if .Values.grafana.sidecar.datasources.annotations }}
  annotations:
{{ toYaml .Values.grafana.sidecar.datasources.annotations | indent 4 }}
{{- end }}
  labels:
    {{ $.Values.grafana.sidecar.datasources.label }}: "1"
    app: {{ template "prometheus-operator.name" $ }}-grafana
{{ include "prometheus-operator.labels" $ | indent 4 }}
data:
  datasource.yaml: |-
    apiVersion: 1
    datasources:
{{- if .Values.grafana.sidecar.datasources.defaultDatasourceEnabled }}
    - name: Prometheus
      type: prometheus
      url: http://{{ template "prometheus-operator.fullname" . }}-prometheus:{{ .Values.prometheus.service.port }}/{{ trimPrefix "/" .Values.prometheus.prometheusSpec.routePrefix }}
      access: proxy
      isDefault: true
{{- if .Values.grafana.sidecar.datasources.createPrometheusReplicasDatasources }}
{{- range until (int .Values.prometheus.prometheusSpec.replicas) }}
    - name: Prometheus-{{ . }}
      type: prometheus
      url: http://prometheus-{{ template "prometheus-operator.fullname" $ }}-prometheus-{{ . }}.prometheus-operated:9090/{{ trimPrefix "/" $.Values.prometheus.prometheusSpec.routePrefix }}
      access: proxy
      isDefault: false
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.grafana.additionalDataSources }}
{{ tpl (toYaml .Values.grafana.additionalDataSources | indent 4) . }}
{{- end }}
{{- end }}
