无法将 dict 传递到 tpl 函数(实际上除了 <dot>')

Mar*_*cha 1 kubernetes-helm

我是 helm/go 模板的新手,似乎我仍然不明白上下文是如何工作的。有人可以解释一下,问题出在哪里以及为什么 dict 不能这样使用?

我的文件中有这个模板stubs/.test.yaml(我想将其与 tpl 函数一起使用):

test: abc
test2: {{.Values.key}}
Run Code Online (Sandbox Code Playgroud)

在values.yaml中只有:

key: value
Run Code Online (Sandbox Code Playgroud)

我包括这样的模板:

{{ tpl (.Files.Get "stubs/.test.yaml" ) (.) }}
Run Code Online (Sandbox Code Playgroud)

产生:

test: abc
test2: value
Run Code Online (Sandbox Code Playgroud)

因此,我从 Stubs/.test.yaml 加载文件,并将其传递给当前(根)上下文,然后.Values.key找到并替换该文件。正如预期的那样。

现在让我们重新定义stubs/.test.yaml为:

test: abc
test2: {{.key}}
Run Code Online (Sandbox Code Playgroud)

并在替换模板时传递字典作为上下文,如下所示:

{{ tpl (.Files.Get "stubs/.test.yaml" ) (dict "key" .Values.key) }}
Run Code Online (Sandbox Code Playgroud)

所以我期望得到相同的结果,因为我传递了 dict 代替根上下文, dict 有一个名为 的值key,其值取自.Values.key,该值在该模板中被正确引用。但我得到了:

error calling tpl: cannot retrieve Template.Basepath from values inside tpl function: test: abc
test2: {{.key}}: "BasePath" is not a value
Run Code Online (Sandbox Code Playgroud)

反而。这里发生了什么?

编辑:我对它进行了更多测试,这与字典无关。我必须将.第二个参数传递给 tpl 函数,否则会失败。

3.10版本

Mar*_*cha 7

我想我在这找到了答案问题中找到了答案

IIUC:在tpl启动之前,它在根上下文中创建对象.Template(请参阅builtin_objects),其中包含有关当前处理的模板的数据。因此,如果您正在使用,tpl则不得更改上下文并传递.,否则您必须传递一些内容作为上下文传递。

因此,如果我们像我的问题中的示例那样传递 dict:

{{ tpl (.Files.Get "stubs/.test.yaml" ) (dict "key" .Values.key) }}
Run Code Online (Sandbox Code Playgroud)

不起作用。但是如果你像这样扩展 dict 定义:

{{ tpl (.Files.Get "stubs/.test.yaml" ) (dict "key" .Values.key "Template" $.Template) }}
Run Code Online (Sandbox Code Playgroud)

现在就可以了。