Repository: oxheadalpha/tezos-k8s (language: python)

Issue:
Add a way to define env vars in values.yaml
I am resubmitting my prior work on environment variables, but I have updated it to be able to set env vars on a container by container basis. This could be used for `TEZOS_LOG`, but also for things like `HTTP_PROXY` or `LD_PRELOAD`.

I take care of a few other things in this commit. Namely, I fix the expected test results and I enable the tests for the CI workflow. We discussed this before and I believe that we agreed on the approach which is to have a small set of generated charts which have to be updated when we make changes to them. The goal is that as we have to commit an updated chart to the repo for each test case as part of the commit, we gain two features:
1. the committer will be shown what changes are being made to the charts, and
2. the reviewer will also see the diffs as part of the review.

Please keep in mind that the test program `./bin/test-charts` takes a `-Y` flag and if you have dyff installed, it will do a YAML aware comparison. This makes development much easier.

I also include a commit called ``[Refactor fundamental containers to avoid repetition](https://github.com/oxheadalpha/tezos-k8s/pull/393/commits/00b408af35fc835cf8075121c3f37a0d4eebf6f4)''. In this commit, I try to aggressively avoid repetition in the templating by making a generic way to define a container which uses parameters to generate each section and it refactors the complexity into a single call. We avoid the repetition for the standard reasons, the main one being to reduce the likelihood of bugs being introduced into the code when N of M complex sections are updated.

The code might want to be reviewed one commit at a time.


Discussion / hints:
I think that the failed helm lint is because the version of helm that is doing the linting is quite old. It passes this test on my local box.
To include special characters in the YAML, we must quote them. I've done this and added `TEZOS_LOG` to our tests to prevent a regression.
Running helm template after checking out the branch:
```shell
helm template charts/tezos --debug
install.go:172: [debug] Original chart version: ""
install.go:189: [debug] CHART PATH: /Users/aryehharris/Desktop/tezos-k8s-copy/charts/tezos


Error: parse error at (tezos-chain/templates/_containers.tpl:117): unclosed action
helm.go:81: [debug] parse error at (tezos-chain/templates/_containers.tpl:117): unclosed action
```
It is difficult to visually parse a lot of this PR as the yaml logic is pretty complex. Indenting nested if statements, loops, etc. instead of linear stacking nested logic would be a big help in being able to review this.
Examples:
<details>

```yaml
# From this
{{- if not (hasKey .call_args "name") }}
{{- $_ := set .call_args "name" .call_args.type }}
{{- end }}

# To this
{{- if not (hasKey .call_args "name") }}
  {{- $_ := set .call_args "name" .call_args.type }}
{{- end }}
```

```yaml
# From this
{{- define "tezos.init_container.config_init" }}
{{- if include "tezos.shouldConfigInit" . }}
{{- $_ := set $ "call_args" (dict "type"           "config-init"
                                  "image"          "octez"
                                  "with_config"    1
                                  "localvars"      1
) }}
{{ include "tezos.generic_container" $ }}
{{- end }}
{{- end }}

# To this
{{- define "tezos.init_container.config_init" }}
  {{- if include "tezos.shouldConfigInit" . }}
    {{- $_ := set $ "call_args" (dict "type"           "config-init"
                                      "image"          "octez"
                                      "with_config"    1
                                      "localvars"      1
    ) }}
    {{ include "tezos.generic_container" $ }}
  {{- end }}
{{- end }}
```

```yaml
# From this
{{- define "tezos.container.bakers" }}
{{- if has "baker" $.node_vals.runs }}
{{ $node_vals_images := $.node_vals.images | default dict }}
{{- range .Values.protocols }}
{{- $_ := set $ "command_in_tpl" .command }}
{{- $_ := set $ "call_args" (dict "name"      (print "baker-" (lower .command))
                                  "type"        "baker"
                                  "image"       "octez"
                                  "with_config" 1
                                  "localvars"   1
) }}
{{ include "tezos.generic_container" $ }}

{{- if or (regexFind "GRANAD" .command) (regexFind "Hangz" .command) }}
{{- /*
Also start endorser for protocols that need it.
*/}}
{{- $_ := set $ "call_args" (dict "name"  (print "endorser-" (lower .command))
                                  "type"                "endorser"
                                  "image"               "octez"
                                  "script_command"      "baker"
                                  "with_config"         1
                                  "localvars"           1
) }}
{{ include "tezos.generic_container" $ }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

# To this
{{- define "tezos.container.bakers" }}
  {{- if has "baker" $.node_vals.runs }}
    {{ $node_vals_images := $.node_vals.images | default dict }}
    {{- range .Values.protocols }}
      {{- $_ := set $ "command_in_tpl" .command }}
      {{- $_ := set $ "call_args" (dict "name"      (print "baker-" (lower .command))
                                        "type"        "baker"
                                        "image"       "octez"
                                        "with_config" 1
                                        "localvars"   1
                                   )
      }}
      {{ include "tezos.generic_container" $ }}

      {{- if or (regexFind "GRANAD" .command) (regexFind "Hangz" .command) }}
        {{- /* Also start endorser for protocols that need it.*/}}
        {{- $_ := set $ "call_args" (dict "name"  (print "endorser-" (lower .command))
                                          "type"                "endorser"
                                          "image"               "octez"
                                          "script_command"      "baker"
                                          "with_config"         1
                                          "localvars"           1
                                     ) 
        }}
        {{ include "tezos.generic_container" $ }}
      {{- end }}
    {{- end }}
  {{- end }}
{{- end }}
```
</details>
I tried to indent all of the code and then realised that we're just dealing with a templating language here. It's actually a bit annoying, because you get errors like:
```
Error: INSTALLATION FAILED: YAML parse error on tezos-chain/templates/nodes.yaml: error converting YAML to JSON: yaml: line 22: did not find expected '-' indicator
```
which at first perplex you. But then you realise that in this sort of thing:
```
list:
  - thing1
  - thing2
  {{- if something }}
    {{- if something else }}
  - thing3
    {{- end }}
  {{- end }}
```
Because you indented the lines, you end up with lines which contain only 2 or 4 spaces which is not valid YAML in the middle of a list.
The force push did this:
```
: arioch; git diff  817b53a 2b5c6a6
diff --git a/charts/tezos/templates/_containers.tpl b/charts/tezos/templates/_containers.tpl
index 77bf455..38fb441 100644
--- a/charts/tezos/templates/_containers.tpl
+++ b/charts/tezos/templates/_containers.tpl
@@ -225,7 +225,7 @@
 {{- end }}

 {{- define "tezos.container.sidecar" }}
-  {{- if not (eq $.node_vals.readiness_probe false) }}
+  {{- if or (not (hasKey $.node_vals "readiness_probe")) $.node_vals.readiness_probe }}
     {{- include "tezos.generic_container" (dict "root"  $
                                                 "type"  "sidecar"
                                                 "image" "utils"
```
Again:
```
: arioch; git diff 34fea9a 09f626b
: arioch;
```
All we did with that force push was to move 886e14d and squash it into 00cd5a6. No difference to the resulting text files.

Fix patch:
diff --git a/charts/tezos/templates/_containers.tpl b/charts/tezos/templates/_containers.tpl
index 38fb44130..c1f146c7c 100644
--- a/charts/tezos/templates/_containers.tpl
+++ b/charts/tezos/templates/_containers.tpl
@@ -174,6 +174,7 @@
     {{- include "tezos.generic_container" (dict "root"        $
                                                 "type"        "config-init"
                                                 "image"       "octez"
+                                                "with_config" 1
                                                 "localvars"   1
     ) | nindent 0 }}
   {{- end }}


Test patch:
diff --git a/test/charts/mainnet.expect.yaml b/test/charts/mainnet.expect.yaml
index a0ef23e94..7a468306c 100644
--- a/test/charts/mainnet.expect.yaml
+++ b/test/charts/mainnet.expect.yaml
@@ -210,6 +210,8 @@ spec:
               printf "\n\n\n\n\n\n\n"
               
           envFrom:
+            - configMapRef:
+                name: tezos-config
           env:    
             - name: MY_POD_IP
               valueFrom:
diff --git a/test/charts/mainnet2.expect.yaml b/test/charts/mainnet2.expect.yaml
index 72238916a..3f4aded4e 100644
--- a/test/charts/mainnet2.expect.yaml
+++ b/test/charts/mainnet2.expect.yaml
@@ -307,6 +307,8 @@ spec:
               printf "\n\n\n\n\n\n\n"
               
           envFrom:
+            - configMapRef:
+                name: tezos-config
           env:    
             - name: MY_POD_IP
               valueFrom:
@@ -662,6 +664,8 @@ spec:
               printf "\n\n\n\n\n\n\n"
               
           envFrom:
+            - configMapRef:
+                name: tezos-config
           env:    
             - name: MY_POD_IP
               valueFrom: