问题描述
我在 helm 库中有以下代码段,它应该将文件夹中的所有文件添加到 ConfigMap 中,但那些以 .tz 结尾的文件除外。
{{- /*
add the contents of every file in the config folder of this IOC helm chart
into the config map - this must include start.sh the startup script.
The files must be text only. Any files ending in .tz are explicitly ommitted
*/ -}}
{{ (.Files.Glob "config/*[!.tz]").AsConfig | indent 2 }}
version.txt: |
IOC {{ .Release.Name }} version {{ .Chart.AppVersion }}
这不太有效,因为它过滤掉了所有以 .或 t 或 z。
我不知道如何使用 Go globbing 来做到这一点。我也无法计算出对 .Files.Glob
创建的列表使用“without”的语法。
谁能教教我?谢谢!
解决方法
Helm 文档引用了 Go "github.com/gobwas/glob"
包以获得支持的 glob 语法;不支持任何类型的“例外”情况,除了单个字符。
您可以做的是迭代所有文件,然后使用正常条件排除您不想要的文件。您必须自己构建 ConfigMap 结构,而不是依赖 AsConfig
助手。
data:
{{- range $path,$_ := .Files.Glob "config/*" }}
{{- if not (hasSuffix ".tz" $path) }}
{{ $path }}: |
{{ .Files.Get $path | indent 4 }}
{{- end }}
{{- end }}
,
谢谢@David Maze。这几乎有效,但对 .Files.Get 的引用失败了,我认为这是因为 .上下文成为范围循环内的范围计数器。
在循环中将 $ 添加到 .Files 使其正常工作。
还要完全复制 AsConfig,我需要从中提取基本名称 我通过添加 regexReplaceAll 所做的路径。
data:
{{- range $path,$_ := .Files.Glob "config/*" }}
{{- if not (hasSuffix ".tz" $path) }}
{{ regexReplaceAll "(.*)/" $path "" }}: |
{{ $.Files.Get $path | indent 4 }}
{{- end }}
{{- end }}