Hi, let us understand more about functions. As a part of values yaml file or the templates yaml file
We can put in functions to compute the values.
Let us see some example that's going to give us more clarity.
Let me get into the values file and add some values.
I'm going to edit the value file.
Here we were adding key value pair
Now I'm going to add a type of collection within the yaml file where I'm going to have key as infra
And it's going to have a couple of key value pairs, zone and region.
Let me go ahead and add it. Dont worry about the content that I am adding over here.
I will be adding it as a resources within the lecture.
So I'm adding a key project code and it's having some value, then I'm adding another key.
That's going to have collection of key value pairs.
So this is another example to demonstrate how to access the collection as well.
Let me get into the templates.
Let me edit the template file
Here I'm going to add and access the values.
Say, for example Zone, Region and Project code.
I'm adding zone as key, and I'm adding template derivative within that I am adding a function called quote.
So the function quote will be getting one argument and it's going to quote that specific value that's
being read from the value file.
So within the value file it's going to read zone, within infra.
And it's going to surround it with double quotes, the same way it's going to read the project code from
the values file and it is going to convert it into uppercase.
There are n number of predefined functions available as a part of the go template that can be accessed
from this particular location or helm supports the functions defined within sprig library as well.
So within sprig librrary there are n number of useful template functions for go templates.
Here we can use any of these functions.
We do have lots of simple to complex function within it.
Say for example, within string function we do have upper which is going to take an argument and convert
that into uppercase.
The same way we do have quote which is going to convert or enclose the string using a double quote.
So I'm going to call the function within the template directive and pass the value reqired for that particular
function as an argument and pass the required argument to those functions.
Let me save this particular template and I'm going to do a dry run. Let me get into the parent directory
where I do have the chart resources.
My chart resources are available as a part of mychart.
So I'll be calling the mychart and do a dry run and I will provide the release name as well
So it's going to read the values file, do all the computation and that will be replaced within the
config map yaml file. Here
We can see it read the zones and it enclosed within the double quotes.
The same way region is read.
And it is enclosed within the double quote.
And the project code, it is read as a small case and we passed it to convert it into uppercase
And that is getting passed to this particular YAML file.
And functions play a very significant role where
I can convert whatever the data that I wanted and dynamically it'll help me to compute the required values.
And along with functions, we will be using another useful feature called pipeline that we will be seeing
it in the next lecture. So in a quick summary
We have seen how to use functions as a part of the template directive and the introduction to another
useful library called Sprig.