Skip to content

Commit 09ddf8e

Browse files
Group Grafana dashboard mounts into a single mapContainers (#671)
Currently, each `mapContainers` call adds an additional layer to the stack When we get to a number of dashboards that is too high, this can go above the default max stack limit for jsonnet
1 parent ccdd25d commit 09ddf8e

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

grafana/configmaps.libsonnet

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,24 @@ local configMap = k.core.v1.configMap;
125125
// Helper to mount a variable number of sharded config maps.
126126
local shardedMounts(folder) =
127127
local shards = calculateShards(folder);
128-
std.foldl(
129-
function(acc, shard)
130-
acc + k.util.configVolumeMount(shard, '/grafana/%s/%s' % [prefix(folder.id), shard]),
131-
std.objectFields(shards),
132-
{}
133-
),
128+
[
129+
k.util.volumeMountItem(shard, '/grafana/%s/%s' % [prefix(folder.id), shard])
130+
for shard in std.objectFields(shards)
131+
],
134132

135133
// configmap mounts for use within statefulset/deployment
136134
configmap_mounts::
137-
k.util.configMapVolumeMount($.grafana_ini_config_map, '/etc/grafana-config')
138-
+ k.util.configMapVolumeMount($.dashboard_provisioning_config_map, '%(provisioningDir)s/dashboards' % $._config)
139-
+ k.util.configMapVolumeMount($.grafana_datasource_config_map, '%(provisioningDir)s/datasources' % $._config)
140-
+ k.util.configMapVolumeMount($.notification_channel_config_map, '%(provisioningDir)s/notifiers' % $._config)
141-
+ std.foldr(
142-
function(folder, acc) shardedMounts($.grafanaDashboardFolders[folder]) + acc,
143-
std.objectFields($.grafanaDashboardFolders),
144-
{},
145-
),
135+
local mounts =
136+
[
137+
k.util.configMapVolumeMountItem($.grafana_ini_config_map, '/etc/grafana-config'),
138+
k.util.configMapVolumeMountItem($.dashboard_provisioning_config_map, '%(provisioningDir)s/dashboards' % $._config),
139+
k.util.configMapVolumeMountItem($.grafana_datasource_config_map, '%(provisioningDir)s/datasources' % $._config),
140+
k.util.configMapVolumeMountItem($.notification_channel_config_map, '%(provisioningDir)s/notifiers' % $._config),
141+
]
142+
+ std.flattenArrays([
143+
shardedMounts($.grafanaDashboardFolders[folder])
144+
for folder in std.objectFields($.grafanaDashboardFolders)
145+
]);
146+
147+
k.util.volumeMounts(mounts),
146148
}

ksonnet-util/util.libsonnet

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,42 +103,55 @@ local util(k) = {
103103
// For example, passing "volumeMount.withSubPath(subpath)" will result in
104104
// a subpath mixin.
105105
configVolumeMount(name, path, volumeMountMixin={})::
106-
local container = k.core.v1.container,
107-
deployment = k.apps.v1.deployment,
108-
volumeMount = k.core.v1.volumeMount,
109-
volume = k.core.v1.volume,
110-
addMount(c) = c + container.withVolumeMountsMixin(
111-
volumeMount.new(name, path) +
112-
volumeMountMixin,
113-
);
114-
115-
deployment.mapContainers(addMount) +
116-
deployment.mixin.spec.template.spec.withVolumesMixin([
117-
volume.fromConfigMap(name, name),
118-
]),
106+
$.volumeMounts([$.volumeMountItem(name, path, volumeMountMixin)]),
119107

120108
// configMapVolumeMount adds a configMap to deployment-like objects.
121109
// It will also add an annotation hash to ensure the pods are re-deployed
122110
// when the config map changes.
123111
configMapVolumeMount(configMap, path, volumeMountMixin={})::
124-
local name = configMap.metadata.name,
125-
hash = std.md5(std.toString(configMap)),
126-
container = k.core.v1.container,
112+
$.volumeMounts([$.configMapVolumeMountItem(configMap, path, volumeMountMixin)]),
113+
114+
115+
// configMapVolumeMountItem represents a config map to be mounted.
116+
// It is used in the volumeMounts function
117+
configMapVolumeMountItem(configMap, path, volumeMountMixin={})::
118+
local name = configMap.metadata.name;
119+
local annotations = { ['%s-hash' % name]: std.md5(std.toString(configMap)) };
120+
$.volumeMountItem(name, path, volumeMountMixin, annotations),
121+
122+
// volumeMountItem represents a volume to be mounted.
123+
// It is used in the volumeMounts function
124+
volumeMountItem(name, path, volumeMountMixin={}, annotations={}):: {
125+
name: name,
126+
path: path,
127+
volumeMountMixin: volumeMountMixin,
128+
annotations: annotations,
129+
},
130+
131+
// volumeMounts adds an array of volumeMountItem to deployment-like objects.
132+
// It can also add a set of annotations for each mount
133+
volumeMounts(mounts)::
134+
local container = k.core.v1.container,
127135
deployment = k.apps.v1.deployment,
128136
volumeMount = k.core.v1.volumeMount,
129-
volume = k.core.v1.volume,
130-
addMount(c) = c + container.withVolumeMountsMixin(
131-
volumeMount.new(name, path) +
132-
volumeMountMixin,
137+
volume = k.core.v1.volume;
138+
local addMounts(c) = c + container.withVolumeMountsMixin([
139+
volumeMount.new(m.name, m.path) +
140+
m.volumeMountMixin
141+
for m in mounts
142+
]);
143+
local annotations = std.foldl(
144+
function(acc, ann) acc + ann,
145+
[m.annotations for m in mounts],
146+
{}
133147
);
134148

135-
deployment.mapContainers(addMount) +
136-
deployment.mixin.spec.template.spec.withVolumesMixin([
137-
volume.fromConfigMap(name, name),
138-
]) +
139-
deployment.mixin.spec.template.metadata.withAnnotationsMixin({
140-
['%s-hash' % name]: hash,
141-
}),
149+
deployment.mapContainers(addMounts)
150+
+ deployment.mixin.spec.template.spec.withVolumesMixin([
151+
volume.fromConfigMap(m.name, m.name)
152+
for m in mounts
153+
])
154+
+ (if annotations != {} then deployment.mixin.spec.template.metadata.withAnnotationsMixin(annotations) else {}),
142155

143156
hostVolumeMount(name, hostPath, path, readOnly=false, volumeMountMixin={})::
144157
local container = k.core.v1.container,

0 commit comments

Comments
 (0)