forked from grafana/jsonnet-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy-custom.libsonnet
152 lines (137 loc) · 4.65 KB
/
legacy-custom.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
// legacy-custom.libsonnet retrofits k8s-libsonnet functionality into ksonnet-lib
{
core+: {
v1+: {
configMap+: {
// allow configMap without data
new(name, data={})::
super.new(name, data),
withData(data)::
// don't add 'data' key if data={}
if (data == {}) then {}
else super.withData(data),
withDataMixin(data)::
// don't add 'data' key if data={}
if (data == {}) then {}
else super.withDataMixin(data),
},
volume+:: {
// Make items parameter optional from fromConfigMap
fromConfigMap(name, configMapName, configMapItems=[])::
{
configMap+:
if configMapItems == [] then { items:: null }
else {},
}
+ super.fromConfigMap(name, configMapName, configMapItems),
// Shortcut constructor for secret volumes.
fromSecret(name, secretName)::
super.withName(name) +
super.mixin.secret.withSecretName(secretName),
// Rename emptyDir to claimName
fromPersistentVolumeClaim(name='', claimName=''):: super.fromPersistentVolumeClaim(name=name, emptyDir=claimName),
},
volumeMount+:: {
// Override new, such that it doesn't always set readOnly: false.
new(name, mountPath, readOnly=false)::
{} + self.withName(name) + self.withMountPath(mountPath) +
if readOnly
then self.withReadOnly(readOnly)
else {},
},
containerPort+:: {
// Shortcut constructor for UDP ports.
newNamedUDP(name, containerPort)::
super.newNamed(name=name, containerPort=containerPort) +
super.withProtocol('UDP'),
},
persistentVolumeClaim+:: {
new(name='')::
super.new()
+ (if name != ''
then super.mixin.metadata.withName(name)
else {}),
},
container+:: {
withEnvMixin(es)::
// if an envvar has an empty value ("") we want to remove that property
// because k8s will remove that and then it would always
// show up as a difference.
local removeEmptyValue(obj) =
if std.objectHas(obj, 'value') && std.length(obj.value) == 0 then
{
[k]: obj[k]
for k in std.objectFields(obj)
if k != 'value'
}
else
obj;
super.withEnvMixin([
removeEmptyValue(envvar)
for envvar in es
]),
withEnvMap(es)::
self.withEnvMixin([
$.core.v1.envVar.new(k, es[k])
for k in std.objectFields(es)
]),
},
},
},
batch+: {
v1beta1+: {
cronJob+: {
new(name='', schedule='', containers=[])::
super.new()
+ super.mixin.spec.jobTemplate.spec.template.spec.withContainers(containers)
+ (if name != ''
then
super.mixin.metadata.withName(name)
+ super.mixin.spec.jobTemplate.spec.template.metadata.withLabels({ name: name })
else {})
+ (
if schedule != ''
then super.mixin.spec.withSchedule(schedule)
else {}
),
},
},
},
local appsExtentions = {
daemonSet+: {
new(name, containers, podLabels={})::
local labels = podLabels { name: name };
super.new() +
super.mixin.metadata.withName(name) +
super.mixin.spec.template.metadata.withLabels(labels) +
super.mixin.spec.template.spec.withContainers(containers) +
// apps.v1 requires an explicit selector:
super.mixin.spec.selector.withMatchLabels(labels),
},
deployment+: {
new(name, replicas, containers, podLabels={})::
local labels = podLabels { name: name };
super.new(name, replicas, containers, labels) +
// apps.v1 requires an explicit selector:
super.mixin.spec.selector.withMatchLabels(labels),
},
statefulSet+: {
new(name, replicas, containers, volumeClaims=[], podLabels={})::
local labels = podLabels { name: name };
super.new(name, replicas, containers, volumeClaims, labels) +
// apps.v1 requires an explicit selector:
super.mixin.spec.selector.withMatchLabels(labels) +
// remove volumeClaimTemplates if empty
// (otherwise it will create a diff all the time)
(
if std.length(volumeClaims) > 0
then super.mixin.spec.withVolumeClaimTemplates(volumeClaims)
else {}
),
},
},
apps+: {
v1beta1+: appsExtentions,
v1+: appsExtentions,
},
}