New in Grafana Tanka: Customize Helm charts without modifying them
Helm charts are great. They combine high quality, ready-made runtime configurations for a huge number of applications with an incredible getting-started experience.
There is literally no faster way to install a production-ready Grafana or Loki on Kubernetes than using helm install
.
Unfortunately, Helm charts can also be incredibly inflexible:
Customization requires modification
In Helm, customizing the outcome of your chart is handled using Values.
However, a chart author naturally cannot predict all of the properties any user might want to set. This leads to frustration and extra work for both users who need to spend time on pull requests, and maintainers who need to review them.
And in case you run into edge cases that only benefit your organization, you might not see your PR merged at all. Maintaining forks and keeping those close to upstream is even more time-consuming.
It’s all about the language
In early 2020, we announced Grafana Tanka to breathe fresh air into Kubernetes configuration management. What set us apart was the adoption of a radically different language, Jsonnet.
At its core, Helm is powered by a templating engine that literally substitutes characters in a text file. This, however, comes at the cost of the engine not fully understanding the semantics of the data it modifies.
In contrast, Jsonnet is a superset of JSON, so it does exactly that. Having a clear perception of the actual data and its representation enables advanced features, most notably deep merging. So instead of creating countless injection points in an upstream definition, you can mix in your very own overrides.
The best of both worlds
Wouldn’t it be nice to use both? Combine the incredibly rich ecosystem of Helm with the powerful capabilities of Tanka?
As it turns out, you can do so now! Freshly released Tanka includes Helm support, which enables you to load Helm charts into Jsonnet and treat them as regular JSON objects.
1. Project setup
Please follow the steps at https://tanka.dev/install first and make sure to install
tk
,jb
andhelm
. You need at least Tanka v0.12 for Helm support to be available.
To use Tanka, you need to bootstrap a project. This is covered in more detail as part of the tutorial.
$ tk init # create a new project
$ jb install github.com/grafana/jsonnet-libs/helm-util # install our Helm library
$ cd ./environments/default # change into the default environment
2. Helm chart
We are going to use the MySQL chart. Tanka requires charts to be vendored:
$ tk tool charts init
$ tk tool charts add stable/mysql@1.6.7
3. Usage from Jsonnet
Next, edit main.jsonnet
inside environments/default
and add the following contents:
local helm = (import "github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet").new(std.thisFile);
{
mysql: helm.template("mysql", "./charts/mysql", {
namespace: "databases",
values: {
persistence: { size: "4Gi" }
}
})
}
Running tk show .
will now yield all the YAML resources that make up the mysql
chart.
As you see, you can pass values
right from Jsonnet.
4. Overwriting
The reason why we are here is to change fields that are not covered by values. This is straightforward.
Say we wanted to set the prometheus.io/scrape: true
annotation on the Deployment
called mysql
:
local helm = (import "github.com/grafana/jsonnet-libs/helm-util/helm.libsonnet").new(std.thisFile);
{
mysql: helm.template("mysql", "./charts/mysql", {
namespace: "databases",
values: {
persistence: { size: "4Gi" }
}
}) + {
deployment_mysql+: {
spec+: { template+: { metadata+: {
annotations+: {
"prometheus.io/scrape": "true"
}
}}}
}
}
}
It’s that easy and works for every single field!
Wrapping up
Grafana Tanka is open source on GitHub. Get started using the tutorial today and level up your Kubernetes configs!