Converting Gigabytes to bytes in helm templates

4/30/2019

I have a chart for kafka, that has a pvc size defined in its values file like so: 20Gi. I also have a configmap, that has a definition that it takes in bytes, like so: log.retention.bytes=21474836480.

I'm trying to get my chart to use the same value defined in size in the values file, in the configmap (hopefully, do some arithmetics on it before, like taking away a constant value to reserve some extra space)

I've been looking for a while to see if there is such a function built in to helm templates, or a way to create my own functions, with not much luck.

Ideally, I'm looking for something like this:

log.retention.bytes={{ .Values.persistence.size | convert-to-bytes | substract 10000 }}
-- Tom Klino
go
go-templates
kubernetes-helm

1 Answer

4/30/2019

You can use the div Sprig function. For example, you could do:

{{ div .Values.persistence.size 1024 }}

If you'd like to perform a subtraction on the result, you can use the sub function. For example:

{{ sub (div .Values.persistence.size 1024) 10000 }}
-- yanivoliver
Source: StackOverflow