如何使用Helm模板移除values文件中的嵌套service变量
解决Helm模板中删除service字段的方法
针对你提供的values配置,要在Helm模板里去掉每个destination下的service字段,可以用Helm的omit函数来实现,直接在渲染配置时过滤掉不需要的字段。
原始Values配置
http: - match: - headers: end-user: test: true route: - destination: host: apie service: 'svc-1' - route: - destination: host: api service: 'svc-2'
模板实现代码
在你的Helm模板文件(比如templates/xxx.yaml)里,这样写就能生成去掉service字段的配置:
http: {{- range $httpEntry := .Values.http }} - {{- with $httpEntry.match }} match: {{- toYaml . | nindent 4 }} {{- end }} route: {{- range $route := $httpEntry.route }} - destination: {{- omit $route.destination "service" | toYaml | nindent 8 }} {{- end }} {{- end }}
代码说明
- 用
range遍历http数组里的每一条规则 - 如果当前规则有
match字段,就原样渲染保留 - 遍历每条规则下的
route,对destination使用omit函数移除service字段,再转成YAML格式输出,同时用nindent保证缩进正确
这样渲染出来的最终配置就会自动去掉所有service字段,结果如下:
http: - match: - headers: end-user: test: true route: - destination: host: apie - route: - destination: host: api
内容的提问来源于stack exchange,提问作者SET
相关产品推荐
相关产品推荐

