helm chart template merging root scope values in multiple files cause stack overflow error

3/19/2020

I'm new to helm chart and recently is taking over a legacy project(the initial commiter has left so nobody to ask).

The chart is running well, but when I try to follow its patterns to add a new manifest, I got error. To make it simple, this is just demo chart:

templates/deployment1.yaml (it's working well)

deploy1:
{{ $rootScope := . }}
{{ range $folder, $foldercontents := .Values.mydisk }}
{{- $merged := merge $foldercontents $rootScope.Values -}}
{{$foldercontents.files}}
{{ end }}

Values.yaml:

mydisk:
folder1: 
  files: 
    - aaa
folder2:
  files: 
    - bbb

In above deployment, $merged is a complex data structure that will be passed as parameter in almost every template (to simplify, I just omitted the usages here).

Output:

---
# Source: hello/templates/deployment.yaml
deploy1:

[aaa]
[bbb]

So I added another file:

templates/deployment2.yaml

deploy2:
{{ $rootScope := . }}
{{ range $folder, $foldercontents := .Values.mydisk }}
{{- $merged := merge $foldercontents $rootScope.Values -}}
{{$foldercontents.files}}
{{ end }}

But when I run helm template ./, I got stackoverflow error:

runtime: goroutine stack exceeds 1000000000-byte limit fatal error: stack overflow

runtime stack: runtime.throw(0x16585ea, 0xe) /usr/local/go/src/runtime/panic.go:616 +0x88 runtime.newstack() /usr/local/go/src/runtime/stack.go:1054 +0x72d runtime.morestack() /usr/local/go/src/runtime/asm_amd64.s:480 +0x91

goroutine 1 [running]: reflect.flag.mustBe(0x15, 0x15) /usr/local/go/src/reflect/value.go:205 +0xc3 fp=0xc0629012f8 sp=0xc0629012f0 pc=0x4cc1e3 reflect.Value.MapKeys(0x146e7c0, 0xc042630240, 0x15, 0x0, 0x0, 0x0) /usr/local/go/src/reflect/value.go:1103 +0x50 fp=0xc0629013a0 sp=0xc0629012f8 pc=0x4cfe80 ......

I guess the error was cuased by some variable evaluation, the $rootScope can only be got before other manifests evaluated, but the two files rely on each other, right?

How to solve this? I cannot find any document on this. I'm using helm v2.9.1.

-- Lei Yang
dictionary
go-templates
kubernetes-helm

0 Answers