簡體   English   中英

為什么在 `if` 塊中定義的變量會出現“未定義變量”錯誤?

[英]Why do I get an "undefined variable" error for a variable defined inside an `if` block?

當我嘗試使用模板文件安裝 Helm 圖表時:

{{ if eq .Release.Namespace "fedx-app-1100" }}{{ $nodePort := 30106 }}{{ end }}
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: {{ .Release.Namespace }} 
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: {{ $nodePort }}

它產生一個錯誤:

Error: parse error at (fedx-install/templates/mysql.yaml:21): undefined variable "$nodePort"

是什么導致了這個錯誤? 如何重組模板文件以避免它?

試試這個

{{ if eq .Release.Namespace "fedx-app-1100" }}{{ $nodePort := 30106 }}
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: {{ .Release.Namespace }} 
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: {{ $nodePort }}
{{ end }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM