-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkustomize.libsonnet
253 lines (237 loc) · 8.36 KB
/
kustomize.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
// An implementation of the core primitives of kustomize, in jsonnet.
// .. as documented by the comments in
// https://github.com/kubernetes-sigs/kustomize/blob/master/docs/kustomization.yaml
//
// Each of these is implemented as a "factory" function that returns a
// function that acts on a single k8s resource. These can then be
// applied using a common std.map() or similar.
{
// Handy helper that applies multiple transformations to a single object.
applyList(funcs):: function(o) std.foldl(function(result, f) f(result), funcs, o),
// Add a namespace.
namespace(ns):: function(o) (
// Exclude non-namespaced resources
// See kubectl api-resources --namespaced=false
local nonNamespaced = std.set([
"ComponentStatus",
"Namespace",
"Node",
"PersistentVolume",
"MutatingWebhookConfiguration",
"ValidatingWebhookConfiguration",
"CustomResourceDefinition",
"APIService",
"TokenReview",
"SelfSubjectAccessReview",
"SelfSubjectRulesReview",
"SubjectAccessReview",
"CertificateSigningRequest",
"PodSecurityPolicy",
"ClusterRoleBinding",
"ClusterRole",
"PriorityClass",
"CSIDriver",
"CSINode",
"StorageClass",
"VolumeAttachment",
]);
if std.objectHas(o, "kind") && !std.setMember(o.kind, nonNamespaced) then o + {
metadata+: {
namespace: ns
}
} else o
),
// Prepend the value to the resource name.
namePrefix(p):: function(o) o + {metadata+: {name: p + super.name}},
// Append the value to the resource name.
nameSuffix(s):: function(o) o + {metadata+: {name+: s}},
// Add the labels to all resources and selectors.
commonLabels(l):: function(o) (
// Aside: The kustomize code is quite inconsistent on what it uses
// as the match criteria.
// Kustomize's list is in https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/transformers/config/defaultconfig/commonlabels.go
local gv = std.split(o.apiVersion, "/");
local g = if std.length(gv) == 1 then "" else gv[0];
local v = if std.length(gv) == 1 then gv[0] else gv[1];
local k = o.kind;
o + {
metadata+: {labels+: l},
} +
if (v == "v1" && k == "Service") then {
spec+: {selector+: l},
} else {} +
if (v == "v1" && k == "ReplicationController") then {
spec+: {
selector+: l,
template+: {metadata+: {labels+: l}},
},
} else {} +
if (k == "Deployment" || k == "ReplicaSet" || k == "DaemonSet") then {
spec+: {
selector+: {matchLabels+: l},
template+: {metadata+: {labels+: l}},
},
} else {} +
if (g == "apps" && (k == "Deployment" || k == "StatefulSet")) then {
spec+: {
template+: {
spec+: {
affinity+: {
podAffinity+: {
preferredDuringSchedulingIgnoredDuringExecution+: {
podAffinityTerm+: {
labelSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
},
requiredDuringSchedulingIgnoredDuringExecution+: {
labelSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
},
podAntiAffinity+: {
preferredDuringSchedulingIgnoredDuringExecution+: {
podAffinityTerm+: {
labelSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
},
requiredDuringSchedulingIgnoredDuringExecution+: {
labelSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
},
},
},
},
},
} else {} +
if (g == "apps" && k == "StatefulSet") then {
spec+: {volumeClaimTemplates+: {metadata+: {labels+: l}}},
} else {} +
if (g == "batch" && k == "Job") then {
spec+: {
selector+: {[if super.matchLabels then "matchLabels"]+: l},
template+: {metadata+: {labels+: l}},
},
} else {} +
if (g == "batch" && k == "CronJob") then {
spec+: {
jobTemplate+: {
metadata+: {labels+: l},
spec+: {
selector+: {[if super.matchLabels then "matchLabels"]+: l},
template+: {metadata+: {labels+: l}},
},
},
},
} else {} +
if (g == "policy" && k == "PodDisruptionBudget") then {
spec+: {
selector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
} else {} +
if (g == "networking.k8s.io" && k == "NetworkPolicy") then {
spec+: {
podSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
ingress+: {
from+: {
podSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
},
egress+: {
to+: {
podSelector+: {
[if super.matchLabels then "matchLabels"]+: l,
},
},
},
},
} else {}
),
commonAnnotations(a):: function(o) (
local gv = std.split(o.apiVersion, "/");
local g = if std.length(gv) == 1 then "" else gv[0];
local v = if std.length(gv) == 1 then gv[0] else gv[1];
local k = o.kind;
o + {
metadata+: {annotations+: a},
} +
if (
(v == "v1" && k == "ReplicationController") ||
(k == "Deployment") ||
(k == "ReplicaSet") ||
(k == "DaemonSet") ||
(k == "StatefulSet") ||
(g == "batch" && k == "Job")
) then {
spec+: {template+: {metadata+: {annotations+: a}}}
} else {} +
if (g == "batch" && k == "CronJob") then {
spec+: {
jobTemplate+: {
metadata+: {annotations+: a},
spec+: {template+: {metadata+: {annotations+: a}}},
},
},
} else {}
),
resources:: error "not relevant - I/O needs to be done separately and explicitly in jsonnet",
bases:: error "not relevant - I/O needs to be done separately and explicitly in jsonnet",
configMapGenerator:: error "TODO? unclear what makes sense here",
secretGenerator:: error "TODO? unclear what makes sense here",
generatorOptions:: error "not applicable",
patchesStrategicMerge(o):: error "TODO(kubecfg): No s-m-p implementation exposed to jsonnet yet.",
// FYI: Jsonnet's std.mergePatch does RFC7396 json-merge-patch,
// which is not json-patch.
patchesJson6902(target, patch):: error "TODO: No json-patch implementation for jsonnet yet.",
crds: error "TODO(gus): think about how to merge this with above functions",
vars(vars):: function(o) (
error "FIXME: not yet implemented"
),
// Modify image names/tags/digests.
// `images` is an array of objects describing the desired modifications.
images(images):: function(o) (
local newImage(old, img) = (
local nametag = std.split(old, ":");
local name = nametag[0];
local tag = if std.length(nametag) > 1 then nametag[1] else "latest";
if name == img.name then (
local newName = if std.objectHas(img, "newName") then img.newName else name;
local newTag = if std.objectHas(img, "newTag") then img.newTag else tag;
local newDigest = if std.objectHas(img, "digest") then img.digest else null;
newName + if newDigest != null then ("@" + newDigest) else (":" + newTag)
) else old
);
// NB: Unlike other kustomize transformers, which act on specific
// paths only, "images" walks the object looking for anything
// matching "containers" or "initContainers".
local visitAll(x, f) = (
local recurse(o) = visitAll(f(o), f);
if std.isArray(x) then [recurse(o) for o in x]
else if std.isObject(x) then {[k]: recurse(x[k]) for k in std.objectFields(x)}
else f(x)
);
local updateContainer(o) = (
if std.isObject(o) then o + {
[if std.objectHas(o, "containers") then "containers"]: [
c + {image: std.foldl(newImage, images, super.image)} for c in super.containers
],
[if std.objectHas(o, "initContainers") then "initContainers"]: [
c + {image: std.foldl(newImage, images, super.image)} for c in super.initContainers
],
} else o
);
visitAll(o, updateContainer)
),
}