[英]Inject file into Helm template
I have a list of properties defined in values.yaml
as follows:我在values.yaml
定义了一个属性列表,如下所示:
files:
- "file1"
- "file2"
Then in my template I want to create config maps out of my values.然后在我的模板中,我想根据我的值创建配置映射。
I came up with the following template:我想出了以下模板:
{{- range $value := .Values.files }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $value }}
data:
{{ $value }}: {{ .Files.Get (printf "%s/%s" "files" $value) | indent 4 }}
{{- end }}
As you can see I want to have configmaps with same name as files.正如您所看到的,我希望拥有与文件同名的配置映射。 I mixed up several parts of documentation, however my template does not work as expected.我混淆了文档的几个部分,但是我的模板没有按预期工作。
How can I achieve configmap creation through templates?如何通过模板实现 configmap 创建?
//EDIT I expect to have the following ConfigMap: //EDIT我希望有以下 ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: file1
data:
file1: <file1 content>
---
apiVersion: v1
kind: ConfigMap
metadata:
name: file2
data:
file2: <file2 content>
Try the following:请尝试以下操作:
{{- $files := .Files }}
{{- range $value := .Values.files }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $value }}
data:
{{ $value }}: |
{{ $files.Get (printf "%s/%s" "files" $value) | indent 6 }}
{{- end }}
You problem seems to be with incorrect indentation.您的问题似乎是缩进不正确。 Make sure the line with $files.Get
starts with no spaces.确保以$files.Get
开头的行没有空格。
And I also added {{- $files := .Files }}
to access .Files
, because for some reason range
is changeing the .
而且我还添加了{{- $files := .Files }}
来访问.Files
,因为由于某种原因range
正在改变.
scope.范围。
I have found this example in documentation but it seems to work only if file's content are one-line.我在文档中找到了这个例子,但它似乎只有在文件内容是一行时才有效。 So if your files are one line only, then you should check the example.因此,如果您的文件只有一行,那么您应该检查示例。
Also notice the |
还要注意|
after {{ $value }}:
. {{ $value }}:
。 You need it because file content is a multiline string.您需要它,因为文件内容是一个多行字符串。 Check this StackQuestion on how to use multiline strings in yaml.查看此 StackQuestion ,了解如何在 yaml 中使用多行字符串。
Try something as below尝试如下
{{ $currentScope := .}}
{{- range $value := .Values.files }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $value }}
data:
{{- with $currentScope}}
{{ $value }}: {{ .Files.Get (printf "%s/%s" "files" $value) | indent 4 }}
{{- end }}
{{- end }}
My Chart structure我的图表结构
├── Chart.yaml
├── charts
├── files
│ ├── file1
│ └── file2
├── templates
│ ├── configmap.yaml
│ ├── deployment.yaml
│ ├── secret.yaml
│ └── service.yaml
└── values.yaml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.