0

这篇 Stack Overflow 帖子中,您似乎应该能够渲染文本,toYaml然后将其传递给,tpl但这对我不起作用。

采取以下措施:

#values.yaml
configMaps:
  test:
    data:
      config.yaml: |-
        aaaaaa: sdlkfjlskdfj
        bbbb: sdlkfjlskdfj
        ccccc: sdlkfjlskdfj
          ssdfs: slkdjflksdj
        cccc: sdlkfjlskdfj
          lskdjflksd: slkdjflksdj
          sdfs: slkdjflksdj
        AList: 
          - aaaa
          - bbbb
          - cccc

#In my chart
apiVersion: v1
kind: ConfigMap
metadata:
  name: mymap
data:
  {{ tpl (toYaml .Values.configMaps.test.data) . }}

这一直有效,直到我实际向需要渲染的文本添加一些内容。

当我尝试向该文本添加函数时,出现错误:

#values.yaml
configMaps:
  test:
    data:
      config.yaml: |-
        valueFromFunction: prefix-{{ include "myfunction" . }}-suffix
        aaaaaa: sdlkfjlskdfj
        bbbb: sdlkfjlskdfj
        ccccc: sdlkfjlskdfj
          ssdfs: slkdjflksdj
        cccc: sdlkfjlskdfj
          lskdjflksd: slkdjflksdj
          sdfs: slkdjflksdj
        AList: 
          - aaaa
          - bbbb
          - cccc

那是因为当它到达tpl函数时看起来像这样:include \\\"myfunction\\\"\n

tpl 我尝试先运行它,但这给了我另一个错误:wrong type for value; expected string; got map[string]interface {}

4

1 回答 1

1

There are several ways to write and escape strings in YAML. It looks like toYaml is picking a double-quoted string, which doesn't work for this particular case.

For this particular setup, you probably know that .Values.configMaps.test.data has a specific structure: it is a string-keyed map with string values, and there is no further nesting. If you take advantage of that knowledge, then you can invoke tpl on the string values directly, and there will not be an additional layer of YAML quoting.

You should be able to do something like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mymap
data:
{{- $k, $v := range .Values.configMaps.test.data }}
  {{ $k }}: |-
{{ tpl $v . | indent 4 }}
{{- end }}
于 2021-10-30T01:14:19.407 回答