Skip to content

Commit 6122dee

Browse files
committed
chore: refactor variables
1 parent 17bcf29 commit 6122dee

File tree

4 files changed

+84
-132
lines changed

4 files changed

+84
-132
lines changed
Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
1-
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
2-
local var = g.dashboard.variable;
1+
local common = import './common.libsonnet';
32

43
{
54
// Cluster dashboard variables
65
// 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-
},
6+
cluster(config)::
7+
local datasource = common.datasource(config);
8+
{
9+
datasource: datasource,
10+
cluster: common.cluster(config, datasource, 'up{%(cadvisorSelector)s}'),
11+
},
3712
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
2+
local var = g.dashboard.variable;
3+
4+
{
5+
datasource(config)::
6+
var.datasource.new('datasource', 'prometheus')
7+
+ var.datasource.withRegex(config.datasourceFilterRegex)
8+
+ var.datasource.generalOptions.showOnDashboard.withLabelAndValue()
9+
+ var.datasource.generalOptions.withLabel('Data source')
10+
+ {
11+
current: {
12+
selected: true,
13+
text: config.datasourceName,
14+
value: config.datasourceName,
15+
},
16+
},
17+
18+
cluster(config, datasourceVar, selectorTemplate)::
19+
var.query.new('cluster')
20+
+ var.query.withDatasourceFromVariable(datasourceVar)
21+
+ var.query.queryTypes.withLabelValues(
22+
config.clusterLabel,
23+
selectorTemplate % config,
24+
)
25+
+ var.query.generalOptions.withLabel('cluster')
26+
+ var.query.refresh.onTime()
27+
+ (
28+
if config.showMultiCluster
29+
then var.query.generalOptions.showOnDashboard.withLabelAndValue()
30+
else var.query.generalOptions.showOnDashboard.withNothing()
31+
)
32+
+ var.query.withSort(type='alphabetical'),
33+
34+
namespace(config, datasourceVar)::
35+
var.query.new('namespace')
36+
+ var.query.withDatasourceFromVariable(datasourceVar)
37+
+ var.query.queryTypes.withLabelValues(
38+
'namespace',
39+
'kube_namespace_status_phase{%(kubeStateMetricsSelector)s, %(clusterLabel)s="$cluster"}' % config,
40+
)
41+
+ var.query.generalOptions.withLabel('namespace')
42+
+ var.query.refresh.onTime()
43+
+ var.query.generalOptions.showOnDashboard.withLabelAndValue()
44+
+ var.query.withSort(type='alphabetical'),
45+
46+
pod(config, datasourceVar)::
47+
var.query.new('pod')
48+
+ var.query.withDatasourceFromVariable(datasourceVar)
49+
+ var.query.queryTypes.withLabelValues(
50+
'pod',
51+
'kube_pod_info{%(kubeStateMetricsSelector)s, %(clusterLabel)s="$cluster", namespace="$namespace"}' % config,
52+
)
53+
+ var.query.generalOptions.withLabel('pod')
54+
+ var.query.refresh.onTime()
55+
+ var.query.generalOptions.showOnDashboard.withLabelAndValue()
56+
+ var.query.withSort(type='alphabetical'),
57+
}
58+
Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,14 @@
1-
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
2-
local var = g.dashboard.variable;
1+
local common = import './common.libsonnet';
32

43
{
54
// Namespace dashboard variables
65
// Returns datasource, cluster, and namespace variables
7-
namespace(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{%(kubeStateMetricsSelector)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-
namespace:
38-
var.query.new('namespace')
39-
+ var.query.withDatasourceFromVariable(self.datasource)
40-
+ var.query.queryTypes.withLabelValues(
41-
'namespace',
42-
'kube_namespace_status_phase{%(kubeStateMetricsSelector)s, %(clusterLabel)s="$cluster"}' % config,
43-
)
44-
+ var.query.generalOptions.withLabel('namespace')
45-
+ var.query.refresh.onTime()
46-
+ var.query.generalOptions.showOnDashboard.withLabelAndValue()
47-
+ var.query.withSort(type='alphabetical'),
48-
},
6+
namespace(config)::
7+
local datasource = common.datasource(config);
8+
local clusterVar = common.cluster(config, datasource, 'up{%(kubeStateMetricsSelector)s}');
9+
{
10+
datasource: datasource,
11+
cluster: clusterVar,
12+
namespace: common.namespace(config, datasource),
13+
},
4914
}
Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,15 @@
1-
local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';
2-
local var = g.dashboard.variable;
1+
local common = import './common.libsonnet';
32

43
{
54
// Pod dashboard variables
65
// Returns datasource, cluster, namespace, and pod variables
7-
pod(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{%(kubeStateMetricsSelector)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-
namespace:
38-
var.query.new('namespace')
39-
+ var.query.withDatasourceFromVariable(self.datasource)
40-
+ var.query.queryTypes.withLabelValues(
41-
'namespace',
42-
'kube_namespace_status_phase{%(kubeStateMetricsSelector)s, %(clusterLabel)s="$cluster"}' % config,
43-
)
44-
+ var.query.generalOptions.withLabel('namespace')
45-
+ var.query.refresh.onTime()
46-
+ var.query.generalOptions.showOnDashboard.withLabelAndValue()
47-
+ var.query.withSort(type='alphabetical'),
48-
49-
pod:
50-
var.query.new('pod')
51-
+ var.query.withDatasourceFromVariable(self.datasource)
52-
+ var.query.queryTypes.withLabelValues(
53-
'pod',
54-
'kube_pod_info{%(kubeStateMetricsSelector)s, %(clusterLabel)s="$cluster", namespace="$namespace"}' % config,
55-
)
56-
+ var.query.generalOptions.withLabel('pod')
57-
+ var.query.refresh.onTime()
58-
+ var.query.generalOptions.showOnDashboard.withLabelAndValue()
59-
+ var.query.withSort(type='alphabetical'),
60-
},
6+
pod(config)::
7+
local datasource = common.datasource(config);
8+
local clusterVar = common.cluster(config, datasource, 'up{%(kubeStateMetricsSelector)s}');
9+
{
10+
datasource: datasource,
11+
cluster: clusterVar,
12+
namespace: common.namespace(config, datasource),
13+
pod: common.pod(config, datasource),
14+
},
6115
}

0 commit comments

Comments
 (0)