Is it possible to use the same variable that changes across different helm templates/functions? - kubernetes-helm

Let's say I have 2 functions :
{{- define fn1 -}}
$var = add $var 5
{{- end}}
{{- define fn2 -}}
$var = add $var 5
{{- end}}
So let's say "var" was declared with 0, after invoking "fn1" and "fn2", the value in it needs to be (0+5+5 = 10). The requirement is to get 10 by printing the same "var", outside "fn1" and "fn2" after calling "fn1" and "fn2"
I'm aware of using the "include" directive, but I've only come across instances of using that for computing the same result.

Related

helm using argument in template to check if value exists in Values.yaml

The problem is following: I want to check if field in Values.yaml exists based on argument given to the template in _helpers.tpl:
{{- define "example-name" -}}
{{- $objectRef := index . 0 -}}
{{- if .Values.custom -}}
{{- if .Values.custom.$objectRef -}}
{{- if .Values.custom.$objectRef.annotations -}}
{{- include "some-library" (tuple .Values.custom.$objectRef.annotations) | indent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
Then in my deployment for example:
{{- template "example-name" "someField" }}
I want the result to be following:
{{- define "example-name" -}}
{{- $objectRef := index . 0 -}}
{{- if .Values.custom -}}
{{- if .Values.custom.someField-}}
{{- if .Values.custom.someField.annotations -}}
{{- include "some-library" (tuple .Values.custom.someField.annotations) | indent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
But the only thing I get is following error: bad character U+0024 '$'
I want to use template multiple times with various arguments. I haven't seen anyone dealing with the same problem before.
Any ideas?
The standard template function you're looking for is index. In its simplest form, index $map $key does a dynamic lookup of the $key (can be any expression) in the provided $map (can be any expression). It can also do integer-index lookups in arrays (Go slices) and nested lookups if you need to.
The other problem that you'll run into is that $map.undefinedKey (or index $map "undefinedKey"), assuming undefinedKey is not present in $map, is valid but evaluates to Go nil. So you can't do further lookups in that. The workaround to this I typically use is to use the Helm (Sprig) default function to use an empty dict if a value is not present.
That would give you a template like:
{{- define "example-name" -}}
{{- $top := index . 0 -}}
{{- $objectRef := index . 1 -}}
{{- $custom := $top.Values.custom | default dict -}}
{{- $object := index $custom $objectRef | default dict -}}
{{- with $object.annotations -}}
{{- include "some-library" (list $top .) | indent 4 }}
{{- end }}
{{- end }}
This is called with a list of two values, the top-level Helm object and a reference to a key in .Values.custom
{{- include "example-name" (list . "someField") -}}
The template extracts the two values from the list parameter. It then traverses the values structure one level at a time, at each level defaulting to an empty dictionary. So for example if there is no .Values.custom then $custom is set to an empty dictionary, which allows index $custom $objectRef to execute successfully (and return nil, but not abort). At the bottom level we use the with template function to check to see if come value is truthy, and if so, temporarily bind . to its value. When we make the inner call, we already have the top-level Helm object in a variable, and we can assemble a list of $top and the non-empty annotation structure . as the single template parameter.

Passing Dictionary as an argument in Helm Template

I wonder if there is anyway for us to pass dictionary as an argument in Helm Templates. In Python, I could simply create some scripts like this but I am not sure how to translate this script into Helm.
>>> def display(d):
... for key in d:
... print("key:", key, "Value:", d[key])
...
>>> def func():
... D = {'a':1, 'b':2, 'c':3}
... display(D)
...
>>> func()
key: a Value: 1
key: b Value: 2
key: c Value: 3
I tried to implement some script like this but they seem does not solve out.
_helpers.tpl
{{- define "display"}}
{{- $d := .d -}}
{{- range $key, $value := $d -}}
{{- printf "%s-%s" $key $value-}}
{{- end -}}
_test.tpl
{{- define "func"}}
{{- $D = dict 'a' 1 'b' 2 'c' 3} -}}
{{- $res := include "display" (dict "d" $D) -}}
{{- $res -}}
{{- end -}}
Would you give me some working examples of how a dictionary is passed as an arguments in helm?
Thank you in advance for your help! I am much appreciate it!
A Go text/template template takes only one parameter. It has the special name . inside the template. That parameter can be any type, though, and in a Helm context there are extension functions like list and dict that can assemble the containers.
A template can only return a string, and if you need to capture that return value then you need to use the Helm include extension function rather than the standard template template directive. Anything the template outputs is part of that "return value" and there is not a specific "return" command.
So, for example, your display function is pretty easy to write: its one parameter is a dictionary and you use a range loop to iterate over its contents. (I'm dumping it out as a YAML list, which would be a more typical Kubernetes/Helm output.)
{{- define "display" -}}
{{- range $k, $v := . -}}
- key: {{ $k }}
value: {{ $v }}
{{ end -}}
{{ end -}}
And then to call it, you'd construct a dictionary with dict (or extract one from .Values) and pass that dictionary to the function.
{{- $d := dict "a" 1 "b" 2 "c" 3 }}
{{- include "display" $d -}}

helm check for required variables in a single file

I'd like to have a file that checks for all "required" variables. So far I came up with this, but it doesn't look like this is "best" practices
values.yaml
# this is required name of the hosted zone
domainZone: ""
someOtherRequireVar: ""
/templates/required.yaml
# the block below makes sure that our required variables are
# declared in the values file and if not that will throw the error
# so even the block below is commeted, it's still evaluated by the HELM and error
# will be generated if the variable's value is not set
# {{ .Values.domainZone | required "Domain Zone is required and should be non-empty string" }}
# {{ .Values.someOtherRequireVar | required "The someOtherRequireVar is also required and should be non-empty string" }}
Are there better ways to achieve this?
I'd recommend using named templates for validation, which you can execute in your installation notes instead of regular template files. Furthermore, by using failsafe validation functions (unlike require) for individual values, you can validate multiple values before failing execution of your helm command.
Bitnami charts use this strategy.
Setup
In a partials file, e.g. templates/_helpers.tpl, define named templates for validating the supplied values:
{{/* Compile all validation warnings into a single message and call fail. */}}
{{- define "mychart.validateValues" -}}
{{- $messages := list -}}
{{- $messages = append $messages (include "mychart.validateValues.foo" .) -}}
{{- $messages = append $messages (include "mychart.validateValues.bar" .) -}}
{{- $messages = without $messages "" -}}
{{- $message := join "\n" $messages -}}
{{- if $message -}}
{{- printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
{{- end -}}
{{- end -}}
{{/* Validate value of foo */}}
{{- define "mychart.validateValues.foo" -}}
{{- if not (and .Values.foo (kindIs "string" .Values.foo)) -}}
mychart: foo
`foo` is required and should be a non-empty string
{{- end -}}
{{- end -}}
{{/* Validate the value of bar */}}
{{- define "mychart.validateValues.bar" -}}
{{- if not (and .Values.bar (kindIs "string" .Values.bar)) -}}
mychart: bar
`bar` is required and should be a non-empty string
{{- end -}}
{{- end -}}
In this case, the named template mychart.validateValues will run multiple validations (i.e. mychart.validateValues.foo, mychart.validateValues.bar). If one or more validations produce a validation warning, it will call fail with a summary of the warnings.
In templates/NOTES.txt, evaluate the named template for validation:
{{- include "mychart.validateValues" . }}
Test
With the following values.yaml file:
bar: 24
Templating, installing, or upgrading the chart will fail with the following message:
Error: execution error at (test/templates/NOTES.txt:1:4):
VALUES VALIDATION:
mychart: foo
`foo` is required and should be a non-empty string
mychart: bar
`bar` is required and should be a non-empty string

How to concatenate variables inside a ternary statement?

I'm trying to do this:
name: {{ $value.enable | ternary $value.prefix $.Release.Namespace $value.suffix $value.override }}
But that syntax is wrong. I can't find any examples for how I would concatenate these vars together: $value.prefix $.Release.Namespace $value.suffix
Edit
I thought I could use print like this:
name: {{ true | ternary (print $value.prefix $.Release.Namespace $value.suffix) $value.fullnameOverride }}
But if you don't specify one of the fields it prints <nil> instead of not printing anything which is what I want.
Helm includes the Sprig template library which includes many useful composable parts.
For the "true" half, you have the three components; you'd like them joined together; but you'd like nil parts to be removed. The Sprig list function constructs a list from arbitrary items; compact takes a list and returns a new list without empty items (either empty string or nil, anything that's a Go zero value); and then join combines the list together into a single string. You can assign that to a temporary variable, and use it in the ternary call:
{{- $qualifiedName := list $value.prefix $.Release.Namespace $value.suffix | compact | join "" }}
name: {{ $value.enable | ternary $qualifiedName $value.fullnameOverride }}
I find the ternary syntax a little hard to read (even if it matches the C-style expr ? t : f syntax) and in this context it's not necessary. A helper template that spelled this out could be easier to understand later.
{{-/* Generate the name of the thing. Call with a list containing
the top-level Helm object and an item from the values file.
(Indented for readability, the template engine removes all of
the whitespace.) */-}}
{{- define "name" -}}
{{- $top := index . 0 -}}
{{- $value := index . 1 -}}
{{- if $value.enable -}}
{{- with $value.prefix -}}{{- . -}}{{- end -}}
{{- with $top.Release.Namespace -}}{{- . -}}{{- end -}}
{{- with $value.suffix -}}{{- . -}}{{- end -}}
{{- else -}}
{{- $value.fullnameOverride -}}
{{- end -}}
{{- end -}}
name: {{ include "name" (list $ .) }}

Is it possible to define variables use if/else condition in helm chart?

In my values.yaml I have:
isLocal: false
localEnv:
url: abcd
prodEnv:
url: defg
Then I have a service.yaml:
{{- if .Values.isLocal }}
{{- $env := .Values.localEnv }}
{{- else}}
{{- $env := .Values.prodEnv }}
{{- end}}
url: {{ $env.url}}
Unfortunately I got a 'undefined variable "$env"' error, does anyone have idea how to achieve this? Thanks!
One more way would be to create the variable before starting the if/else block. For example:
{{- $env := .Values.prodEnv -}}
{{ if .Values.isLocal }}
{{- $env = .Values.localEnv -}}
{{ end}}
Notice that I've not used the := operator inside the if block as the variable is already created.
and then
url: {{ $env.url}}
The Go text/template documentation notes:
A variable's scope extends to the "end" action of the control structure ("if", "with", or "range") in which it is declared....
so your declarations of $env go out of scope at the end of the {{ if }}...{{ end }} block, which isn't so useful.
Helm also includes (almost all of) a support template library called Sprig which includes a ternary function, which acts sort of like an inline "if" statement. For your example you could write
{{- $env := ternary .Values.localEnv .Values.prodEnv .Values.isLocal -}}
(Also consider just writing totally separate Helm values files for each environment, and installing your chart with helm install -f prod.yaml, instead of trying to encapsulate every possible environment in a single file.)
You actually can define the variable $env outside the if block and assign its value using = with if block.
{{- $env := "" }}
{{- if .Values.isLocal }}
{{- $env = .Values.localEnv }}
{{- else}}
{{- $env = .Values.prodEnv }}
{{- end}}
url: {{ $env.url}}
Please note: := will declare the variable while assigning the value. On the other hand, = will assign the value only.
If you use := in if, it will declare a new local variable $env which will not impact the value of $env declared outside the if block.