<p>I am trying to create a configmap for a Grafana datasource, using an instance of Grafana from the Kube-Prometheus-Stack helm chart <a href="https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack" rel="noreferrer">https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack</a></p>
<p>I am aware for dashboards, you can create a configmap from a json file using the commands listed in this answer: <a href="https://stackoverflow.com/questions/57322022/stable-prometheus-operator-adding-persistent-grafana-dashboards">stable/prometheus-operator - adding persistent grafana dashboards</a></p>
<pre><code>wget https://raw.githubusercontent.com/percona/grafana-dashboards/master/dashboards/MongoDB_Overview.json
kubectl -n monitoring create cm grafana-mongodb-overview --from-file=MongoDB_Overview.json
kubectl -n monitoring label cm grafana-mongodb-overview grafana_dashboard=mongodb-overview
</code></pre>
<p>Can something similar be done for grafana datasources? I currently have a datasource.yaml which contains the following lines:</p>
<pre class="lang-yaml prettyprint-override"><code>    data:
      datasource-PRF1-Prometheus.yaml: |-
        apiVersion: 1
        datasources:
          - name: Test-Prometheus
            type: prometheus
            url: https://prometheus.url.net/
            access: Server
            isDefault: true
            basicAuth: true
            basicAuthPassword: password
            basicAuthUser: admin
</code></pre>
<p>However, I am not able to import a datasource using it, even though it creates a configmap.</p>

<p>For you to load the data by grafana server component, you would need to set this in your metadata field <code>grafana_datasource: &quot;1&quot;</code>.</p>
<p>For a configmap:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: example-grafana-datasource
  labels:
     grafana_datasource: &quot;1&quot;
     namespace: monitoring
data:
  datasource.yaml: |-
    apiVersion: 1
    datasources:
    - access: proxy
      basicAuth: false
      editable: false
      isDefault: false
      jsonData:
        authType: credentials
        defaultRegion: us-west-2
      name: CloudWatch
      type: cloudwatch
</code></pre>
<p>For a secret with the same label</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: influx-grafana-datasource
  labels:
     grafana_datasource: &quot;1&quot;
     namespace: monitoring
type: Opaque
stringData:
  influxdatasource.yaml: |-
    # config file version
    apiVersion: 1
    datasources:
      - name: influxdb
        type: influxdb
        access: proxy
        database: metrics_db
        user: metrics_read_user
        url: http://influx.example.com:8086/
        jsonData:
          timeInterval: &quot;15s&quot;
        secureJsonData:
          password: yourinfluxpassword
</code></pre>
