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
|
// util.libsonnet provides a number of useful (opinionated) shortcuts to replace boilerplate code
local util(k) = {
// mapToFlags converts a map to a set of golang-style command line flags.
mapToFlags(map, prefix='-'): [
'%s%s=%s' % [prefix, key, map[key]]
for key in std.objectFields(map)
if map[key] != null
],
// 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={})::
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.fromConfigMap(name, name),
]),
// 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={})::
local name = configMap.metadata.name,
hash = std.md5(std.toString(configMap)),
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.fromConfigMap(name, name),
]) +
deployment.mixin.spec.template.metadata.withAnnotationsMixin({
['%s-hash' % name]: hash,
}),
hostVolumeMount(name, hostPath, 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(name, path, readOnly=readOnly) +
volumeMountMixin,
);
deployment.mapContainers(addMount) +
deployment.mixin.spec.template.spec.withVolumesMixin([
volume.fromHostPath(name, hostPath),
]),
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');
f(std.toString(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 {})
),
resourcesLimits(cpu, memory)::
k.core.v1.container.mixin.resources.withLimits(
(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),
}
|