Helm + Gomplate

Dmitrii Zhiliaev
2 min readNov 10, 2021

Helm uses go templates to render kubernetes manifests. Helm also provide Sprig library for extends functionality.

In this blog post I’m going to show how you can use custom render. Gomplate is just example. You can use another tool if you would.

gomplate is a template renderer which supports a growing list of datasources, such as: JSON (including EJSON - encrypted JSON), YAML, AWS EC2 metadata, BoltDB, Hashicorp Consul and Hashicorp Vault secrets.

Install tools

I use helm v3.7.1 and gomplate v3.10.0 on macOS.

$ brew install helm
$ brew install gomplate

gomplate and helm use the same delimiters {{ and }} for templating .

Of course you can use doble quotas. But I think this is a bad idea. Just reset delimeters for gomplate.

export GOMPLATE_LEFT_DELIM="[["
export GOMPLATE_RIGHT_DELIM="]]"

Testing

echo 'Hello, [[ .Env.USER ]]' | gomplate
Hello, zhilyaev

Looks great!

Create helm chart

# [[ defineDatasource "foo" "https://httpbin.org/get" ]]  
# [[ if not (datasourceReachable "foo")]] unreachable [[end]]
# [[ $d := ds "foo" ]]
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "this.fullname" . }}
labels: {{ include "this.labels" . | nindent 4 }}
data:
random: "[[ random.AlphaNum 32 ]]"
host: "[[ $d.headers.Host ]]"

Helm has --post-renderer flag. It allows you pass manifest to another tool.

Run

helm template this chart --post-renderer gomplate

We will get it.

# Source: this/templates/configmap.yaml
#
#
#
apiVersion: v1
kind: ConfigMap
metadata:
name: this
labels:
helm.sh/chart: this-0.1.0
app.kubernetes.io/name: this
app.kubernetes.io/instance: this
app.kubernetes.io/version: "1.16.0"
app.kubernetes.io/managed-by: Helm
data:
random: "2tJyK9xSsg1gvEmunEokQf4lyGkvojiQ"
host: "httpbin.org"

Deploy

helm upgrade --install this chart --post-renderer gomplate

Profit

P.S.

--post-renderer adds extra complexity to your work. Follow KISS you can try helmwave. In it, we solve everyday problems when working with helm, without interfering with the functionality of charts.

--

--