News for hackers. Stuff that matters.
Re:Pitfalls of Helm – Insights from 3 yrs with the leading K... by kevincox on Friday December 15, 2023 @10:36AM (#38655153)
I totally agree that string-templating into a data serialization format is a mistake. But you can make life dramatically easier on yourself by doing `{{ something | toJson }}`. In fact write a linter that every single substitution is followed by `| toJson` and you will save yourself a lot of headaches.

The main issue is that it make it more difficult to mix hardcoded and inserted values.

    labels:
        - mylabel
        - {{ extraLabels | indent 4 }} # toJson doesn't work here.
Also the small technical concern that YAML isn't actually a superset of JSON. (But you are far less likely to hit these cases than other escaping bugs).
Re:Pitfalls of Helm – Insights from 3 yrs with the leading K... by mdaniel on Friday December 15, 2023 @01:51PM (#38657241)
As far as I know the way helm thinks about that problem is putting any such literals into a temp copy _then_ serializing https://masterminds.github.io/sprig/lists.html#append-mustap...

  labels: {{ append .extraLabels "myLabel" "myOtherLabel" | toJson }}
your commentary talks about json but your code snippet is in yaml, so it's possible one or both of us are solving the wrong problem
Re:Pitfalls of Helm – Insights from 3 yrs with the leading K... by kevincox on Friday December 15, 2023 @01:55PM (#38657292)
YAML is (almost) a superset of JSON. So it is much easier to serialize data into JSON than avoiding worrying about indentation.
Re:Pitfalls of Helm – Insights from 3 yrs with the leading K... by mdaniel on Friday December 15, 2023 @02:06PM (#38657437)
I think you are lobbying the wrong person about the relationship between yaml and json; I was trying to point out that you had

  thing:
  - item1
  - {{ foo | toJson }} # <-- is not going to do what you expect
unless you quite literally wanted

  thing:
  - alpha
  - - alpha1
    - alpha2
Re:Pitfalls of Helm – Insights from 3 yrs with the leading K... by kevincox on Friday December 15, 2023 @02:09PM (#38657486)
Ah, I did have a typo. But you also copied it wrong. I wrote this

    labels:
        - mylabel
        - {{ extraLabels | indent 4 }} # toJson doesn't work here.
Which has an extra `-` and as you pointed out would produce a nested list.

But I meant this which would merge the lists.

    labels:
        - mylabel
        {{ extraLabels | indent 4 }} # toJson doesn't work here.