prometheus-operator / kube-prometheus

Use Prometheus to monitor Kubernetes and applications running on Kubernetes
https://prometheus-operator.dev/
Apache License 2.0
6.65k stars 1.92k forks source link

Adding custom configmap to Grafana deployment #364

Closed lanmarti closed 4 years ago

lanmarti commented 4 years ago

What did you do? I'm trying to add a configmap to the grafana deployment, without modifying the original /vendor/grafana/grafana.libsonnet file. However, due to inexperience with jsonnet/ksonnet, I seem to be stuck. By trying to add a new volume, I'm actually overwriting the existing volumes attached to the deployment. I'm looking for a way to add to rather than overwrite the existing volumes, as well as add a mount to the volume mounts of the grafana container that is already defined in grafana.libsonnet.

Did you expect to see some different? Ideally, I would be able to mount the custom config map in the grafana container without modifying grafana.libsonnet itself.

brancz commented 4 years ago

I'm not trying to add a new dashboard, the configmap contains a file needed for a cluster-specific problem.

Could you elaborate what it is that you are trying to solve?

metalmatze commented 4 years ago

Usually, add a new dashboard works by adding a new object to the global grafanaDashboards object. As we loop over that object to generate the configmaps and volumes those object are added as dashboards automatically.

Basically:

{
  grafanaDashboards+:: {
    'mydashboard.json':
    ...
  }
}
lanmarti commented 4 years ago

I'm adding an nsswitch.conf file containing hosts: files dns so that host aliases are resolved in the correct order. I'm not fully clear about the details, but basically, some go/alpine based containers ignore the hosts file as set by Kubernetes when using hostAlias specifications in the deployment .yaml, unless the nsswitch.conf file is added. See for example https://github.com/golang/go/issues/22846

I have to use host aliases to make sure the authentication setup for this cluster works correctly. Manually updating the grafana deployment yaml works for now, but obviously adding a few lines to my .jsonnet file is vastly preferred.

lanmarti commented 4 years ago

@metalmatze thanks for your reply, but this is specifically NOT about dashboards, but a separate configMap, see my previous reply. I already have some custom dashboards up and running, indeed through grafanaDashboards, which works like a charm!

brancz commented 4 years ago

I think this is something that is pretty specific to your environment so I would suggest you to do a patch specific to your problem. A jsonnet patch for this would look something like this:

{
   grafana+:: {
    deployment+:
      {
        spec+: {
          template+: {
            spec+: {
              volumes+: [ * volume definition * ],
              containers:
                std.map(
                  function(c)
                    if c.name == 'grafana' then
                      c {
                        volumeMounts+: [
                          * volume mount definition *,
                        ],
                      }
                    else
                      c,
                  super.containers,
                ),
            },
          },
        },
      },
  },
}
lanmarti commented 4 years ago

Thank you, that did the trick!