-
Notifications
You must be signed in to change notification settings - Fork 163
/
util.libsonnet
309 lines (271 loc) · 11.1 KB
/
util.libsonnet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// util.libsonnet provides a number of useful (opinionated) shortcuts to replace boilerplate code
local util(k) = {
// checkFlagsMap checks map for presence of flags that values with both with and without prefix set ('foo' and '-foo').
checkFlagsMap(map, prefix): [
error 'key "%(key)s" provided with value "%(value)s" but key "%(prefix)s%(key)s" was provided too with value "%(otherValue)s", if want to ignore this, set check=false in mapToFlags' %
{ key: key, value: map[key], prefix: prefix, otherValue: map[prefix + key] }
for key in std.objectFields(map)
if map[key] != null && std.objectHas(map, prefix + key) && map[prefix + key] != null
],
// mapToFlags converts a map to a set of golang-style command line flags.
// if check=true, it will check for 'foo' and '-foo' presence, failing in that case.
mapToFlags(map, prefix='-', check=true): [
'%s%s=%s' % [prefix, key, map[key]]
for key in std.objectFields(map)
if map[key] != null
] + if check then $.checkFlagsMap(map, prefix) else [],
// serviceFor create service for a given deployment.
serviceFor(deployment, ignored_labels=[], nameFormat='%(container)s-%(port)s')::
local container = k.core.v1.container;
local service = k.core.v1.service;
local servicePort = k.core.v1.servicePort;
local ports = [
servicePort.newNamed(
name=(nameFormat % { container: c.name, port: port.name }),
port=port.containerPort,
targetPort=port.containerPort
) +
if std.objectHas(port, 'protocol')
then servicePort.withProtocol(port.protocol)
else {}
for c in deployment.spec.template.spec.containers
for port in (c + container.withPortsMixin([])).ports
];
local labels = {
[x]: deployment.spec.template.metadata.labels[x]
for x in std.objectFields(deployment.spec.template.metadata.labels)
if std.count(ignored_labels, x) == 0
};
service.new(
deployment.metadata.name, // name
labels, // selector
ports,
) +
service.mixin.metadata.withLabels({ name: deployment.metadata.name }),
// rbac creates a service account, role and role binding with the given
// name and rules.
rbac(name, rules, namespace):: {
local clusterRole = k.rbac.v1.clusterRole,
local clusterRoleBinding = k.rbac.v1.clusterRoleBinding,
local subject = k.rbac.v1.subject,
local serviceAccount = k.core.v1.serviceAccount,
service_account:
serviceAccount.new(name),
cluster_role:
clusterRole.new() +
clusterRole.mixin.metadata.withName(name) +
clusterRole.withRules(rules),
cluster_role_binding:
clusterRoleBinding.new() +
clusterRoleBinding.mixin.metadata.withName(name) +
clusterRoleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
clusterRoleBinding.mixin.roleRef.withKind('ClusterRole') +
clusterRoleBinding.mixin.roleRef.withName(name) +
clusterRoleBinding.withSubjects([
subject.new() +
subject.withKind('ServiceAccount') +
subject.withName(name) +
subject.withNamespace(namespace),
]),
},
namespacedRBAC(name, rules, namespace):: {
local role = k.rbac.v1.role,
local roleBinding = k.rbac.v1.roleBinding,
local subject = k.rbac.v1.subject,
local serviceAccount = k.core.v1.serviceAccount,
service_account:
serviceAccount.new(name) +
serviceAccount.mixin.metadata.withNamespace(namespace),
role:
role.new() +
role.mixin.metadata.withName(name) +
role.mixin.metadata.withNamespace(namespace) +
role.withRules(rules),
role_binding:
roleBinding.new() +
roleBinding.mixin.metadata.withName(name) +
roleBinding.mixin.metadata.withNamespace(namespace) +
roleBinding.mixin.roleRef.withApiGroup('rbac.authorization.k8s.io') +
roleBinding.mixin.roleRef.withKind('Role') +
roleBinding.mixin.roleRef.withName(name) +
roleBinding.withSubjects([
subject.new() +
subject.withKind('ServiceAccount') +
subject.withName(name) +
subject.withNamespace(namespace),
]),
},
// VolumeMount helper functions can be augmented with mixins.
// For example, passing "volumeMount.withSubPath(subpath)" will result in
// a subpath mixin.
configVolumeMount(name, path, volumeMountMixin={})::
$.volumeMounts([$.volumeMountItem(name, path, volumeMountMixin)]),
// configMapVolumeMount adds a configMap to deployment-like objects.
// It will also add an annotation hash to ensure the pods are re-deployed
// when the config map changes.
configMapVolumeMount(configMap, path, volumeMountMixin={})::
$.volumeMounts([$.configMapVolumeMountItem(configMap, path, volumeMountMixin)]),
// configMapVolumeMountItem represents a config map to be mounted.
// It is used in the volumeMounts function
configMapVolumeMountItem(configMap, path, volumeMountMixin={})::
local name = configMap.metadata.name;
local annotations = { ['%s-hash' % name]: std.md5(std.toString(configMap)) };
$.volumeMountItem(name, path, volumeMountMixin, annotations),
// volumeMountItem represents a volume to be mounted.
// It is used in the volumeMounts function
volumeMountItem(name, path, volumeMountMixin={}, annotations={}):: {
name: name,
path: path,
volumeMountMixin: volumeMountMixin,
annotations: annotations,
},
// volumeMounts adds an array of volumeMountItem to deployment-like objects.
// It can also add a set of annotations for each mount
volumeMounts(mounts)::
local container = k.core.v1.container,
deployment = k.apps.v1.deployment,
volumeMount = k.core.v1.volumeMount,
volume = k.core.v1.volume;
local addMounts(c) = c + container.withVolumeMountsMixin([
volumeMount.new(m.name, m.path) +
m.volumeMountMixin
for m in mounts
]);
local annotations = std.foldl(
function(acc, ann) acc + ann,
[m.annotations for m in mounts],
{}
);
deployment.mapContainers(addMounts)
+ deployment.mixin.spec.template.spec.withVolumesMixin([
volume.fromConfigMap(m.name, m.name)
for m in mounts
])
+ (if annotations != {} then deployment.mixin.spec.template.metadata.withAnnotationsMixin(annotations) else {}),
hostVolumeMount(name, hostPath, path, readOnly=false, volumeMountMixin={}, volumeMixin={})::
local container = k.core.v1.container,
deployment = k.apps.v1.deployment,
volumeMount = k.core.v1.volumeMount,
volume = k.core.v1.volume,
addMount(c) = c + container.withVolumeMountsMixin(
volumeMount.new(name, path, readOnly=readOnly) +
volumeMountMixin,
);
deployment.mapContainers(addMount) +
deployment.mixin.spec.template.spec.withVolumesMixin([
volume.fromHostPath(name, hostPath) +
volumeMixin,
]),
pvcVolumeMount(pvcName, path, readOnly=false, volumeMountMixin={})::
local container = k.core.v1.container,
deployment = k.apps.v1.deployment,
volumeMount = k.core.v1.volumeMount,
volume = k.core.v1.volume,
addMount(c) = c + container.withVolumeMountsMixin(
volumeMount.new(pvcName, path, readOnly=readOnly) +
volumeMountMixin,
);
deployment.mapContainers(addMount) +
deployment.mixin.spec.template.spec.withVolumesMixin([
volume.fromPersistentVolumeClaim(pvcName, pvcName),
]),
secretVolumeMount(name, path, defaultMode=256, volumeMountMixin={})::
local container = k.core.v1.container,
deployment = k.apps.v1.deployment,
volumeMount = k.core.v1.volumeMount,
volume = k.core.v1.volume,
addMount(c) = c + container.withVolumeMountsMixin(
volumeMount.new(name, path) +
volumeMountMixin,
);
deployment.mapContainers(addMount) +
deployment.mixin.spec.template.spec.withVolumesMixin([
volume.fromSecret(name, secretName=name) +
volume.mixin.secret.withDefaultMode(defaultMode),
]),
emptyVolumeMount(name, path, volumeMountMixin={}, volumeMixin={})::
local container = k.core.v1.container,
deployment = k.apps.v1.deployment,
volumeMount = k.core.v1.volumeMount,
volume = k.core.v1.volume,
addMount(c) = c + container.withVolumeMountsMixin(
volumeMount.new(name, path) +
volumeMountMixin,
);
deployment.mapContainers(addMount) +
deployment.mixin.spec.template.spec.withVolumesMixin([
volume.fromEmptyDir(name) + volumeMixin,
]),
manifestYaml(value):: (
local f = std.native('manifestYamlFromJson');
if f != null
then f(std.toString(value))
else std.manifestYamlDoc(value)
),
resourcesRequests(cpu, memory)::
k.core.v1.container.mixin.resources.withRequests(
(if cpu != null
then { cpu: cpu }
else {}) +
(if memory != null
then { memory: memory }
else {})
),
resourcesRequestsMixin(cpu, memory)::
k.core.v1.container.mixin.resources.withRequestsMixin(
(if cpu != null
then { cpu: cpu }
else {}) +
(if memory != null
then { memory: memory }
else {})
),
resourcesLimits(cpu, memory)::
k.core.v1.container.mixin.resources.withLimits(
(if cpu != null
then { cpu: cpu }
else {}) +
(if memory != null
then { memory: memory }
else {})
),
resourcesLimitsMixin(cpu, memory)::
k.core.v1.container.mixin.resources.withLimitsMixin(
(if cpu != null
then { cpu: cpu }
else {}) +
(if memory != null
then { memory: memory }
else {})
),
antiAffinity:
{
local deployment = k.apps.v1.deployment,
local podAntiAffinity = deployment.mixin.spec.template.spec.affinity.podAntiAffinity,
local name = super.spec.template.metadata.labels.name,
spec+: podAntiAffinity.withRequiredDuringSchedulingIgnoredDuringExecution([
podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecutionType.new() +
podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecutionType.mixin.labelSelector.withMatchLabels({ name: name }) +
podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecutionType.withTopologyKey('kubernetes.io/hostname'),
]).spec,
},
antiAffinityStatefulSet:
{
local statefulSet = k.apps.v1.statefulSet,
local podAntiAffinity = statefulSet.mixin.spec.template.spec.affinity.podAntiAffinity,
local name = super.spec.template.metadata.labels.name,
spec+: podAntiAffinity.withRequiredDuringSchedulingIgnoredDuringExecution([
podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecutionType.new() +
podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecutionType.mixin.labelSelector.withMatchLabels({ name: name }) +
podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecutionType.withTopologyKey('kubernetes.io/hostname'),
]).spec,
},
// Add a priority to the pods in a deployment (or deployment-like objects
// such as a statefulset).
local deployment = k.apps.v1.deployment,
podPriority(p):
deployment.mixin.spec.template.spec.withPriorityClassName(p),
};
util((import 'grafana.libsonnet')) + {
withK(k):: util(k),
}