A Helm chart trick for dependency highlighting
A simple utility to express and highlight dependencies in helm chart.
[This post needs some basic understanding of Helm charts.]
Recently, I was working on refactoring helm charts. As the different microservices evolved, the charts became harder to manage. Many of the charts were sharing some common ConfigMaps and Secrets. One of the main goals of the refactoring was to remove any outside dependencies out of application charts. Although I could remove most of them, we still had some resources which were strictly outside the responsibility of any application.
Consider the following scenario:
We have two microservices, both depend on the common Redis service. So redis-config
ConfigMap and redis-cred
Secret should maintain their existence independent of any application chart. One obvious advantage is rolling access credentials giving us a single source of truth. This is a pretty common scenario.
Typically we can achieve this using a "common-resources" chart that loads such common resources, and a particular resource from this chart - ConfigMap/Secret can be referenced in the application chart. This works well as long as we install the common charts first. If we fail to do that the the application helm installation would still work but eventually end up either in
pods in failing state because required resources not found
helm rollback if handled via Test hooks
Writing pre-install
hook test was a great option. But to start with I chose an easy way where I can specify my dependencies and helm can take care of validating the presence of those dependencies for me.
If you've worked with Ubuntu you might've met the unmet dependencies error. That's what I wanted!
I went on reading the helm documentation. (Which is nicely maintained. Kudos to the maintainers for that.) I came across the lookup
function. This was a great start. Next, I wanted a way to prevent my helm installation if any dependency was not found.
From here I had two options
A “hard requirements” way where I'll not install the application chart itself. i.e. to use `--atomic`
A “soft requirements” way like provide a warning message to the user that some dependency was not met.
Notes.txt
was the perfect option for this. Anything we add in Notes, gets printed at the end of helm install/upgrade.
I chose the second option to start with. It was easier to implement and did a good job of highlighting the developer of any expected issues. So with a combination of Library Chart to provide the implementation logic and invoking that from the application/service chart I could achieve my goal. Below is the exact implementation of the code which is also available in a GitHub repo.
First, we start with a library chart.
Helm code for parsing and looking up the resources:
Declaring the external dependencies in Service/Application’s chart
Invoking the utility to print the unmet dependencies before and after they are installed in the cluster
That’s it. If you have any feedback feel free to reach out to me.