英文:
Nested loop in helm charts
问题
以下是您要翻译的内容:
在helm charts的values.yml中,我有一个嵌套的ingress值,如下所示:
hosts:
- host:
paths:
- path: /
pathType: Prefix
- host: ppa
paths:
- path: /api/v1/ppob
pathType: Prefix
- path: /api/v1/pgob
pathType: Prefix
使用这个,我试图通过notes.txt中的代码打印所有的路由,代码如下:
1. 通过运行以下命令获取应用程序URL:
{{- $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块和第二个.host块中有两个.path块。
如果有人能帮忙解决这个问题,将不胜感激。
提前感谢!
英文:
I have a nested ingress value in the helm charts values.yml as below
hosts:
- host:
paths:
- path: /
pathType: Prefix
- host: ppa
paths:
- path: /api/v1/ppob
pathType: Prefix
- path: /api/v1/pgob
pathType: Prefix
with this, I'm trying to print all the routes through notes.txt with the code as
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 }}
the output I'm expecting is if tls is enabled
https://ppgp.mydomainvar
https://ppa.mydomainvar/api.v1/ppob
https://ppa.mydomainvar/api.v1/pgob
The output I get is
https://ppgp.mydomainvar
https://ppgp.mydomainvar/api.v1/ppob
https://ppgp.mydomainvar/api.v1/pgob
I know something is incorrect, but I cannot figure it out as I have two .host blocks and I have two .path in the second .host block.
Appreciate it if someone could help with this.
Thanks in advance
答案1
得分: 1
在检查.host
时,您位于最内层的range
循环中。在这个上下文中,.
是来自paths:
列表之一的项目之一。
您已经有一个对hosts:
列表中父级项目的引用$host
,所以最简单的修复方法是在出现.host
的地方将其更改为$host.host
。
与其立即深入到值结构的最深层,然后尝试从父上下文构造值,您还可以考虑在它出现的级别上创建URL的每个部分。例如,paths:
中的每个项目都属于同一个hosts:
条目,并且将具有URL的相同主机部分;这将使发出URL的代码行稍微清晰一些。可能会像这样:
{{- $domainname := .Values.npdomain -}}
{{- $fullName := include "ppgp.fullname" . -}}
{{- with .Values.ingress }}
{{- if .enabled }}
{{- $scheme := empty .tls | ternary "https" "http" }}
{{- range .hosts }}
{{- $host := printf "%s.%s" (.host | default $fullName) $domainName }}
{{- range .paths }}
{{- printf "%s://%s/%s" $scheme $host .path }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
如果您需要进一步的帮助,请告诉我。
英文:
Where you check .host
you are in the innermost range
loop. In that context, .
is one of the items from one of the paths:
lists.
You already have a reference $host
to the parent item in the hosts:
list, and so the easiest fix is to change .host
to $host.host
the places where it appears.
Rather than immediately drilling down to the deepest level of the values structure and then trying to construct values from the parent context, you also might consider creating each of the parts of the URL at the level it appears. Each item in paths:
belongs to the same hosts:
entry and will have the same host part of the URL, for example; this will make the line that emits the URL a little clearer. That might look like:
{{- $domainname := .Values.npdomain -}}
{{- $fullName := include "ppgp.fullname" . -}}
{{- with .Values.ingress }}
{{- if .enabled }}
{{- $scheme := empty .tls | ternary "https" "http" }}
{{- range .hosts }}
{{- $host := printf "%s.%s" (.host | default $fullName) $domainName }}
{{- range .paths }}
{{- printf "%s://%s/%s" $scheme $host .path }}
{{ end }}
{{- end }}
{{- end }}
{{- end }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论