gpt4 book ai didi

kubernetes - 如何从需要模板化的文件目录生成配置映射?

转载 作者:行者123 更新时间:2023-12-02 00:49:50 27 4
gpt4 key购买 nike

我可以从目录生成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>

最佳答案

我找到了一种使用 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 }}

关于kubernetes - 如何从需要模板化的文件目录生成配置映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57879351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com