Template file in yaml

8/29/2018

Values.yaml

cpulimit: 200m
memlimit: 512M

configmap.yaml

mem_pool_size = {{ ((.Values.memlimit)) mul 0.8 }} --> not working
mem_pool_size = {{  .Values.memlimit mul 0.8 }} --> not working
mem_pool_size = {{ .Values.memlimit * 0.8 }} --> not working
mem_pool_size = {{ .Values.memlimit }} * 0.8 --> not working
mem_pool_size = {{ .Values.memlimit }} mul 0.8 --> not working

Tried many ways but i dint got the exact solution.if user provides value of memlimit as 512M i should assign only 80 % ram so the value will be 410M. I am finding a way whether arithmetic operations are supported in helm templates. Is there any example for this.

--
kubernetes
kubernetes-helm

1 Answer

8/31/2018

In helm templates this is done via pipelines. Some of them are defined via Go template language and some others are part of Sprig template library.

I did not find a complete list which are valid and working in Helm, but I did not find a Sprig one not working as explained in the Sprig documentation.

So first the syntax for pipelines has to be:

{{  .Values.memlimit | mul 2 }}

However the Math Functions only work on int64. But 512M is not an int. So you can let the user specify the limits as bytes or chain more pipes to first remove the "M" and then do the calculation:

{{ .Values.memlimit | replace "M" "" |mul 2  }}M

Since Memory can be specified with different units you maybe need some regexp:

{{ .Values.memlimit |regexFind "[1-9]+" |mul 2 }}{{ .Values.memlimit | regexFind "[a-zA-Z]+"  }}

But like stated all Sprig Math Functions only work on int64 so mul 0.8 will multiply by zero, mul 1,6 with only multiply with 1 and so on.

So probably you have to wait for the Sprig functions to also work with floats to achieve a percentage based calculation or you find some clever trick with the provided Math functions by Sprig and the int64 type.

Maybe something like explained in this answer:

C How to calculate a percentage(perthousands) without floating point precision

-- mszalbach
Source: StackOverflow