DEV Community

langyizhao
langyizhao

Posted on

Helm Charts, with or without Kustomize

I've recently read an interesting blog about using Helm together with Kustomize.

Though it provides good information such as the difference between Templates (via Helm) vs Overlays (via Kustomize), I would argue that, in a real deployment, the same effect can be achieved with Helm alone without the extra complexity of Kustomize.

To avoid repeating the details in the original blog, I just provide the files to simulate the approach used by the original blog here: https://github.com/langyizhao/helm-charts-with-or-without-kustomize/tree/master/with-kustomize

The distilled bare minimum steps are:

  1. kustomize build
  2. helm template
  3. kubectl apply -k

3 steps are actually not too bad. The major clutter is the introduction of the temporary folder and temporary template files and the easily-confusing order of applying overlays.

So how do we achieve the effect of overlays by just using templates? We will need some logic when drafting the template. Fortunately the Go template language under the hood of Helm charts easily support such trivial logic and some non-trivial ones.

One possible implementation is:

# from without-kustomize/templates/pod.yaml
{{- if and .Values.employeeName .Values.employeeDepartment }}
    args: ["/bin/echo My name is {{ .Values.employeeName }}. I work for {{ .Values.employeeDepartment }} department. Our company name is {{ .Values.companyName}}"]
{{ else }}
    args: ["/bin/echo Hello! My company name is {{ .Values.companyName }}"]
{{ end }}
Enter fullscreen mode Exit fullscreen mode

In this case we will only render the complex form when the necessary values exists in the values.yaml file provided by our customer and safely default to the simple form when they are missing.

No pre or post rendering needed and Helm alone handles everything.

Feel free to check the simple examples out and try it yourself:

This is the example code for https://dev.to/langyizhao/helm-charts-with-or-without-kustomize-2f1j

  1. Example 1: helm template without-kustomize -f values1.yaml

Expected last line: args: ["/bin/echo Hello! My company name is ABC Company"]

  1. Example 2: helm template without-kustomize -f values2.yaml

Expected last line: args: ["/bin/echo My name is Gary. I work for Marketing department. Our company name is ABC Company"]

Top comments (2)

Collapse
 
hacktron95 profile image
Osama Nabil

Hey, thanks sharing
I would like to argue that the approach you mentioned actually don't replace the original post's approach.

to do what you suggested for a large chart, you actually "HAVE TO" fork the chart repository and do your go-template modifications there, u will miss any updates on the original chart..etc (mentioned in the original post).

Collapse
 
langyizhao profile image
langyizhao

Good point, thanks for pointing it out!