Helm for beginners -> Helm charts Anatomy-> Lab: Conditionals, With block and Rangers

Hi folks, Quastion 7 of this lab asks for :

Make appropriate changes in the serviceaccount.yaml file to:

  1. Check whether serviceAccount should be created or not (use the with conditional).
  2. Use the serviceAccount name from the values.yaml file.

Well, I didn’t get why use “with” to check a condition, “with” according to helm docs should be used to control variable scoping. I have to read the answer tab to find what you did, as no answer was good, and I confess I didn’t like your solution too much. Is it possible to review it ?

Also, question 10 is not clear enough. Beyond use of $val and $key values which were not covered, the exercise asks for a label : “app.kubernetes.io/managed-by=Helm” which I could not find anywhere and had to remove because it was not at the answer. Also the $.Values.serviceAccount.name, is strange, if I’m already into .Values.serviceAccount scope, why should I refer to the root as it was out of the scope? Is it possible to check these? tks…

Hi @Fabio-Muller

Question 7

Although in practice you probably would not, you can use with to test a boolean condition like serviceAccount.create.

with affects the scope, so that to now get the service account name, we must explicitly request the root scope, i.e. {{ $.Values.serviceAccount.name }}. This is one of the “appropriate changes” expected.

The solution text should include this - and you saw that in Q10 it does include it, so I will raise a ticket to have it corrected.

Tip to test the solution when editing charts

cd ~/webapp-color
helm template test . --set serviceAccount.create=true

Here I explicitly set the value of serviceAccount.create and helm will output the YAML it would send to a cluster to your screen. Lab validation scripts do something similar in the background.

Question 10

Here we are using a range, which is partly what this lab is about. When you range a map, e.g. the map called labels in the values file:

  labels: 
    tier: frontend
    type: web
    mode: proxy

then range returns the key/value pairs one at a time. Keys are tier, type, mode and values are frontend, web, proxy. The names $val and $key are completely arbitrary. You could have used $foo and $bar if you wanted to. They are only local variables, and all that matters is that after keyword range, the first variable gets the keys and the second one gets the values.

The label app.kubernetes.io/managed-by=Helm, know that this is automatically added to all resources by helm when deployed on the cluster. You do not need to add it yourself.

So, after making the edit to add the range into the service account, deploy the chart

controlplane ~/webapp-color is 📦 v0.1.0 via ⎈ v3.13.0 ➜  helm install webapp-color  .

Now look at the SA, or any other resource’s labels

k get sa webapp-sa --show-labels

and you will see that helm label

Wonderful answer Alistair, much clearer now to me. Thanks for reply.