|
| 1 | +local d = import 'github.com/sh0rez/docsonnet/doc-util/main.libsonnet'; |
| 2 | +{ |
| 3 | + local this = self, |
| 4 | + |
| 5 | + '#':: d.pkg( |
| 6 | + name='helm-util', |
| 7 | + url='github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet', |
| 8 | + help='`helm-util` provides utilities for using helm in jsonnet', |
| 9 | + ), |
| 10 | + |
| 11 | + // This common label is usually set to 'Helm', this is not true anymore. |
| 12 | + // You can override this with any value you choose. |
| 13 | + // https://helm.sh/docs/chart_best_practices/labels/#standard-labels |
| 14 | + defaultLabels:: { 'app.kubernetes.io/managed-by': 'Helmraiser' }, |
| 15 | + |
| 16 | + '#template':: d.fn( |
| 17 | + ||| |
| 18 | + `template` expands the Helm Chart to it's underlying resources and returns them in an `Object`, |
| 19 | + so they can be consumed and modified from within Jsonnet. |
| 20 | +
|
| 21 | + This functionality requires Helmraiser support in Jsonnet (e.g. using Grafana Tanka) and also |
| 22 | + the `helm` binary installed on your `$PATH`. |
| 23 | + |||, |
| 24 | + [ |
| 25 | + d.arg('name', d.T.string), |
| 26 | + d.arg('chart', d.T.string), |
| 27 | + d.arg('conf', d.T.object), |
| 28 | + ] |
| 29 | + ), |
| 30 | + template(name, chart, conf={}):: |
| 31 | + this.patchLabels( |
| 32 | + std.native('helmTemplate')(name, chart, conf), |
| 33 | + defaultLabels |
| 34 | + ), |
| 35 | + |
| 36 | + '#patchKubernetesObjects':: d.fn( |
| 37 | + '`patchKubernetesObjects` finds all Kubernetes objects and patches them`', |
| 38 | + [ |
| 39 | + d.arg('object', d.T.object), |
| 40 | + d.arg('patch', d.T.object), |
| 41 | + ] |
| 42 | + ), |
| 43 | + patchKubernetesObjects(object, patch):: |
| 44 | + if std.isObject(object) |
| 45 | + then |
| 46 | + // a Kubernetes object is characterized by having an apiVersion and Kind |
| 47 | + if std.objectHas(object, 'apiVersion') && std.objectHas(object, 'kind') |
| 48 | + then object + patch |
| 49 | + else |
| 50 | + std.mapWithKey( |
| 51 | + function(key, obj) |
| 52 | + this.patchKubernetesObjects(obj, patch), |
| 53 | + object |
| 54 | + ) |
| 55 | + else if std.isArray(object) |
| 56 | + then |
| 57 | + std.map( |
| 58 | + function(obj) |
| 59 | + this.patchKubernetesObjects(obj, patch), |
| 60 | + object |
| 61 | + ) |
| 62 | + else object, |
| 63 | + |
| 64 | + '#patchLabels':: d.fn( |
| 65 | + '`patchLabels` finds all Kubernetes objects and adds labels', |
| 66 | + [ |
| 67 | + d.arg('object', d.T.object), |
| 68 | + d.arg('labels', d.T.object), |
| 69 | + ] |
| 70 | + ), |
| 71 | + patchLabels(object, labels={}):: |
| 72 | + this.patchKubernetesObjects( |
| 73 | + object, |
| 74 | + { |
| 75 | + metadata+: { |
| 76 | + labels+: labels, |
| 77 | + }, |
| 78 | + } |
| 79 | + ), |
| 80 | + |
| 81 | +} |
0 commit comments