Skip to content

Commit c6590ac

Browse files
authored
feat: add helm-util (#328)
1 parent 8629e32 commit c6590ac

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

helm-util/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# helm-util
2+
3+
Provides utilities for using helm in jsonnet.
4+
5+
## Helmraiser
6+
7+
Helmraiser refers to a Go package that provides helmTemplate native function through Tanka for generating a Jsonnet
8+
library from any Helm chart. The goal of this function is to keep track of upstream changes in a Helm chart while
9+
retaining Jsonnet changes (read: mixins) on top of them.
10+
11+
## Example usage
12+
13+
```
14+
jb install github.com/grafana/jsonnet-libs/helm-util
15+
```
16+
17+
```
18+
local helm = import 'github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet';
19+
20+
{
21+
version:: 'v0.16.0',
22+
23+
values:: {
24+
installCRDs: true,
25+
global: {
26+
podSecurityPolicy: {
27+
enabled: true,
28+
useAppArmor: false,
29+
},
30+
},
31+
},
32+
33+
chart: helm.template(
34+
name='cert-manager',
35+
chart='jetstack/cert-manager',
36+
conf={
37+
values: $.values,
38+
flags: [
39+
'--version=%s % $.version',
40+
],
41+
},
42+
),
43+
}
44+
```

helm-util/helm.libsonnet

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)