如何通过 Helm 图表从 json 中读取值

How to read a value from json through Helm charts

我在 json 文件中定义了值。

cat templates/encrypt.json
{"encrypt": "cg8StVXbQJ0gPvMd9o7yrg=="}

必须将值传递给 yaml 文件,如下所示

-config-file={{ tpl (.Files.Get "encrypt.json") . | b64enc }}  \

下面是 helm 图表片段

exec /bin/consul agent \
            -node="${NODE}" \
            -advertise="${POD_IP}" \
            -bind=0.0.0.0 \
            -client=0.0.0.0 \
            {{- if .Values.client.grpc }}
            -hcl="ports { grpc = 8502 }" \
            {{- end }}
            -config-dir=/consul/config \
            {{- range .Values.client.extraVolumes }}
            {{- if .load }}
            -config-dir=/consul/userconfig/{{ .name }} \
            {{- end }}
            {{- end }}
            -datacenter={{ .Values.global.datacenter }} \
            -data-dir=/consul/data \
            -config-file={{ tpl (.Files.Get "encrypt.json") . | b64enc }}  \
            {{- if (.Values.client.join) and (gt (len .Values.client.join) 0) }}

当我 运行 我的健康图表时,我收到以下错误。

Error: unable to decode "": Object 'Kind' is missing in '{"encrypt":"cg8StVXbQJ0gPvMd9o7yrg=="}'

您用 {{ tpl (.Files.Get "encrypt.json") . | b64enc }} 注入的是 json 的内容,即 {"encrypt": "cg8StVXbQJ0gPvMd9o7yrg=="}。但我认为这不是该参数所期望的。它似乎期望 Pod 中应该可用的文件的文件名,这可以通过安装 configmap 来完成。即how the consul helm chart in the official kubernetes charts handles it:

            {{- if .Values.Gossip.Encrypt }}
            if [ -e /etc/consul/secrets/gossip-key ]; then
              echo "{\"encrypt\": \"$(base64 /etc/consul/secrets/gossip-key)\"}" > /etc/consul/encrypt.json
              GOSSIP_KEY="-config-file /etc/consul/encrypt.json"
            fi
            {{- end }}

lets the user set a gossip key in the values file and sets that in a secret which is mounted into the Pods as a volume。如果可以的话,我建议您遵循该图表的方法。

我猜您正在做的是在 the consul helm chart that Hashicorp provides 之上构建,因为您包含的代码与此类似。因此,大概您不能使用 kubernetes 存储库中的那个,但您应该能够按照该图表为该配置文件采用的方法。