0

I'm completely new to helm/k8s. Following question might not be best way to do things or it might not make a lot of sense, but I don't know best(or any other) practices yet, and this is my best attempt to solve something much more complex, while not understanding it properly. Thanks(I beg) for patience with me.

In values.yaml say I have something like this:

whatever1: true
whatever2: true

myConfigs:
  variantA:
    test: tooSteep
  variantB:
    test: learningCurve

in reality it's a lot bigger, of course. Then I have some template, which I need to generate in 2 (or more) variants and I cannot use whole different configs for it, which would overwrite data in values.yaml(it's complicated). Previous solution by college who understands this more than me involves some heavy copy-pasting. What I'd like to see instead is to have one abstract template, say:

whatever1: true
whatever2: true
test: {{ .Values.myConfigs.$what.test}}

which I'd include/tpl somehow within child templates, and say how it should behave conditionally, say:

{{- $what := "variantA" -}}
{{ tpl (.Files.Get "stubs/abstract.template") . }}

The syntax is obviously wrong, and it does not work. I know this is wrong, I cannot find any way which would enable this. I don't know the correct way how to write that. What I need to do is somehow express, which part of .Values should be used in specific part of template. IIUC if I have all data in 1 file I cannot, just use tpl function, load template and pass it context of specific variant, as won't have access to whatever then. So I need to pass the broader context and somehow pass some selector.

I understand, that correct way is probably restructuring files so that I can actually work effectively with context, but that would require me to actually understand which part of configuration will have to go together every time, and I'm far from ready for this refactoring.

Martin Mucha
  • 2,385
  • 1
  • 29
  • 49
  • Are you looking for the `index` function, like [Access a map value using a variable key in a Go template](https://stackoverflow.com/questions/26152088/access-a-map-value-using-a-variable-key-in-a-go-template)? – David Maze Sep 05 '22 at 15:29
  • Also consider that you can supply an additional values file at deploy time using the `helm install -f` option, and it might be simpler to provide a single configuration map object in an additional file than to have this sort of dynamic-lookup structure. – David Maze Sep 05 '22 at 15:30
  • @DavidMaze I indeed tried index, but either I'm stupid or the variable visibility stands in the way — as shown in simplified example, the $what is visible in template where defined, but not in file abstract.template. ~ regarding second comment, please elaborate, no idea how to do that; I need to generate all variants in one helm invocation. – Martin Mucha Sep 05 '22 at 18:10

1 Answers1

0

Not sure what is the correct way, but what could be actually used to solve described problem: do the decision when calling template, and alter the passed context to contain all required information. In my case it wasn't possible just to select specific subtree in actual context, so what I was missing was function dict, which creates a new dictionary, which then can be passed as context.

Martin Mucha
  • 2,385
  • 1
  • 29
  • 49