Helm Charts中Ingress嵌套循环路由输出错误求助
Helm Ingress路由URL生成问题排查与修复
问题场景
在Helm Chart的values.yml中定义了嵌套的Ingress hosts配置:
hosts: - host: paths: - path: / pathType: Prefix - host: ppa paths: - path: /api/v1/ppob pathType: Prefix - path: /api/v1/pgob pathType: Prefix
尝试通过notes.txt中的模板生成路由URL:
1. Get the application URL by running these commands: {{- $domainname := .Values.npdomain -}} {{- $fullName := include "ppgp.fullname" . -}} {{ println }} {{- if .Values.ingress.enabled }} {{- range $host := .Values.ingress.hosts }} {{- range .paths }} http{{ if $.Values.ingress.tls }}s{{ end }}://{{ if .host }}{{ .host }}.{{ $domainname }}{{ else }}{{ $fullName }}.{{ $domainname }}{{ end }}{{ .paths.path }} {{- end }} {{- end }} {{- end }}
期望输出(TLS启用时):
https://ppgp.mydomainvar https://ppa.mydomainvar/api/v1/ppob https://ppa.mydomainvar/api/v1/pgob
实际输出:
https://ppgp.mydomainvar https://ppgp.mydomainvar/api/v1/ppob https://ppgp.mydomainvar/api/v1/pgob
第二个host下的路径未使用指定的ppa主机名,而是默认使用了服务名ppgp。
问题原因
模板存在两处上下文引用错误:
- 在
paths循环内部,.host引用的是当前path对象的属性(而path对象没有host字段),而非外层$host对象的host属性; .paths.path是错误的引用方式,当前循环上下文已经是单个path对象,直接用.path即可获取路径值。
修复方案
修正notes.txt中的模板代码:
1. Get the application URL by running these commands: {{- $domainname := .Values.npdomain -}} {{- $fullName := include "ppgp.fullname" . -}} {{ println }} {{- if .Values.ingress.enabled }} {{- range $host := .Values.ingress.hosts }} {{- range .paths }} http{{ if $.Values.ingress.tls }}s{{ end }}://{{ if $host.host }}{{ $host.host }}.{{ $domainname }}{{ else }}{{ $fullName }}.{{ $domainname }}{{ end }}{{ .path }} {{- end }} {{- end }} {{- end }}
关键修改点
- 将
{{ if .host }}改为{{ if $host.host }}:明确引用外层range循环中定义的$host变量的host属性; - 将
{{ .paths.path }}改为{{ .path }}:当前上下文是单个path对象,直接访问其path属性。
验证结果
修正后,TLS启用时的输出与期望一致:
https://ppgp.mydomainvar https://ppa.mydomainvar/api/v1/ppob https://ppa.mydomainvar/api/v1/pgob
内容的提问来源于stack exchange,提问作者Venu Reddy
相关产品推荐
相关产品推荐

