<p>I am trying to get default Grafana dashboard and admin username/password on deploy.</p>
<p>I tried creating <code>values.yaml</code> but doesn't seem like its working</p>
<pre><code>---
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana
  namespace: monitoring
  labels:
    grafana_dashboard: &quot;1&quot;
data:
  dashboard-k8.json: | (3)
    ${indent(4, data)}
</code></pre>
<p>Is there any better working way to do this? and I used same method for default username and password for grafana.</p>
<p>I am working on a maintenance project so its not new installation of Grafana. But, the requirement is to have default dashboard set with new admin username and password when the service is deployed.</p>

<p>As per the grafana docker <a href="https://grafana.com/docs/grafana/latest/setup-grafana/configure-docker/#configure-grafana-with-docker-secrets" rel="nofollow noreferrer">documentation</a>, any config in the <code>grafana.ini</code> file can be set at runtime using environment variables of the format <code>GF_&lt;SectionName&gt;_&lt;KeyName&gt;__FILE</code>, which represent paths to files containing the actual value. So we can use the environment variables <code>GF_SECURITY_ADMIN_USER__FILE</code>, <code>GF_SECURITY_ADMIN_PASSWORD__FILE</code> and <code>GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH__FILE</code>, to set the admin username, password and the dashboard that is opened at launch.</p>
<p>According to the instructions in <a href="https://stackoverflow.com/questions/63518460/grafana-import-dashboard-as-part-of-docker-compose/74995091#74995091">this answer</a>, we can use a dashboard provider of type <code>file</code> to import dashboards from files.</p>
<p>Using both these techniques together, I think I was able to get what you wanted.</p>
<p><strong>Steps performed:</strong></p>
<ul>
<li>Created a secret that contains the admin password. At runtime, the password gets mounted into the grafana container at <code>/etc/secrets/admin_password</code> and environment variable <code>GF_SECURITY_ADMIN_PASSWORD__FILE</code> gets set to <code>/etc/secrets/admin_password</code></li>
<li>Created a config map that contains the dashboard provider config which gets mounted as <code>/etc/grafana/provisioning/dashboards/main.yaml</code></li>
<li>Created another config map that contains the admin user and default dashboard path. These values are exposed to the container as files <code>/etc/config/admin_user</code> and <code>/etc/config/default_home_dashboard_path</code>, with those file paths set to the <code>GF_SECURITY_ADMIN_PASSWORD__FILE</code> and <code>GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH__FILE</code> environment variables</li>
<li>Created another config map which contains the JSON for the default dashboard. The JSON file gets mounted as <code>/var/lib/grafana/dashboards/test-dashboard.json</code>, with the text <code>/var/lib/grafana/dashboards/test-dashboard.json</code> exposed as <code>/etc/config/default_home_dashboard_path</code>(set in the previous step)</li>
</ul>
<p><strong>Config map for the dashboard JSON:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: default
  labels:
    app: grafana
  name: grafana-dashboards
data:
  test-dashboard.json: |-
    {
      &quot;annotations&quot;: {
        &quot;list&quot;: [
      ...truncated...
    }
</code></pre>
<p><strong>Other config maps and deployment spec:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: v1
kind: Secret
metadata:
  namespace: default
  labels:
    app: grafana
  name: grafana-secrets
type: Opaque
stringData:
  admin_password: testpwd
  
---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: default
  labels:
    app: grafana
  name: grafana-dashboard-provider
data:
  dashboard.yml: |-
    ---
    apiVersion: 1

    providers:
      - name: &quot;Dashboard provider&quot;
        orgId: 1
        type: file
        disableDeletion: false
        updateIntervalSeconds: 10
        allowUiUpdates: false
        options:
          path: /var/lib/grafana/dashboards
          foldersFromFilesStructure: true
          
---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: default
  labels:
    app: grafana
  name: grafana-config
data:
  admin_user: testuser
  default_home_dashboard_path: /var/lib/grafana/dashboards/test-dashboard.json        

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: default
  labels:
    app: grafana
  name: grafana
spec:
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      volumes:
        - name: secret-volume
          secret:
            secretName: grafana-secrets
            items:
              - key: admin_password
                path: admin_password
        - name: dashboard-provider-volume
          configMap:
            name: grafana-dashboard-provider
            items:
              - key: dashboard.yml
                path: main.yaml
        - name: dashboards-volume
          configMap:
            name: grafana-dashboards
            items:
              - key: test-dashboard.json
                path: test-dashboard.json
        - name: config-volume
          configMap:
            name: grafana-config
            items:
              - key: admin_user
                path: admin_user
              - key: default_home_dashboard_path
                path: default_home_dashboard_path          
      containers:
        - name: grafana
          image: grafana/grafana-oss:9.5.1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3000
          volumeMounts:
            - name: secret-volume
              mountPath: /etc/secrets
            - name: dashboard-provider-volume
              mountPath: /etc/grafana/provisioning/dashboards
            - name: dashboards-volume
              mountPath: /var/lib/grafana/dashboards
            - name: config-volume
              mountPath: /etc/config
          env:
            - name: GF_SECURITY_ADMIN_PASSWORD__FILE
              value: /etc/secrets/admin_password
            - name: GF_SECURITY_ADMIN_USER__FILE
              value: /etc/config/admin_user
            - name: GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH__FILE
              value: /etc/config/default_home_dashboard_path
</code></pre>
<p>The complete config files can be found on <a href="https://github.com/devatherock/random-demo/commit/fd868fa7f488fcb77091a0202499da8684c905df" rel="nofollow noreferrer">github</a></p>
