DEV Community

loading...

Helm Charts, with or without Kustomize

langyizhao
Coding in Java, Scala, JavaScript, Python, Go, and Haskell. Trying to focus on architecture and be more language agnostic.
・2 min read

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-1m9j-temp-slug-9736484

  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"]

Discussion (0)