【问题标题】:How can I generate a configmap from a directory of files that need to be templated?如何从需要模板化的文件目录生成配置映射?
【发布时间】:2020-01-12 17:11:38
【问题描述】:

我可以从目录中生成ConfigMap,但它们不会翻译模板指令或值。以下是Release.Namespace 模板指令未在ConfigMap 中输出的示例。

.
|____Chart.yaml
|____charts
|____.helmignore
|____templates
| |____my-scripts.yaml
|____values.yaml
|____test-files
  |____test1.txt
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: {{ .Release.Namespace }}
data:
  test.txt: |-
{{ .Files.Get "test-files/test1.txt" | indent 4}}
# test-files/test1.txt
test file
{{ .Release.Namespace }}

当我运行helm install . --dry-run --debug --namespace this-should-print 时,这是我得到的与我所期望的:

实际:

---
# Source: test/templates/my-scripts.yaml
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: test
data:
  test.txt: |-
    # test-files/test1.txt
    test file
    {{ .Release.Namespace }}

预期:

---
# Source: test/templates/my-scripts.yaml
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: test
data:
  test.txt: |-
    # test-files/test1.txt
    test file
    this-should-print

或者,我会对指定目录中的每个文件都以如下格式输出感兴趣:

<filename>: |-
  <content>

【问题讨论】:

  • 一个很好问的 SO 问题的好例子:关于您的输入的所有必要信息(目录结构、关键文件的内容)、您运行的命令、您得到的确切内容以及您所期望的内容。非常好!
  • 快速提示,您可以运行helm template 而不是helm install --dry-run
  • 酷,感谢您的提示!

标签: kubernetes kubernetes-helm


【解决方案1】:

.Files.Get 将获取这些文件的原始内容并将其转储到生成的 YAML 中,如果您希望这些文件内容受 Helm 模板自身渲染的影响,这种方法将不起作用。您可以改为在其他模板中创建 named templates 然后 include 它们。

目录结构:tree

.
├── Chart.yaml
├── templates
│   ├── _test1.tpl
│   └── my-scripts.yaml
└── values.yaml

模板:cat templates/my-scripts.yaml

---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: {{ .Release.Namespace }}
data:
  test.txt: |-
{{- include "mychart.test1" . | indent 4 }}

帮手:cat templates/_test1.tpl

{{- define "mychart.test1" }}
test file
{{ .Release.Namespace }}
{{- end }}

结果:helm template . --namespace this-should-print

---
# Source: helm/templates/my-scripts.yaml
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: this-should-print
data:
  test.txt: |-
    test file
    this-should-print

【讨论】:

    【解决方案2】:

    我找到了一种使用 tpl 函数的方法:

    ---
    # templates/myscripts.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-scripts
      namespace: {{ .Release.Namespace }}
    data:
      test.txt: |-
    {{ tpl ( .Files.Get "test-files/test1.txt" ) . | indent 4 }}
    

    新的输出完全符合预期:

    # templates/myscripts.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-scripts
      namespace: this-should-print
    data:
      test.txt: |-
        # test-files/test1.txt
        test file
        this-should-print
    

    对于奖励积分,从目录中获取所有文件而无需在配置映射中更新此列表:

    ---
    # templates/myscripts.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-scripts
      namespace: {{ .Release.Namespace }}
    data:
    {{ tpl (.Files.Glob "groovy-scripts/*").AsConfig . | indent 4 }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多