Skip to content

Commit

Permalink
* refactor: helm-util into tanka-util (grafana#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic authored Dec 11, 2020
1 parent 1894e32 commit 216bc80
Show file tree
Hide file tree
Showing 16 changed files with 434 additions and 255 deletions.
2 changes: 1 addition & 1 deletion cert-manager/jsonnetfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs.git",
"subdir": "helm-util"
"subdir": "tanka-util"
}
},
"version": "master"
Expand Down
3 changes: 2 additions & 1 deletion cert-manager/main.libsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local helm = (import 'github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet').new(std.thisFile);
local tanka = (import 'github.com/grafana/jsonnet-libs/tanka-util/main.libsonnet');
local helm = tanka.helm.new(std.thisFile);

{
values:: {
Expand Down
3 changes: 0 additions & 3 deletions helm-util/Makefile

This file was deleted.

106 changes: 1 addition & 105 deletions helm-util/README.md
Original file line number Diff line number Diff line change
@@ -1,105 +1 @@
---
permalink: /
---

# package helm-util

```jsonnet
local helm-util = import "github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet"
```

Package `helm-util` allows the user to consume Helm Charts as plain Jsonnet
resources. This package implements [Helm support](https://tanka.dev/helm) for
Grafana Tanka.

### Usage

> **Warning:** [Functionality required](#internals) by this library is still experimental and may break.
The [`helm.template`](#fn-helmtemplate) function to converts a Helm Chart into Jsonnet objects,
to be consumed by tools like [Grafana Tanka](https://tanka.dev).

Helm Charts are required to be available on the local file system and are
resolved relative to the file that calls `helm.template`:

```jsonnet
local helm = (import 'github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet').new(std.thisFile);
{
// render the Grafana Chart, set namespace to "test"
grafana: helm.template('grafana', './charts/grafana', {
values: {
persistence: { enabled: true },
plugins: ['grafana-clock-panel'],
},
namespace: 'test',
}),
}
```

For more information on that see https://tanka.dev/helm

### Internals

The functionality of `helm-util` is based on the `helm template` command.
Because Jsonnet does not support executing arbitrary command for [good
reasons](https://jsonnet.org/ref/language.html#independence-from-the-environment-hermeticity),
a different way was required.

To work around this, [Tanka](https://tanka.dev) instead binds special
functionality into Jsonnet that provides `helm template`.

This however means this library and all libraries using this library are not
compatible with `google/go-jsonnet` or `google/jsonnet`.


## Index

* [`fn new(calledFrom)`](#fn-new)
* [`fn patchKubernetesObjects(object, patch)`](#fn-patchkubernetesobjects)
* [`fn patchLabels(object, labels)`](#fn-patchlabels)
* [`fn template(name, chart, conf)`](#fn-template)

## Fields

### fn new

```ts
new(calledFrom)
```

`new` initiates the `helm-util` library. It must be called before any `helm.template` call:
> ```jsonnet
> // std.thisFile required to correctly resolve local Helm Charts
> helm.new(std.thisFile)
> ```
### fn patchKubernetesObjects
```ts
patchKubernetesObjects(object, patch)
```
`patchKubernetesObjects` applies `patch` to all Kubernetes objects it finds in `object`.

### fn patchLabels

```ts
patchLabels(object, labels)
```

`patchLabels` finds all Kubernetes objects and adds labels to them.

### fn template

```ts
template(name, chart, conf)
```

`template` expands the Helm Chart to its underlying resources and returns them in an `Object`,
so they can be consumed and modified from within Jsonnet.

This functionality requires Helmraiser support in Jsonnet (e.g. using Grafana Tanka) and also
the `helm` binary installed on your `$PATH`.
**Deprecated**: Please use [`tanka-util`](../tanka-util) instead.
32 changes: 0 additions & 32 deletions helm-util/README.md.tmpl

This file was deleted.

12 changes: 0 additions & 12 deletions helm-util/_example.jsonnet

This file was deleted.

106 changes: 5 additions & 101 deletions helm-util/helm.libsonnet
Original file line number Diff line number Diff line change
@@ -1,101 +1,5 @@
local d = import 'github.com/sh0rez/docsonnet/doc-util/main.libsonnet';
{
local this = self,

'#':: d.pkg(
name='helm-util',
url='github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet',
help=(importstr 'README.md.tmpl') % (importstr '_example.jsonnet'),
),

'#_config':: 'ignore',
_config: {
calledFrom:: error 'new(std.thisFile) was not called',
},


'#new': d.fn(
|||
`new` initiates the `helm-util` library. It must be called before any `helm.template` call:
> ```jsonnet
> // std.thisFile required to correctly resolve local Helm Charts
> helm.new(std.thisFile)
> ```
|||,
[d.arg('calledFrom', d.T.string)]
),
new(calledFrom):: self {
_config+: { calledFrom: calledFrom },
},

// This common label is usually set to 'Helm', this is not true anymore.
// You can override this with any value you choose.
// https://helm.sh/docs/chart_best_practices/labels/#standard-labels
defaultLabels:: { 'app.kubernetes.io/managed-by': 'Helmraiser' },

'#template':: d.fn(
|||
`template` expands the Helm Chart to its underlying resources and returns them in an `Object`,
so they can be consumed and modified from within Jsonnet.
This functionality requires Helmraiser support in Jsonnet (e.g. using Grafana Tanka) and also
the `helm` binary installed on your `$PATH`.
|||,
[
d.arg('name', d.T.string),
d.arg('chart', d.T.string),
d.arg('conf', d.T.object),
]
),
template(name, chart, conf={})::
local cfg = conf { calledFrom: this._config.calledFrom };
local chartData = std.native('helmTemplate')(name, chart, cfg);

this.patchLabels(chartData, this.defaultLabels),

'#patchKubernetesObjects':: d.fn(
'`patchKubernetesObjects` applies `patch` to all Kubernetes objects it finds in `object`.',
[
d.arg('object', d.T.object),
d.arg('patch', d.T.object),
]
),
patchKubernetesObjects(object, patch, kind=null, name=null)::
if std.isObject(object)
then
// a Kubernetes object is characterized by having an apiVersion and Kind
if std.objectHas(object, 'apiVersion') && std.objectHas(object, 'kind')
&& (kind == null || object.kind == kind) && (name == null || object.metadata.name == name)
then object + patch
else
std.mapWithKey(
function(key, obj)
this.patchKubernetesObjects(obj, patch, kind, name),
object
)
else if std.isArray(object)
then
std.map(
function(obj)
this.patchKubernetesObjects(obj, patch, kind, name),
object
)
else object,

'#patchLabels':: d.fn(
'`patchLabels` finds all Kubernetes objects and adds labels to them.',
[
d.arg('object', d.T.object),
d.arg('labels', d.T.object),
]
),
patchLabels(object, labels={})::
this.patchKubernetesObjects(
object,
{
metadata+: {
labels+: labels,
},
}
),
}
std.trace(
'Deprecated, please use `github.com/grafana/jsonnet-libs/tanka-util/` instead. (2020-12-11)',
(import 'github.com/grafana/jsonnet-libs/tanka-util/helm.libsonnet')
+ (import 'github.com/grafana/jsonnet-libs/tanka-util/k8s.libsonnet')
)
15 changes: 15 additions & 0 deletions helm-util/jsonnetfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 1,
"dependencies": [
{
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs.git",
"subdir": "tanka-util"
}
},
"version": "master"
}
],
"legacyImports": true
}
3 changes: 3 additions & 0 deletions tanka-util/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: docs
docs:
docsonnet -o . main.libsonnet
Loading

0 comments on commit 216bc80

Please sign in to comment.