想知道是否可以使用值中存在的埠,否則使用 http.. 類似這樣的東西;
svc:
app:
ports:
- port: 8080
name: http
- port: 8090
name: metrics
app2:
ports:
- port: 8080
name: http
一些服務通過 http 公開他們的指標,一些有指標埠。所以我想將它模板化為:
{{ define "app.service.ports" }}
{{ range (index .Values.svc (include "app.refName" .) "ports") }}
- name: {{ .name }}
{{ end }}
{{ end }}
這將正確地提取每個埠名稱,但我想提取指標(如果存在),否則拉 http .. 有人能指出我正確的方向嗎?
uj5u.com熱心網友回復:
Gotext/template語言和 Helm 擴展都沒有辦法在這樣的復雜結構中進行通用查找。相反,您需要手動遍歷串列并使用變數來記住您所看到的內容。
{{- $ports := index .Values "svc" (include "app.refName" .) "ports" -}}
{{-/* Set up variables to remember what we've seen. Initialize them
to an empty dictionary, which is false in a conditional. -*/}}
{{- $httpPort := dict -}}
{{- $metricsPort := dict -}}
{{-/* Scan the list of ports. */-}}
{{- range $port := range $ports -}}
{{- if eq $port.name "metrics" -}}
{{- $metricsPort = $port -}}
{{- else if eq $port.name "http" -}}
{{- $httpPort = $port -}}
{{- end -}}
{{- end -}}
{{-/* Emit the metrics port if it exists, else the HTTP port. */-}}
{{- coalesce $metricsPort $httpPort | toYaml -}}
在更高級的語言中,您可以想象搜索一個串列,并且對于每個字典元素,如果它有一個name帶有 value 的鍵,則接受它metrics。這通常涉及將 lambda 或匿名函式傳遞給find()函式,并且該text/template語言不支持。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425766.html
