使用Helm模板文件有条件地设置在整个模板文件中使用的变量。

huangapple go评论123阅读模式
英文:

With helm template file conditionally set a variables used throughout template file

问题

我想在我的Helm模板文件的顶部有条件地设置一个名为'go'的变量,然后在模板文件的其他地方使用它。以下是我尝试但不起作用的方法:

在模板文件的顶部,我进行如下操作:

  1. {{- if eq $.Values.kafka.create true -}}
  2. {{- $kafka_port := 9094 -}}
  3. {{- else -}}
  4. {{- $kafka_port := $.Values.kafka.port -}}
  5. {{- end }}

但是,当我尝试执行"helm install"时,我在模板文件的其他地方使用$kafka_port时出现解析错误。具体的错误是:

错误:解析错误 (tombolo/templates/tom-node-launch.yaml:89):未定义变量"$kafka_port"

英文:

I want to conditionally set a 'go' variable at the top of my helm template file then use it later in the template file. Here is what I tried that doesn't work:

At the top of my template file, I do the following:

  1. {{- if eq $.Values.kafka.create true -}}
  2. {{- $kafka_port := 9094 -}}
  3. {{- else -}}
  4. {{- $kafka_port := $.Values.kafka.port -}}
  5. {{- end }}

But when I attempt to do "helm install" I get a parse error where I use $kafka_port later in the template file. The specific error is:

Error: parse error at (tombolo/templates/tom-node-launch.yaml:89): undefined variable "$kafka_port"

答案1

得分: 2

在外部代码块中定义变量:

  1. {{- $kafka_port := 9094 -}}
  2. {{- if eq $.Values.kafka.create false -}}
  3. {{- $kafka_port = $.Values.kafka.port -}}
  4. {{- end }}

在外部代码块中定义变量:

  1. {{- $kafka_port := 9094 -}}
  2. {{- if eq $.Values.kafka.create false -}}
  3. {{- $kafka_port = $.Values.kafka.port -}}
  4. {{- end }}
英文:

Define the variable in the outer block:

  1. {{- $kafka_port := 9094 -}}
  2. {{- if eq $.Values.kafka.create false -}}
  3. {{- $kafka_port = $.Values.kafka.port -}}
  4. {{- end }}

答案2

得分: 0

你的示例非常简短,对于你的用例可能是可以的。

但作为一般规则:当你开始在模板中编写代码时,这也可能是你应该在控制器部分(即Go代码)中编写代码的一个指示:

  1. type KafkaCfg struct {
  2. port int
  3. create bool
  4. }
  5. const kafkaCreationPort = 9094
  6. func (c KafkaCfg) Port() int {
  7. if c.create {
  8. return kafkaCreationPort
  9. }
  10. return c.port
  11. }
  12. func someWhereElse() {
  13. var data struct {
  14. Values struct {
  15. Kafka KafkaCfg
  16. }
  17. }
  18. data.Values.Kafka.create = true
  19. helmTemplate.Execute(w, data)
  20. }
  21. // 在你的模板中:
  22. {{ .Values.Kafka.Port }} {{ /* <- 将调用 .Port() 方法 */ }}

链接:https://play.golang.org/p/6pNHY0kcegp

英文:

Your example is pretty short and may well be ok for your use case.

But as a general rule : when you start writing code in your templates, it could also be an indication that you should write code in the controller part (the go code) :

  1. type KafkaCfg struct {
  2. port int
  3. create bool
  4. }
  5. const kafkaCreationPort = 9094
  6. func (c KafkaCfg) Port() int {
  7. if c.create {
  8. return kafkaCreationPort
  9. }
  10. return c.port
  11. }
  12. func someWhereElse() {
  13. var data struct {
  14. Values struct {
  15. Kafka KafkaCfg
  16. }
  17. }
  18. data.Values.Kafka.create = true
  19. helmTemplate.Execute(w, data)
  20. }
  21. // in your template :
  22. {{ .Values.Kafka.Port }} {{ /* &lt;- will call the .Port() method */ }}

https://play.golang.org/p/6pNHY0kcegp

答案3

得分: 0

Go的text/template语言(与Go编程语言本身不同)没有全局变量。

你可以编写一个辅助模板,通常放在templates/_helpers.tpl中,生成这个片段:

  1. {{- define "kafka.port" -}}
  2. {{- if .Values.kafka.create -}}
  3. 9094
  4. {{- else -}}
  5. {{- .Values.kafka.port -}}
  6. {{- end -}}

当调用这个模板时,它会输出端口号;它的单个参数应该是标准的顶级Helm对象

  1. - name: KAFKA_PORT
  2. value: {{ include "kafka.port" . | quote }}

或者,如果在特定上下文中需要将其作为变量使用,你可以在那里包含它(使用Helm include扩展将调用模板的输出捕获为字符串);但它将仅限于特定的模板或文件。

  1. {{- $kafkaPort := include "kafka.port" . }}
  2. - name: KAFKA_PORT
  3. value: {{ quote $kafkaPort }}
  4. - name: KAFKA_BROKER_URL
  5. value: kafka://{{ include "kafka.name" . }}:{{ $kafkaPort }}
英文:

The Go text/template language (as distinct from the Go programming language itself) doesn't have global variables.

The thing you can do is to write a helper template, typically in templates/_helpers.tpl, that generates this fragment:

  1. {{- define &quot;kafka.port&quot; -}}
  2. {{- if .Values.kafka.create -}}
  3. 9094
  4. {{- else -}}
  5. {{- .Values.kafka.port -}}
  6. {{- end -}}

This template outputs the port number when called; its single parameter should be the standard top-level Helm object.

  1. - name: KAFKA_PORT
  2. value: {{ include &quot;kafka.port&quot; . | quote }}

Or, if in a specific context, you happen to need it as a variable, you can include it there (using the Helm include extension to capture the called template's output as a string); but it will be limited to its specific template or file.

  1. {{- $kafkaPort := include &quot;kafka.port&quot; . }}
  2. - name: KAFKA_PORT
  3. value: {{ quote $kafkaPort }}
  4. - name: KAFKA_BROKER_URL
  5. value: kafka://{{ include &quot;kafka.name&quot; . }}:{{ $kafkaPort }}

huangapple
  • 本文由 发表于 2021年6月18日 02:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68025124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定