Hi, Let us understand about
Flow control, the important building block of the template is this flow control and it will be achieved
using if else statement. This is very similar to the
If else statements we have in any other languages where
The condition will be evaluated using the pipeline and it will be used along with the if or elseif
conditions. If the condition gets to true, it will be getting into that particular block
And the statements within the block will get executed, if the condition fails
It's going to move on to the next condition that is else if
Again, there is going to be another pipeline or condition evaluation that will be evaluated.
If that evaluated to true, then the blocks within that will get evaluated and we will be having a else condition
where if all the above conditions failed, then the statements within the else conditions will be executed.
That's like a default keys and elseif,
else conditions are optional and only if statement is sufficient enough, most of the places we
will be using that to verify the conditions and execute a few set of statements.
Let's have a quick check on what all situations
The condition would be evaluated as false.
If the pipeline is evaluated, as the following values, it will be considered as false, like boolean
false or a numerical value of zero, empty string or nil value.
That is like null and any empty collection of map, slice, tuple, dictionary or an array
Now let us see a small example. Here I do have the template.
Let me get into the sample template that we had created. Here
I am going to update the values, within the values file
Already we do have a key called infra, within that
I do have a collection of key and value.
I do have the keys like Zone and region.
I'm going to evaluate this specific key region, whether it is equal to a specific value or not.
If it is evaluated as true, I will be taking some decision.
Let me go ahead and change the templates, within the template
Let me go ahead and edit it.
I'm going to remove the other values that we added for the test purpose.
Here I'm going to add a condition if, and I can have the logical conditions like equal, not equal, all possible conditions available.
I'm going to evaluate for values.infra.region and that should be equal to us-e
If this condition succeeded, then I'm going to add another key value pair true
Then I need to end the condition.
I don't want any if else condition.
So I'm going to end it with double open bracket and close bracket.
Observe very carefully the indentation should start at this specific location.
or else its going to give error messages.
We will try out those scenarios
as well. I returned the changes let me go ahead and do a dry run using helm install
dry run and in the debug mode
I'm going to give a name to this particular release and execute the mychart resources.
So the value it evaluated to be true.
So what happened it printed it as true.
Now, let me go ahead and change the value.
I'm changing it to us-w
Now, let me go ahead and do a dry run
It should not print the value ha:true, because the condition did not get evaluated, the same
way I can have if else conditions as well, the very important thing to be noted in this specific
condition is how to align this particular statement.
Let's try out a few scenarios.
And before that, I need to change the values so that the condition will get evaluated to be true.
So I am changing it back to us-e
Now, let me go ahead and execute the dry run.
If you observe the result very carefully
We do have an empty line over here and within YAML file empty lines
It's not going to create a problem.
But that is not advised to have, only the important thing that we need to take over here is the
indentation. The indentation is done properly with two spaces.
So it should work, but it is not recommended.
Now, let's go ahead and test it out.
What if we provide some indentation or some additional space in the template
Let's check the result.
Let me get into the templates.
Let's try this out, how it's going to work.
Let me go ahead and do the dry run.
Yes. The indentation worked fine. The reason because let me get into the template.
The condition will get evaluated and from this particular point, it's going to provide two indentation
With this particular understanding
Let me go ahead and do this.
Here, the key value pair will be indented with two more additional spaces.
And that is not a valid YAML file because it should be under another key.
Now, let me go ahead and do a dry run on this particular case as well, and see how the error will be.
So here I got an error because values are not allowed in this context.
The reason why I am demonstrating various options most of the time the mistakes happen in this specific
area of indentation while using the conditional statements.
Now, let me go ahead and correct this.
So this should work.
But at the same time, let's understand how to remove the blank line that we had got, so that can be
removed using a minus symbol over here and a minus symbol over here.
That means the new line in that specific location should be removed.
So there will be a new line at the starting of this particular condition evaluation and that will be removed.
And in the end, also, there will be a new line condition and that will be removed.
Now, let me go ahead and do a dry run.
Now, there is no new line character.
And everything is working fine. Now let's see
What if I deleted the new line in the end as well
That should also be not done.
Anyway, let's go ahead and try it out.
So here the end, I'm going to remove the existing new line so that can be provided by adding a dash over here.
So what will happen
It'll remove the starting new line as well as the end new line and this ha it will try to add over here along
with this specific project code
where that will fail because that's not a valid yaml file.
Let's go ahead and do the dry run.
yes the parsing failed because that's not a valid yaml file
So what happened was.
After evaluation, this ha:true will get added next to project code and the value.
So it's going to have another colon and that will fail.
So this is not a right option as well.
So the right thing to do is remove the new line in the beginning of each condition as well as in the end.
And have the indentation and the location from where the condition is getting executed the same place
The text within that will also get aligned in case if we have this particular condition evaluation in
the beginning, then properly the indentation should be provided.
So this is the right way to do.
Finally, let me go ahead and do the dry run
And the reason why I am not installing and uninstalling, the dry run is sufficient enough to evaluate the condition
and how that particular manifest file would look like.
So we have seen what are the different options to use the if else conditions
And the important thing to take care of removing the new line as well as what to do and what not to
do while adding the conditional statements.