我正在尝试将多行 json 字符串插入到 helm 模板中,以进行 Kubernetes secret 所需的 base64 编码。
目标:
b64enc
进行 base64 编码myfile1.json
不起作用,但 myfile2.json
作品。我不想把整个 json 文件放在
values.yaml
中。 .apiVersion: v1
kind: Secret
metadata:
name: {{ template "mychart.fullname" . }}
labels:
app: {{ template "mychart.name" . }}
chart: {{ template "mychart.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
type: Opaque
data:
myfile.json: {{ |-
{
"item1": {
"name": "{{ .Values.item1.name }}"
},
"item2": {
}
} | b64enc }}
myfile2.json: {{ .Values.myfile2 | b64enc }}
最佳答案
您实际上不需要对 helm 图表中的 secret 进行 base64 编码。如果您使用 stringData
字段而不是 data
字段,Kubernetes 知道它需要在 key 部署时对数据进行 base64 编码。
来自文档(Source):
The Secret contains two maps:
data
andstringData
. Thedata
field is used to store arbitrary data, encoded using base64. ThestringData
field is provided for convenience, and allows you to provide secret data as unencoded strings.
所以我们可以使用
stringData
重写您的 secret 而不是 data
并在模板中保留多行 json 字符串,如下所示:apiVersion: "v1"
kind: "Secret"
metadata:
name: {{ template "mychart.fullname" . }}
labels:
app: {{ template "mychart.name" . }}
chart: {{ template "mychart.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
type: "Opaque"
stringData:
myfile.json: |-
{
"item1": {
"name": "{{ .Values.item1.name }}"
},
"item2": {
}
}
myfile2.json: {{ .Values.myfile2 }}
请注意,这并不意味着您突然需要担心有未编码的 secret 。
stringData
最终将被 base64 编码并转换为 data
安装时,因此一旦加载到 Kubernetes 中,它的行为将完全相同。同样,来自文档 (强调我的) (Source):
stringData
allows specifying non-binary secret data in string form. It is provided as a write-only convenience method. All keys and values are merged into thedata
field on write, overwriting any existing values. It is never output when reading from the API.
关于json - 将多行 json 字符串插入 helm 模板以进行 base64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54152619/