2

I am having trouble with configuring grafana datasource with Helm Charts and Kubernetees. This is my release.yaml:

kind: Namespace
apiVersion: v1
metadata:
  name: monitoring
  annotations:
    name: monitoring
  labels:
    name: monitoring
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-datasources
  namespace: monitoring
data:
  grafana.ini: |-
    apiVersion: 1
    datasources:
      - name: Prometheus
        type: prometheus
        access: proxy
        url: http://prometheus-kube-prometheus-prometheus.monitoring.svc.cluster.local:9090
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: prometheus
  namespace: monitoring
spec:
  interval: 5m
  chart:
    spec:
      chart: kube-prometheus
      version: "8.0.7"
      sourceRef:
        kind: HelmRepository
        name: bitnami
        namespace: flux-system
      interval: 1m
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: grafana
  namespace: monitoring
spec:
  interval: 5m
  chart:
    spec:
      chart: grafana
      version: "7.9.8"
      sourceRef:
        kind: HelmRepository
        name: bitnami
        namespace: flux-system
      interval: 1m
  values:
    config:
      useGrafanaIniFile: true
      grafanaIniConfigMap: grafana-datasources
---

I see that grafana does have grafana.ini file in /opt/bitnami/grafana/conf/ directory but when checking the datasources in grafana UI there is none. Does anyone know why this is the case, because I do not understand what I am doing wrong.

puppeteer701
  • 1,225
  • 3
  • 17
  • 33

1 Answers1

0

When deploying with Flux you might benefit from using the configMapGenerator constructor instead of creating the config map manually.

I'm going to assuming you have a folder structure like this

monitoring/
├── kustomize.yaml
├── kustomizeconfig.yaml
├── namepace.yaml
├── release.yaml
└── values.yaml

kustomization.yaml

The namespace attribute tells Flux, that it should install all the resources referenced in the file, in the monitoring namespace. This way you only have to reference a namespace, if you're pulling in objects from other namespaces (eg. sources from the flux-system namespace).

If you call your values file something else (eg. grafana-config.yaml), then you should change the name in the files list to something like values.yaml=grafana-config.yaml. Helm will complain if it does not find a values.yaml entry.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: monitoring

resources:
  - release.yaml
  - namespace.yaml

configMapGenerator:
  - name: grafana-values
    files:
      - values.yaml
      # - values.yaml=grafana-config.yaml

configurations:
  - kustomizeconfig.yaml

kustomizeconfig.yaml

This nameReference will monkey patch the value of the valuesFrom with kind ConfigMap in the release release.yaml file, so it matches the ConfigMap.

nameReference:
  - kind: ConfigMap
    version: v1
    fieldSpecs:
      - path: spec/valuesFrom/name
        kind: HelmRelease

namespace.yaml

kind: Namespace
apiVersion: v1
metadata:
  name: monitoring
  annotations:
    name: monitoring
  labels:
    name: monitoring

release.yaml

apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: prometheus
spec:
  interval: 5m
  chart:
    spec:
      chart: kube-prometheus
      version: "8.0.7"
      sourceRef:
        kind: HelmRepository
        name: bitnami
        namespace: flux-system
      interval: 1m
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: grafana
spec:
  interval: 5m
  chart:
    spec:
      chart: grafana
      version: "7.9.8"
      sourceRef:
        kind: HelmRepository
        name: bitnami
        namespace: flux-system
      interval: 1m
  valuesFrom:
    - kind: ConfigMap
      name: grafana-values

values.yaml

You can reference all the values, that can go into the values.yaml file here: example values.yaml file

grafana.ini:
  paths:
    data: /var/lib/grafana/
    logs: /var/log/grafana
    plugins: /var/lib/grafana/plugins
    provisioning: /etc/grafana/provisioning
  metrics:
    enable: true
    diable_total_stats: true
  date_formats:
    use_browser_locale: true
  analytics:
    reporting_enabled: false

datasources:
  datasources.yaml:
    apiVersion: 1
    datasources:
      - name: Prometheus
        type: prometheus
        access: proxy
        url: http://prometheus-server.monitoring
Asger N.
  • 29
  • 4