Mastering Helm Charts. Library Edition
Introduction
Helm is a powerful tool for managing Kubernetes applications. It simplifies the deployment process by using charts, which are packages of pre-configured Kubernetes resources. However, managing multiple charts can become repetitive and error-prone. This is where Library Charts come in handy.
Library Charts on Helm Website
The library chart was introduced at the beginning of Helm 3 (please read the article).
Reasons to Use a Library Chart
Reusability: Share common functionalities and configurations across multiple charts, DRY.
Consistency: All created charts follow the same rules: same labels, same selectors, name conventions.
Maintainability: Majority of the fixes and improvements are centralized. In case you have an independent release flow for your library, all your derivatives will use the defined version, making upgrades straightforward and the impact minimal.
Some of my colleagues still avoid Library Charts and try to create some universal Helm chart for all potential cases/microservices/projects. But over time, all of their “universal” charts turned into chthonic creatures, and support and maintenance became a fight with them.
Anyway, if you have a set of very similar applications, you can create some “half-universal” Helm charts for the set, like:
- backend: ConfigMap (with injection into environment variables), Deployment, Service, HorizontalPodAutoscaler
- single page application: ConfigMap (mount application configuration), Deployment, Service, HorizontalPodAutoscaler
TL;DR
Helm Chart Library you can use from here: helm-generic
1 | apiVersion: v2 |
Helm Chart with the Library under the hood: mlflow-tracking-server
Defining a Library Chart
To define the chart as a library, we need to do the following. In Chart.yaml
, you need to set type: library
like this:
1 | apiVersion: v2 |
To follow Helm concepts, all your helpers in the library chart should begin with an underscore (_) to avoid rendering and output as a Kubernetes manifest file.
So, your templates
folder will look like this:
1 | _affinity.yaml |
Function common.util.merge
is one of the key and remarkable functions from the library which allows us to create highly customizable Helm charts with our library. Let’s take a look at the _util.yaml
file, where this helper function is located:
1 | {{- /* |
To demonstrate how this function works, let’s check how to use it to define a ConfigMap, for example.
1 | {{- define "common.configmap.tpl" -}} |
An empty ConfigMap will be rendered after using this helper in your Helm chart.
1 | apiVersion: v1 |
To extend this ConfigMap we can do the following:
- set some default required parameters
- add additional code to give us flexibility and customization via
values
files.
For example
1 | {{- template "common.configmap" (list . "application.env_configmap") -}} |
Conclusion
Library Charts in Helm provide a powerful way to manage and reuse common configurations across multiple charts. By using Library Charts, you can ensure consistency, improve maintainability, and save time by avoiding repetitive tasks. Explore the helm-generic library to get started with creating your own Library Charts.