-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkube.libsonnet
690 lines (591 loc) · 20.5 KB
/
kube.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// Generic library of Kubernetes objects
//
// Objects in this file follow the regular Kubernetes API object
// schema with two exceptions:
//
// ## Optional helpers
//
// A few objects have defaults or additional "helper" hidden
// (double-colon) fields that will help with common situations. For
// example, `Service.target_pod` generates suitable `selector` and
// `ports` blocks for the common case of a single-pod/single-port
// service. If for some reason you don't want the helper, just
// provide explicit values for the regular Kubernetes fields that the
// helper *would* have generated, and the helper logic will be
// ignored.
//
// ## The Underscore Convention:
//
// Various constructs in the Kubernetes API use JSON arrays to
// represent unordered sets or named key/value maps. This is
// particularly annoying with jsonnet since we want to use jsonnet's
// powerful object merge operation with these constructs.
//
// To combat this, this library attempts to provide more "jsonnet
// native" variants of these arrays in alternative hidden fields that
// end with an underscore. For example, the `env_` block in
// `Container`:
// ```
// kube.Container("foo") {
// env_: { FOO: "bar" },
// }
// ```
// ... produces the expected `container.env` JSON array:
// ```
// {
// "env": [
// { "name": "FOO", "value": "bar" }
// ]
// }
// ```
//
// If you are confused by the underscore versions, or don't want them
// in your situation then just ignore them and set the regular
// non-underscore field as usual.
//
//
// ## TODO
//
// TODO: Expand this to include all API objects.
//
// Should probably fill out all the defaults here too, so jsonnet can
// reference them. In addition, jsonnet validation is more useful
// (client-side, and gives better line information).
{
// Returns array of values from given object. Does not include hidden fields.
objectValues(o):: [o[field] for field in std.objectFields(o)],
// Returns array of [key, value] pairs from given object. Does not include hidden fields.
objectItems(o):: [[k, o[k]] for k in std.objectFields(o)],
// Replace all occurrences of `_` with `-`.
hyphenate(s):: std.join("-", std.split(s, "_")),
// Convert an octal (as a string) to number,
parseOctal(s):: (
local len = std.length(s);
local leading = std.substr(s, 0, len-1);
local last = std.substr(s, len-1, 1);
std.parseInt(last) + (if len > 1 then 8 * $.parseOctal(leading) else 0)
),
// Convert {foo: {a: b}} to [{name: foo, a: b}]
mapToNamedList(o):: [{ name: $.hyphenate(n) } + o[n] for n in std.objectFields(o)],
// Convert from SI unit suffixes to regular number
siToNum(n):: (
local convert =
if std.endsWith(n, "m") then [1, 0.001]
else if std.endsWith(n, "K") then [1, 1e3]
else if std.endsWith(n, "M") then [1, 1e6]
else if std.endsWith(n, "G") then [1, 1e9]
else if std.endsWith(n, "T") then [1, 1e12]
else if std.endsWith(n, "P") then [1, 1e15]
else if std.endsWith(n, "E") then [1, 1e18]
else if std.endsWith(n, "Ki") then [2, std.pow(2, 10)]
else if std.endsWith(n, "Mi") then [2, std.pow(2, 20)]
else if std.endsWith(n, "Gi") then [2, std.pow(2, 30)]
else if std.endsWith(n, "Ti") then [2, std.pow(2, 40)]
else if std.endsWith(n, "Pi") then [2, std.pow(2, 50)]
else if std.endsWith(n, "Ei") then [2, std.pow(2, 60)]
else error "Unknown numerical suffix in " + n;
local n_len = std.length(n);
std.parseInt(std.substr(n, 0, n_len - convert[0])) * convert[1]
),
local remap(v, start, end, newstart) =
if v >= start && v <= end then v - start + newstart else v,
local remapChar(c, start, end, newstart) =
std.char(remap(
std.codepoint(c), std.codepoint(start), std.codepoint(end), std.codepoint(newstart))),
toLower(s):: (
std.join("", [remapChar(c, "A", "Z", "a") for c in std.stringChars(s)])
),
toUpper(s):: (
std.join("", [remapChar(c, "a", "z", "A") for c in std.stringChars(s)])
),
_Object(apiVersion, kind, name):: {
local this = self,
apiVersion: apiVersion,
kind: kind,
metadata: {
name: name,
labels: { name: std.join("-", std.split(this.metadata.name, ":")) },
},
},
List(): {
apiVersion: "v1",
kind: "List",
items_:: {},
items: $.objectValues(self.items_),
},
Namespace(name): $._Object("v1", "Namespace", name) {
},
Endpoints(name): $._Object("v1", "Endpoints", name) {
Ip(addr):: { ip: addr },
Port(p):: { port: p },
subsets: [],
},
Service(name): $._Object("v1", "Service", name) {
local service = self,
target_pod:: error "service target_pod required",
port:: self.target_pod.spec.containers[0].ports[0].containerPort,
// Helpers that format host:port in various ways
host:: "%s.%s.svc" % [self.metadata.name, self.metadata.namespace],
host_colon_port:: "%s:%s" % [self.host, self.spec.ports[0].port],
http_url:: "http://%s/" % self.host_colon_port,
proxy_urlpath:: "/api/v1/proxy/namespaces/%s/services/%s/" % [
self.metadata.namespace, self.metadata.name,
],
// Useful in Ingress rules
name_port:: {
service: {
name: service.metadata.name,
local p = service.spec.ports[0].port,
port: {
[if std.isString(p) then "name"]: p,
[if std.isNumber(p) then "number"]: p,
},
},
},
spec: {
selector: service.target_pod.metadata.labels,
ports: [
{
local p = {protocol: "TCP"} + service.target_pod.spec.containers[0].ports[0],
port: service.port,
targetPort: p.containerPort,
protocol: p.protocol,
},
],
type: "ClusterIP",
},
},
PersistentVolume(name): $._Object("v1", "PersistentVolume", name) {
spec: {},
},
// TODO: This is a terrible name
PersistentVolumeClaimVolume(pvc): {
persistentVolumeClaim: { claimName: pvc.metadata.name },
},
StorageClass(name): $._Object("storage.k8s.io/v1", "StorageClass", name) {
provisioner: error "provisioner required",
},
PersistentVolumeClaim(name): $._Object("v1", "PersistentVolumeClaim", name) {
local pvc = self,
storageClass:: null,
storage:: error "storage required",
metadata+: if pvc.storageClass != null then {
annotations+: {
"volume.beta.kubernetes.io/storage-class": pvc.storageClass,
},
} else {},
spec: {
// PVC server-side validation (1.8) gets upset about a "change",
// even if this field is "" or explictly set to the default -
// even though those are all semantically equal.
[if pvc.storageClass != null then "storageClassName"]: pvc.storageClass,
resources: {
requests: {
storage: pvc.storage,
},
},
accessModes: ["ReadWriteOnce"],
},
},
Container(name): {
name: name,
image: error "container image value required",
imagePullPolicy: if std.endsWith(self.image, ":latest") then "Always" else "IfNotPresent",
envList(map):: [
if std.type(map[x]) == "object" then { name: x, valueFrom: map[x] } else { name: x, value: std.toString(map[x]) }
for x in std.objectFields(map)],
env_:: {},
env: self.envList(self.env_),
args_:: {},
args: ["--%s=%s" % kv for kv in $.objectItems(self.args_)],
// Defining `args` nukes the container's `CMD` (but not
// `ENTRYPOINT`), unless `command` is also explicitly defined. I
// cannot count the number of times this has hurt me.
//assert std.length(self.args) == 0 || std.length(self.command) > 0 :
//"args[] defined without command[]. This is almost certainly not what you want.",
// Unfortunately not that simple, since containers that use
// `ENTRYPOINT` _are_ ok with just `args`
ports_:: {},
ports: $.mapToNamedList(self.ports_),
volumeMounts_:: {},
volumeMounts: $.mapToNamedList(self.volumeMounts_),
stdin: false,
tty: false,
assert !self.tty || self.stdin : "tty=true requires stdin=true",
},
PodDisruptionBudget(name): $._Object("policy/v1", "PodDisruptionBudget", name) {
local this = self,
target_pod:: error "target_pod required",
spec: {
assert std.objectHas(self, "minAvailable") || std.objectHas(self, "maxUnavailable") : "exactly one of minAvailable/maxUnavailable required",
selector: {
matchLabels: this.target_pod.metadata.labels,
},
},
},
Pod(name): $._Object("v1", "Pod", name) {
local this = self,
metadata+: {
annotations+: {
[if this.spec.default_container in this.spec.containers_ then "kubectl.kubernetes.io/default-container"]: this.spec.containers_[this.spec.default_container].name,
},
},
spec: $.PodSpec,
},
PodSpec: {
// The 'first' container is used in various defaults in k8s.
local container_names = std.objectFields(self.containers_),
default_container:: container_names[0],
containers_:: {},
local container_names_ordered = [self.default_container] + [n for n in container_names if n != self.default_container],
containers: [{ name: $.hyphenate(name) } + self.containers_[name] for name in container_names_ordered if self.containers_[name] != null],
initContainers_:: {},
initContainers: [{name: $.hyphenate(name) } + self.initContainers_[name] for name in std.objectFields(self.initContainers_) if self.initContainers_[name] != null],
volumes_:: {},
volumes: $.mapToNamedList(self.volumes_),
// hack to disable below check for StatefulSets :(
volumesIsAuthoritative_:: true,
local declared_volumes = std.set([v.name for v in self.volumes]),
local used_volumes = std.set([v.name for c in self.containers + self.initContainers
for v in ({volumeMounts: []} + c).volumeMounts
]),
local missing_volumes = std.setDiff(used_volumes, declared_volumes),
assert !self.volumesIsAuthoritative_ || std.length(missing_volumes) == 0 : "Referenced but not in volumeMounts: %s" % [missing_volumes],
local extra_volumes = std.setDiff(declared_volumes, used_volumes),
assert std.length(extra_volumes) == 0 : "Unused volume in volumeMounts: %s" % [extra_volumes],
terminationGracePeriodSeconds: 30,
assert std.length(self.containers) > 0 : "must have at least one container",
},
EmptyDirVolume(): {
emptyDir: {},
},
HostPathVolume(path, type=""): {
hostPath: { path: path, type: type },
},
GitRepoVolume(repository, revision): {
gitRepo: {
repository: repository,
// "master" is possible, but should be avoided for production
revision: revision,
},
},
SecretVolume(secret): {
secret: { secretName: secret.metadata.name },
},
ConfigMapVolume(configmap): {
configMap: { name: configmap.metadata.name },
},
ConfigMap(name): $._Object("v1", "ConfigMap", name) {
data: {},
// I keep thinking data values can be any JSON type. This check
// will remind me that they must be strings :(
local nonstrings = [k for k in std.objectFields(self.data)
if std.type(self.data[k]) != "string"],
assert std.length(nonstrings) == 0 : "data contains non-string values: %s" % [nonstrings],
},
// subtype of EnvVarSource
ConfigMapRef(configmap, key): {
assert std.objectHas(configmap.data, key) : "%s not in configmap.data" % [key],
configMapKeyRef: {
name: configmap.metadata.name,
key: key,
},
},
Secret(name): $._Object("v1", "Secret", name) {
local secret = self,
type: "Opaque",
data_:: {},
data: { [k]: std.base64(secret.data_[k]) for k in std.objectFields(secret.data_) },
},
// subtype of EnvVarSource
SecretKeyRef(secret, key): {
assert std.objectHas(secret.data, key) : "%s not in secret.data" % [key],
secretKeyRef: {
name: secret.metadata.name,
key: key,
},
},
// subtype of EnvVarSource
FieldRef(key): {
fieldRef: {
apiVersion: "v1",
fieldPath: key,
},
},
// subtype of EnvVarSource
ResourceFieldRef(key, divisor="1"): {
resourceFieldRef: {
resource: key,
divisor: std.toString(divisor),
},
},
Deployment(name): $._Object("apps/v1", "Deployment", name) {
local deployment = self,
spec: {
template: {
spec: $.PodSpec,
metadata: {
labels: deployment.metadata.labels,
local podspec = deployment.spec.template.spec,
annotations+: {
[if podspec.default_container in podspec.containers_ then "kubectl.kubernetes.io/default-container"]: podspec.containers_[podspec.default_container].name,
},
},
},
selector: {
matchLabels: deployment.spec.template.metadata.labels,
},
strategy: {
type: "RollingUpdate",
local pvcs = [v for v in deployment.spec.template.spec.volumes
if std.objectHas(v, "persistentVolumeClaim")],
local is_stateless = std.length(pvcs) == 0,
// Apps trying to maintain a majority quorum or similar will
// want to tune these carefully.
// NB: Upstream default is surge=1 unavail=1
rollingUpdate: if is_stateless then {
maxSurge: "25%", // rounds up
maxUnavailable: "25%", // rounds down
} else {
// Poor-man's StatelessSet. Useful mostly with replicas=1.
maxSurge: 0,
maxUnavailable: 1,
},
},
// NB: Upstream default is 0
minReadySeconds: 30,
replicas: 1,
assert self.replicas >= 1,
},
},
CrossVersionObjectReference(target): {
apiVersion: target.apiVersion,
kind: target.kind,
name: target.metadata.name,
},
HorizontalPodAutoscaler(name): $._Object("autoscaling/v1", "HorizontalPodAutoscaler", name) {
local hpa = self,
target:: error "target required",
spec: {
scaleTargetRef: $.CrossVersionObjectReference(hpa.target),
minReplicas: hpa.target.spec.replicas,
maxReplicas: error "maxReplicas required",
assert self.maxReplicas >= self.minReplicas,
},
},
StatefulSet(name): $._Object("apps/v1", "StatefulSet", name) {
local sset = self,
spec: {
serviceName: name,
updateStrategy: {
type: "RollingUpdate",
},
template: {
spec: $.PodSpec {volumesIsAuthoritative_: false},
metadata: {
labels: sset.metadata.labels,
local podspec = sset.spec.template.spec,
annotations+: {
[if podspec.default_container in podspec.containers_ then "kubectl.kubernetes.io/default-container"]: podspec.containers_[podspec.default_container].name,
},
},
},
selector: {
matchLabels: sset.spec.template.metadata.labels,
},
volumeClaimTemplates_:: {},
volumeClaimTemplates: [
// StatefulSet is overly fussy about "changes" (even when
// they're no-ops).
// In particular annotations={} is apparently a "change",
// since the comparison is ignorant of defaults.
std.prune($.PersistentVolumeClaim($.hyphenate(kv[0])) + {apiVersion:: null, kind:: null} + kv[1])
for kv in $.objectItems(self.volumeClaimTemplates_)],
replicas: 1,
assert self.replicas >= 1,
},
},
Job(name): $._Object("batch/v1", "Job", name) {
local job = self,
spec: $.JobSpec {
template+: {
metadata+: {
labels: job.metadata.labels,
},
},
},
},
// NB: kubernetes >= 1.8.x has batch/v1beta1 (olders were batch/v2alpha1)
CronJob(name): $._Object("batch/v1beta1", "CronJob", name) {
local cronjob = self,
spec: {
jobTemplate: {
spec: $.JobSpec {
template+: {
metadata+: {
labels: cronjob.metadata.labels,
},
},
},
},
schedule: error "Need to provide spec.schedule",
successfulJobsHistoryLimit: 10,
failedJobsHistoryLimit: 20,
// NB: upstream concurrencyPolicy default is "Allow"
concurrencyPolicy: "Forbid",
},
},
JobSpec: {
local this = self,
template: {
metadata+: {
local podspec = this.template.spec,
annotations+: {
[if podspec.default_container in podspec.containers_ then "kubectl.kubernetes.io/default-container"]: podspec.containers_[podspec.default_container].name,
},
},
spec: $.PodSpec {
restartPolicy: "OnFailure",
},
},
completions: 1,
parallelism: 1,
},
DaemonSet(name): $._Object("apps/v1", "DaemonSet", name) {
local ds = self,
spec: {
updateStrategy: {
type: "RollingUpdate",
rollingUpdate: {
maxUnavailable: 1,
},
},
// NB: Upstream default is 0
minReadySeconds: 30,
template: {
metadata: {
labels: ds.metadata.labels,
local podspec = ds.spec.template.spec,
annotations+: {
[if podspec.default_container in podspec.containers_ then "kubectl.kubernetes.io/default-container"]: podspec.containers_[podspec.default_container].name,
},
},
spec: $.PodSpec,
},
selector: {
matchLabels: ds.spec.template.metadata.labels,
},
},
},
Ingress(name): $._Object("networking.k8s.io/v1", "Ingress", name) {
spec: {},
local rel_paths = [
p.path
for r in self.spec.rules
for p in r.http.paths
if !std.startsWith(p.path, "/")
],
assert std.length(rel_paths) == 0 : "paths must be absolute: " + rel_paths,
},
IngressClass(name): $._Object("networking.k8s.io/v1", "IngressClass", name) {
spec: {},
},
CustomResourceDefinition(group, version, kind): {
local this = self,
apiVersion: "apiextensions.k8s.io/v1",
kind: "CustomResourceDefinition",
metadata+: {
name: this.spec.names.plural + "." + this.spec.group,
},
spec: {
scope: "Namespaced",
group: group,
versions_:: {
[version]: {
served: true,
storage: true,
schema: {
openAPIV3Schema: {
// This is the worst schema possible - you should override this.
type: "object",
"x-kubernetes-preserve-unknown-fields": true,
},
},
},
},
versions: $.mapToNamedList(self.versions_),
names: {
kind: kind,
singular: $.toLower(self.kind),
plural: self.singular + "s",
listKind: self.kind + "List",
},
},
// A "constructor" for this custom resource.
// ```
// local Foo = fooCRD.new;
// ```
// ... is sort-of equivalent to:
// ```
// local Foo(name) = kube._Object("foo/v1", "Foo", name);
// ```
new:: function(name) (
local gv = this.spec.group + "/" + this.spec.versions[0].name;
$._Object(gv, this.spec.names.kind, name)
),
},
ServiceAccount(name): $._Object("v1", "ServiceAccount", name) {
},
Role(name): $._Object("rbac.authorization.k8s.io/v1", "Role", name) {
rules: if std.objectHas(self, 'aggregationRule') then error("rules determined by aggregationRule") else [],
},
ClusterRole(name): $.Role(name) {
kind: "ClusterRole",
},
Group(name): {
kind: "Group",
name: name,
apiGroup: "rbac.authorization.k8s.io",
},
User(name): {
kind: "User",
name: name,
apiGroup: "rbac.authorization.k8s.io",
},
RoleBinding(name): $._Object("rbac.authorization.k8s.io/v1", "RoleBinding", name) {
local rb = self,
subjects_:: [],
subjects: [{
kind: o.kind,
namespace: o.metadata.namespace,
name: o.metadata.name,
} for o in self.subjects_],
roleRef_:: error "roleRef is required",
roleRef: {
apiGroup: "rbac.authorization.k8s.io",
kind: rb.roleRef_.kind,
name: rb.roleRef_.metadata.name,
},
},
ClusterRoleBinding(name): $.RoleBinding(name) {
kind: "ClusterRoleBinding",
},
// NB: most of the "helper" stuff comes from above (podLabelsSelector, podsPorts),
// NetworkPolicy returned object will have "Ingress", "Egress" policyTypes auto-set
// based on populated spec.ingress or spec.egress
// See tests/test-simple-validate.jsonnet for example(s).
NetworkPolicy(name): $._Object("networking.k8s.io/v1", "NetworkPolicy", name) {
local networkpolicy = self,
spec: {
podSelector: {},
policyTypes: std.prune([
if networkpolicy.spec.ingress != [] then "Ingress" else null,
if networkpolicy.spec.egress != [] then "Egress" else null,
]),
ingress: $.objectValues(self.ingress_),
ingress_:: {},
egress: $.objectValues(self.egress_),
egress_:: {},
},
},
}