Helm Chart - 多行处理文件结果
Helm Chart - Process File result with multiple lines
我有一个 ConfigMap
,我在其数据属性中包含一个文件,我需要从中替换几个字符串。但是我无法将它(“替换”)分成几行,这样它就不会得到一条巨线。我该怎么做?
这是我不想要的:
apiVersion: v1
kind: ConfigMap
data:
{{ (.Files.Glob "myFolder/*.json").AsConfig | indent 2 | replace "var1_enabled" (toString .Values.myVar1.enabled) | replace "var2_enabled" (toString .Values.myVar2.enabled) }}
这就是我想要做的:
apiVersion: v1
kind: ConfigMap
data:
{{ (.Files.Glob "myFolder/*.json").AsConfig | indent 2 |
replace "var1_enabled" (toString .Values.myVar1.enabled) |
replace "var2_enabled" (toString .Values.myVar2.enabled) }}
执行此操作的正确语法是什么?
What is the right syntax to do this?
在this documentation. There are many different ways to achieve your goal, it all depends on the specific situation. You have everything in that documentation. Look at the example中有很好的描述,与您的当前情况最相关:
When writing templates, you may find yourself wanting to inject the contents of a file into the template. As we saw in previous chapters, there are two ways of doing this:
- Use
{{ .Files.Get "FILENAME" }}
to get the contents of a file in the chart.
- Use
{{ include "TEMPLATE" . }}
to render a template and then place its contents into the chart.
When inserting files into YAML, it's good to understand the multi-line rules above. Often times, the easiest way to insert a static file is to do something like this:
myfile: |
{{ .Files.Get "myfile.txt" | indent 2 }}
Note how we do the indentation above: indent 2
tells the template engine to indent every line in "myfile.txt" with two spaces. Note that we do not indent that template line. That's because if we did, the file content of the first line would be indented twice.
更多信息请查看 similar problem on github and 。
编辑:
But I'm not able to divide it (the "replaces") into several lines so that it doesn't get a giant line. How can I do this?
这是不可能实现的。 Go Template 不支持换行符。 有关更多信息,请参阅 . and this documentation
The input text for a template is UTF-8-encoded text in any format. "Actions"--data evaluations or control structures--are delimited by "{{" and "}}"; all text outside actions is copied to the output unchanged. Except for raw strings, actions may not span newlines, although comments can.
由于无法在 ConfigMap
处将表达式拆分为多行,我终于使用 _helpers.tpl
文件找到了解决方案。
基本上我创建了一个字典,其中包含我要替换的所有变量以及要替换的相应新值,然后我迭代了该字典并在我的文件配置中进行了替换:
{{/*
Manage the files and replace some variables
*/}}
{{- define "myFiles" -}}
{{ $filesConfig := (.Files.Glob "myFolder/*.json").AsConfig }}
{{ $myVars := dict "var1_enabled" (toString .Values.myVar1.enabled) }}
{{ $myVars = merge $myVars (dict "var2_enabled" (toString .Values.myVar2.enabled)) }}
{{ range $key, $value := $myVars }}
{{ $filesConfig = ($filesConfig | replace $key $value) }}
{{ end }}
{{ $filesConfig }}
{{- end -}}
然后我将 ConfigMap
更改为如下内容:
{{- $myFiles := include "myFiles" . -}}
apiVersion: v1
kind: ConfigMap
data:
{{ $myFiles }}
我对这种语言一无所知,所以如果你知道如何改进它,请随时发表评论。
我有一个 ConfigMap
,我在其数据属性中包含一个文件,我需要从中替换几个字符串。但是我无法将它(“替换”)分成几行,这样它就不会得到一条巨线。我该怎么做?
这是我不想要的:
apiVersion: v1
kind: ConfigMap
data:
{{ (.Files.Glob "myFolder/*.json").AsConfig | indent 2 | replace "var1_enabled" (toString .Values.myVar1.enabled) | replace "var2_enabled" (toString .Values.myVar2.enabled) }}
这就是我想要做的:
apiVersion: v1
kind: ConfigMap
data:
{{ (.Files.Glob "myFolder/*.json").AsConfig | indent 2 |
replace "var1_enabled" (toString .Values.myVar1.enabled) |
replace "var2_enabled" (toString .Values.myVar2.enabled) }}
执行此操作的正确语法是什么?
What is the right syntax to do this?
在this documentation. There are many different ways to achieve your goal, it all depends on the specific situation. You have everything in that documentation. Look at the example中有很好的描述,与您的当前情况最相关:
When writing templates, you may find yourself wanting to inject the contents of a file into the template. As we saw in previous chapters, there are two ways of doing this:
- Use
{{ .Files.Get "FILENAME" }}
to get the contents of a file in the chart.- Use
{{ include "TEMPLATE" . }}
to render a template and then place its contents into the chart.When inserting files into YAML, it's good to understand the multi-line rules above. Often times, the easiest way to insert a static file is to do something like this:
myfile: |
{{ .Files.Get "myfile.txt" | indent 2 }}
Note how we do the indentation above:
indent 2
tells the template engine to indent every line in "myfile.txt" with two spaces. Note that we do not indent that template line. That's because if we did, the file content of the first line would be indented twice.
更多信息请查看 similar problem on github and
编辑:
But I'm not able to divide it (the "replaces") into several lines so that it doesn't get a giant line. How can I do this?
这是不可能实现的。 Go Template 不支持换行符。 有关更多信息,请参阅
The input text for a template is UTF-8-encoded text in any format. "Actions"--data evaluations or control structures--are delimited by "{{" and "}}"; all text outside actions is copied to the output unchanged. Except for raw strings, actions may not span newlines, although comments can.
由于无法在 ConfigMap
处将表达式拆分为多行,我终于使用 _helpers.tpl
文件找到了解决方案。
基本上我创建了一个字典,其中包含我要替换的所有变量以及要替换的相应新值,然后我迭代了该字典并在我的文件配置中进行了替换:
{{/*
Manage the files and replace some variables
*/}}
{{- define "myFiles" -}}
{{ $filesConfig := (.Files.Glob "myFolder/*.json").AsConfig }}
{{ $myVars := dict "var1_enabled" (toString .Values.myVar1.enabled) }}
{{ $myVars = merge $myVars (dict "var2_enabled" (toString .Values.myVar2.enabled)) }}
{{ range $key, $value := $myVars }}
{{ $filesConfig = ($filesConfig | replace $key $value) }}
{{ end }}
{{ $filesConfig }}
{{- end -}}
然后我将 ConfigMap
更改为如下内容:
{{- $myFiles := include "myFiles" . -}}
apiVersion: v1
kind: ConfigMap
data:
{{ $myFiles }}
我对这种语言一无所知,所以如果你知道如何改进它,请随时发表评论。