Skip to content

Commit 6eec076

Browse files
committed
chore: refactor out variables for overriding
1 parent 8da4655 commit 6eec076

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

dashboards/resources/cluster.libsonnet

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
22
local defaultQueries = import './queries/cluster-queries.libsonnet';
3+
local defaultVariables = import './variables/cluster-variables.libsonnet';
34

45
local prometheus = g.query.prometheus;
56
local stat = g.panel.stat;
@@ -41,36 +42,10 @@ local var = g.dashboard.variable;
4142
then $._queries.cluster
4243
else defaultQueries;
4344

44-
local variables = {
45-
datasource:
46-
var.datasource.new('datasource', 'prometheus')
47-
+ var.datasource.withRegex($._config.datasourceFilterRegex)
48-
+ var.datasource.generalOptions.showOnDashboard.withLabelAndValue()
49-
+ var.datasource.generalOptions.withLabel('Data source')
50-
+ {
51-
current: {
52-
selected: true,
53-
text: $._config.datasourceName,
54-
value: $._config.datasourceName,
55-
},
56-
},
57-
58-
cluster:
59-
var.query.new('cluster')
60-
+ var.query.withDatasourceFromVariable(self.datasource)
61-
+ var.query.queryTypes.withLabelValues(
62-
$._config.clusterLabel,
63-
'up{%(cadvisorSelector)s}' % $._config,
64-
)
65-
+ var.query.generalOptions.withLabel('cluster')
66-
+ var.query.refresh.onTime()
67-
+ (
68-
if $._config.showMultiCluster
69-
then var.query.generalOptions.showOnDashboard.withLabelAndValue()
70-
else var.query.generalOptions.showOnDashboard.withNothing()
71-
)
72-
+ var.query.withSort(type='alphabetical'),
73-
};
45+
// Allow overriding variables via $._variables.cluster, otherwise use default
46+
local variables = if std.objectHas($, '_variables') && std.objectHas($._variables, 'cluster')
47+
then $._variables.cluster($._config)
48+
else defaultVariables.cluster($._config);
7449

7550
local links = {
7651
namespace: {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
2+
local var = g.dashboard.variable;
3+
4+
{
5+
// Cluster dashboard variables
6+
// Returns both datasource and cluster variables
7+
cluster(config):: {
8+
datasource:
9+
var.datasource.new('datasource', 'prometheus')
10+
+ var.datasource.withRegex(config.datasourceFilterRegex)
11+
+ var.datasource.generalOptions.showOnDashboard.withLabelAndValue()
12+
+ var.datasource.generalOptions.withLabel('Data source')
13+
+ {
14+
current: {
15+
selected: true,
16+
text: config.datasourceName,
17+
value: config.datasourceName,
18+
},
19+
},
20+
21+
cluster:
22+
var.query.new('cluster')
23+
+ var.query.withDatasourceFromVariable(self.datasource)
24+
+ var.query.queryTypes.withLabelValues(
25+
config.clusterLabel,
26+
'up{%(cadvisorSelector)s}' % config,
27+
)
28+
+ var.query.generalOptions.withLabel('cluster')
29+
+ var.query.refresh.onTime()
30+
+ (
31+
if config.showMultiCluster
32+
then var.query.generalOptions.showOnDashboard.withLabelAndValue()
33+
else var.query.generalOptions.showOnDashboard.withNothing()
34+
)
35+
+ var.query.withSort(type='alphabetical'),
36+
},
37+
}
38+

0 commit comments

Comments
 (0)