如何在 configmap.yaml (Helm) 中使用 json 文件?

Luc*_*ucy 3 kubernetes kubernetes-helm configmap

我正在使用 Helm 部署到 Kubernetes 集群。我研究了配置映射,发现可以从文件中检索数据并将其放入配置映射中。

我有以下内容configmap.yaml

kind: ConfigMap 
apiVersion: v1 
metadata:
  name: {{ .Values.app.configMap }}
  namespace: {{ .Values.app.namespace }}
data:
    config.json: |-
      {{ .Files.Glob "my-config.json" | indent 2}}
Run Code Online (Sandbox Code Playgroud)

我的deployment.yaml包含相关的volumeMount(如果我将实际的json数据直接放入configmap.yaml中,那么配置就会部署)。我的configmap.yamldeployment.yaml都保存在 /chart/templates 中,但我保存my-config.json在基本 helm 图表目录中,位于templates文件夹之外。

当我尝试使用图表进行部署时,出现以下错误:

Error: template: chart/templates/configmap.yaml:8:54: executing "chart/templates/configmap.yaml" at <2>: wrong type for value; expected string; got engine.files
Run Code Online (Sandbox Code Playgroud)

如何.json在 configmap 中使用该文件而不将原始 json 数据直接放入 yaml 文件中?

Dav*_*aze 12

.FilesHelm内置对象文档中描述了该对象。 .Files.Glob返回匹配某种模式的文件列表,例如*.json;您可能想要.Files.Get返回文件内容。

YAML 对空格处理和缩进也非常敏感。当您检索文件时,您可能希望该行从第一列开始,但随后使用比indent前一行的缩进级别更多的数字调用该函数。这也会缩进第一行,您可以仔细检查helm template是否出现了正确的内容。

data:
  {{-/* Note, indent of only two spaces */}}
  config.json: |-
{{ .Files.Get "my-config.json" | indent 4 }}
{{/* .Get, not .Glob; indent 4 spaces, more than 2 above */}}
Run Code Online (Sandbox Code Playgroud)