Helm -- 根据值设置访问列表项

Helm --set access list item based on value

根据this

As of Helm 2.5.0, it is possible to access list items using an array index syntax. For example, --set servers[0].port=80

但是是否可以根据值而不是索引访问列表项?

举个例子

servers:
  - port: 80
    host: example.com
  - port: 443
    host: unknown

有没有办法像这样使用它:--set servers[port==443].host=company.com?

不,helm --set 不能那样做;那里没有查询语言。

对于许多实用的东西,helm --set 的语法是...独特的,使用覆盖值文件效果更好。

# values.company.com.yaml
servers: # replaces the list in the chart's `values.yaml`
  - port: 80
    host: example.com # so this needs to be repeated
  - port: 443
    host: company.com # if you want to just change this
helm install ... -f values.company.com.yaml

此限制还意味着您可能希望重组您的 Helm 值以使其更简单。带有 name 键的映射列表在 Kubernetes 中很常见,但在 Helm 值中有点难以操作。如果你知道只有这两个端口,你可以有专用值:

# values.yaml
httpsHost: unknown
insecureHttpHost: example.com
helm install ... --set httpsHost=company.com

另一种选择是有两个列表,一个是“固定的”列表,另一个具有管理员提供的附加值。 “固定”值仍然可以被覆盖,但如果您只需要添加值,则有一个专门的地方可以做到这一点。

# values.yaml
servers:
  - { port: 80, host: example.com }
extraServers: []
# values.company.com.yaml
extraServers:
  - { port: 443, host: company.com }
# templates/???.yaml
{{- define "server-url" -}}
http://{{ .host }}:{{ .port }}?
{{- end -}}

urls:
{{- range .Values.servers }}
  - {{ include "server-url" . }}
{{- end }}
{{- range .Values.extraServers }}
  - {{ include "server-url" . }}
{{- end }}