Skip to content

Commit

Permalink
Add folder support for dashboards (brancz#72).
Browse files Browse the repository at this point in the history
  • Loading branch information
toran414 committed Mar 3, 2020
1 parent 1b07a80 commit c80d495
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 15 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,64 @@ k.core.v1.list.new(
)
```

#### Organizing dashboards

If you have many dashboards and would like to organize them into folders, you can do that as well by specifying them in `folderDashboards` rather than `dashboards`.

[embedmd]:# (examples/dashboard-folder-definition.jsonnet)
```jsonnet
local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;
local grafana = import 'grafonnet/grafana.libsonnet';
local dashboard = grafana.dashboard;
local row = grafana.row;
local prometheus = grafana.prometheus;
local template = grafana.template;
local graphPanel = grafana.graphPanel;
local grafana =
((import 'grafana/grafana.libsonnet') +
{
_config+:: {
namespace: 'monitoring-grafana',
grafana+:: {
folderDashboards+:: {
Services: {
'regional-services-dashboard.json': (import 'dashboards/regional-services-dashboard.json'),
'global-services-dashboard.json': (import 'dashboards/global-services-dashboard.json'),
},
AWS: {
'aws-ec2-dashboard.json': (import 'dashboards/aws-ec2-dashboard.json'),
'aws-rds-dashboard.json': (import 'dashboards/aws-rds-dashboard.json'),
'aws-sqs-dashboard.json': (import 'dashboards/aws-sqs-dashboard.json'),
},
ISTIO: {
'istio-citadel-dashboard.json': (import 'dashboards/istio-citadel-dashboard.json'),
'istio-galley-dashboard.json': (import 'dashboards/istio-galley-dashboard.json'),
'istio-mesh-dashboard.json': (import 'dashboards/istio-mesh-dashboard.json'),
'istio-pilot-dashboard.json': (import 'dashboards/istio-pilot-dashboard.json'),
}
},
},
},
}).grafana;
k.core.v1.list.new(
grafana.dashboardDefinitions +
[
grafana.dashboardSources,
grafana.dashboardDatasources,
grafana.deployment,
grafana.serviceAccount,
grafana.service +
service.mixin.spec.withPorts(servicePort.newNamed('http', 3000, 'http') + servicePort.withNodePort(30910)) +
service.mixin.spec.withType('NodePort'),
]
)
```

#### Dashboards mixins

Using the [kubernetes-mixin](https://github.com/kubernetes-monitoring/kubernetes-mixin)s, simply install:
Expand Down
50 changes: 50 additions & 0 deletions examples/dashboard-folder-definition.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;

local grafana = import 'grafonnet/grafana.libsonnet';
local dashboard = grafana.dashboard;
local row = grafana.row;
local prometheus = grafana.prometheus;
local template = grafana.template;
local graphPanel = grafana.graphPanel;

local grafana =
((import 'grafana/grafana.libsonnet') +
{
_config+:: {
namespace: 'monitoring-grafana',
grafana+:: {
folderDashboards+:: {
Services: {
'regional-services-dashboard.json': (import 'dashboards/regional-services-dashboard.json'),
'global-services-dashboard.json': (import 'dashboards/global-services-dashboard.json'),
},
AWS: {
'aws-ec2-dashboard.json': (import 'dashboards/aws-ec2-dashboard.json'),
'aws-rds-dashboard.json': (import 'dashboards/aws-rds-dashboard.json'),
'aws-sqs-dashboard.json': (import 'dashboards/aws-sqs-dashboard.json'),
},
ISTIO: {
'istio-citadel-dashboard.json': (import 'dashboards/istio-citadel-dashboard.json'),
'istio-galley-dashboard.json': (import 'dashboards/istio-galley-dashboard.json'),
'istio-mesh-dashboard.json': (import 'dashboards/istio-mesh-dashboard.json'),
'istio-pilot-dashboard.json': (import 'dashboards/istio-pilot-dashboard.json'),
}
},
},
},
}).grafana;

k.core.v1.list.new(
grafana.dashboardDefinitions +
[
grafana.dashboardSources,
grafana.dashboardDatasources,
grafana.deployment,
grafana.serviceAccount,
grafana.service +
service.mixin.spec.withPorts(servicePort.newNamed('http', 3000, 'http') + servicePort.withNodePort(30910)) +
service.mixin.spec.withType('NodePort'),
]
)
14 changes: 0 additions & 14 deletions grafana/configs/dashboard-sources/dashboards.libsonnet

This file was deleted.

47 changes: 46 additions & 1 deletion grafana/grafana.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
grafana+:: {
dashboards: {},
rawDashboards: {},
folderDashboards: {},
datasources: [{
name: 'prometheus',
type: 'prometheus',
Expand Down Expand Up @@ -57,6 +58,13 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
configMap.mixin.metadata.withNamespace($._config.namespace)

for name in std.objectFields($._config.grafana.dashboards)
] + [
local dashboardName = 'grafana-dashboard-' + std.strReplace(name, '.json', '');
configMap.new(dashboardName, { [name]: std.manifestJsonEx($._config.grafana.folderDashboards[folder][name], ' ') }) +
configMap.mixin.metadata.withNamespace($._config.namespace)

for folder in std.objectFields($._config.grafana.folderDashboards)
for name in std.objectFields($._config.grafana.folderDashboards[folder])
] + if std.length($._config.grafana.rawDashboards) > 0 then
[
local dashboardName = 'grafana-dashboard-' + std.strReplace(name, '.json', '');
Expand All @@ -67,7 +75,31 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
] else [],
dashboardSources:
local configMap = k.core.v1.configMap;
local dashboardSources = import 'configs/dashboard-sources/dashboards.libsonnet';
local dashboardSources = {
apiVersion: 1,
providers: [
{
name: '0',
orgId: 1,
folder: 'Default',
type: 'file',
options: {
path: '/grafana-dashboard-definitions/0',
},
},
] + [
{
name: folder,
orgId: 1,
folder: folder,
type: 'file',
options: {
path: '/grafana-dashboard-definitions/' + folder
}
}
for folder in std.objectFields($._config.grafana.folderDashboards)
],
};

configMap.new('grafana-dashboards', { 'dashboards.yaml': std.manifestJsonEx(dashboardSources, ' ') }) +
configMap.mixin.metadata.withNamespace($._config.namespace),
Expand Down Expand Up @@ -134,6 +166,12 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
containerVolumeMount.new('grafana-dashboard-' + dashboardName, '/grafana-dashboard-definitions/0/' + dashboardName)
for name in std.objectFields($._config.grafana.dashboards)
] +
[
local dashboardName = std.strReplace(name, '.json', '');
containerVolumeMount.new('grafana-dashboard-' + dashboardName, '/grafana-dashboard-definitions/' + folder + '/' + dashboardName)
for folder in std.objectFields($._config.grafana.folderDashboards)
for name in std.objectFields($._config.grafana.folderDashboards[folder])
] +
[
local dashboardName = std.strReplace(name, '.json', '');
containerVolumeMount.new('grafana-dashboard-' + dashboardName, '/grafana-dashboard-definitions/0/' + dashboardName)
Expand All @@ -154,6 +192,13 @@ local k = import 'ksonnet/ksonnet.beta.4/k.libsonnet';
volume.mixin.configMap.withName(dashboardName)
for name in std.objectFields($._config.grafana.dashboards)
] +
[
local dashboardName = 'grafana-dashboard-' + std.strReplace(name, '.json', '');
volume.withName(dashboardName) +
volume.mixin.configMap.withName(dashboardName)
for folder in std.objectFields($._config.grafana.folderDashboards)
for name in std.objectFields($._config.grafana.folderDashboards[folder])
] +
[
local dashboardName = 'grafana-dashboard-' + std.strReplace(name, '.json', '');
volume.withName(dashboardName) +
Expand Down

0 comments on commit c80d495

Please sign in to comment.