-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk8s.libsonnet
More file actions
78 lines (75 loc) · 2.38 KB
/
Copy pathk8s.libsonnet
File metadata and controls
78 lines (75 loc) · 2.38 KB
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
{
flattenKubernetesObjects(object)::
if std.isObject(object)
then
// a Kubernetes object is characterized by having an apiVersion and Kind
if std.objectHas(object, 'apiVersion') && std.objectHas(object, 'kind')
then [object]
else
self.flattenKubernetesObjects(std.objectValues(object))
else if std.isArray(object)
then
std.flatMap(
function(obj)
self.flattenKubernetesObjects(obj),
object
)
else error 'not a valid kubernetes object: "%s"' % std.toString(object),
patchKubernetesObjects(object, patch, kind=null, name=null)::
if std.isObject(object)
then
// a Kubernetes object is characterized by having an apiVersion and Kind
if std.objectHas(object, 'apiVersion') && std.objectHas(object, 'kind')
&& (kind == null || object.kind == kind) && (name == null || object.metadata.name == name)
then object + patch
else
std.mapWithKey(
function(key, obj)
self.patchKubernetesObjects(obj, patch, kind, name),
object
)
else if std.isArray(object)
then
std.map(
function(obj)
self.patchKubernetesObjects(obj, patch, kind, name),
object
)
else object,
patchLabels(object, labels={})::
self.patchKubernetesObjects(
object,
{
metadata+: {
labels+: labels,
},
}
),
removeCPULimits(krd)::
// helper func: type safe std.get()
local get(object, field) = if std.isObject(object) then std.get(object, field, {}) else {};
// helper func: knocks out the cpu limit if present
local patch(c) =
if get(get(c, 'resources'), 'limits') != {} then
std.mergePatch(c, { resources: { limits: { cpu: null } } })
else c;
// Deployment like
if get(get(get(get(krd, 'spec'), 'template'), 'spec'), 'containers') != {} then
krd {
spec+: {
template+: {
spec+: {
containers: std.map(patch, super.containers),
[if std.objectHas(krd.spec.template.spec, 'initContainers') then 'initContainers']: std.map(patch, super.initContainers),
},
},
},
}
// CRDs, like prometheus.monitoring.coreos.com
else if get(get(get(krd, 'spec'), 'resources'), 'limits') != {} then
krd {
spec: patch(super.spec),
}
else
krd,
}